Here is a variation of something I use in Jupyter notebook:

import os
import gluon.shell
from gluon.settings import global_settings
from gluon.storage import Storage

def web2py_environment(app, controller=None, web2py_path=os.getcwd()):
    # Update global_settings.
    global_settings.gluon_parent = global_settings.applications_parent = 
web2py_path
    global_settings.cmd_options = Storage(shell=True) # for code that tests 
for shell
    with open(os.path.join(web2py_path, 'VERSION'), 'rb') as version_info:
        global_settings.web2py_version = version_info.read().split()[-1].
strip()
    # Create the web2py environment.
    environment = gluon.shell.env(app, import_models=True, c=controller,
        dir=os.path.join(web2py_path, 'applications', app))
    folder = environment['request'].folder
    if controller:
        pyfile = os.path.join(folder, 'controllers', controller + '.py')
        if os.path.isfile(pyfile):
            execfile(pyfile, environment)
    globals().update(**environment)

In your shell, import the above function and call:

web2py_environment('yourapp', 'yourcontroller')

Note, if the web2py folder is not the current working directory, you will 
need to explicity specify the web2py path (though you could certainly hard 
code that for your system rather than specifying it explicitly in the call).

Anthony




On Thursday, August 31, 2017 at 2:02:13 AM UTC-4, Brendan Barnwell wrote:
>
> I use DreamPie, a wrapper around the Python interpreter.  I would like to 
> be able to use the web2py console shell from within DreamPie.  I could also 
> imagine wanting to use it from within other alternative Python 
> interpreters, for instance inside the IPython QT console or a console 
> embedded in an IDE.
>
> The easiest way to do this would be if I could launch the web2py 
> environment from within an already-running Python session.  So that I would 
> start up whatever shell I want, and then do something like:
>
> >>> import web2py
> >>> web2py.set_up_shell(app='my_app')
>
> and have the same environment as if I had run "python web2py.py -S my_app".
>
> Is this possible?
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to