Yes, you can run a web2py app in a shell. At a shell command line (in the 
/web2py folder):

python web2py.py -S app -M -N

The -S tells it to run "app" in a shell. The -M tells it to run the model 
files (so once you're in the shell, you'll have access to any objects 
defined in your model files, such as "db"). The -N tells it not to start 
cron. Note, in place of "app", you can have app/controller, which executes 
the specified controller as well -- that enables you to call specific 
functions within that controller from the shell (you can also specify 
app/controller/function to execute a specific function directly, but that 
generally isn't too useful). If IPython is installed, it will use that for 
the shell by default (though you can also specify a plain shell). For more 
details, 
see http://web2py.com/books/default/chapter/29/4#Command-line-options.

Note, within the shell, printing any web2py HTML helpers (including SQLFORM 
objects) will serialize the helpers into the HTML that would be included in 
the response -- that can be useful for understanding the structure of 
nested objects (like forms) and how to modify them via the server-side DOM 
(http://web2py.com/books/default/chapter/29/5#Server-side-DOM-and-parsing). 
An alternative to printing is calling the .xml() method -- so "print 
myform" and "myform.xml()" will both yield the serialized version of the 
form object.

One thing to keep in mind is that in the shell, database operations do not 
get committed automatically, so if you do an insert, update, or delete, you 
have to explicitly call db.commit() for the operation to be executed.

Finally, note that the "admin" app includes its own web-based shell. 
However, it doesn't always work quite as well as a regular system shell (in 
particular, I have found that database operations don't always work).

Anthony

On Sunday, February 19, 2012 6:45:14 PM UTC-5, davidkw wrote:
>
> I'm wondering if there's some kind of way to use a shell or other IDE 
> to quickly run lines of web2py code and see the result. 
>
> For example, just entering db(query).select().first() and being able 
> to see the result. 
>
> I'm learning right now, and it's a little difficult to pick up the 
> syntax without being able to play around. Any advice? 
>
> Thanks.

Reply via email to