%run /home/js/web2py/web2py.py -M -S alterit
WARNING:web2py:import IPython error; use default python shell Python 2.7.6 (default, Mar 22 2014, 15:40:47) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole)
web2py Web Framework Created by Massimo Di Pierro, Copyright 2007-2014 Version 2.9.5-trunk+timestamp.2014.04.15.10.26.52 Database drivers available: SQLite(sqlite3), MySQL(pymysql), MySQL(MySQLdb), PostgreSQL(pg8000), MSSQL(pyodbc), DB2(pyodbc), Teradata(pyodbc), Ingres(pyodbc), IMAP(imaplib)
import os
import sys
import xml.dom.minidom as xml
from IPython.core.magic import register_line_magic
from IPython.core import display
cwd = os.getcwd()
if cwd.endswith(os.path.sep + 'web2py'):
WEB2PY_PATH = cwd
else:
WEB2PY_PATH = os.path.join('/', 'home', 'www-data', 'web2py')
sys.path.append(WEB2PY_PATH)
import gluon.shell
from gluon.dal import Rows
from gluon.sqlhtml import SQLTABLE
@register_line_magic
def w2p(line):
line = 'test/default' if not line else line.encode('ascii')
line = line.split('/')
app = line[0]
controller = line[1] if len(line) > 1 else None
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)
def pp(helper, indent=' '):
declaration = len(xml.Document().toxml()) + 1
doc = xml.parseString(helper.xml())
print XML(doc.toprettyxml(indent=indent)[declaration:])
def render(html):
if isinstance(html, Rows):
html = SQLTABLE(html)
return display.HTML(str(html))
%w2p myapp/mycontroller
So, just start up a notebook, and at the top, run:%w2p myapp/mycontroller
and you will get a full web2py environment with the models from myapp as well as the (optionally) specified controller (so you can run functions from that controller). Should be easy to add an optional command line flag to later add other controllers (without overwriting the full environment).
line = 'test/default'
line = line.split('/')
line['test', 'default']
app = line[0]
controller = line[1] if len(line) > 1 else None
WEB2PY_PATH = os.path.join('/', 'home', 'js', 'web2py')
sys.path.append(WEB2PY_PATH)
environment = gluon.shell.env(app, import_models=True, c=controller,
dir=os.path.join(WEB2PY_PATH, 'applications', app))
An exception has occurred, use %tb to see the full traceback. SystemExit: 1
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/gluon/restricted.py", line 205, in restricted
exec ccode in environment
File "/home/js/web2py/applications/test/models/db.py", line 48, in <module>
auth.define_tables(username=False, signature=False)
TypeError: define_tables() got an unexpected keyword argument 'signature'
To exit: use 'exit', 'quit', or Ctrl-D.
An exception has occurred, use %tb to see the full traceback. SystemExit: 1
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/gluon/restricted.py", line 205, in restricted
exec ccode in environment
File "/home/js/web2py/applications/nkb/models/0.py", line 18, in <module>
lazy_tables=True, bigint_id = True)
TypeError: __init__() got an unexpected keyword argument 'lazy_tables'
REgards
Johann
--
Is it possible you are running an old version of web2py that doesn't support those arguments. Based on the restricted.py line number shown in the traceback, it looks like you're on web2py 1.99 or earlier.
File "/usr/lib/pymodules/python2.7/gluon/restricted.py", line 205, in restricted exec ccode in environment
Many thanks for your talk Anthony. It was really very useful for me. Particularly with the addition of these files. Working inside ipython notebook will be a real asset. By the way for windows users the magic python file should go in
import os
import sys
import xml.dom.minidom as xml
from IPython.core.magic import register_line_magic
from IPython.core import display
@register_line_magic
def w2p(line):
'''
`line` is of the form: app[/controller] [web2py_version], where controller
and web2py_version are optional, and web2py_version is the folder suffix
(excluding the hyphen) identifying the version (e.g., "2.15", "dev", etc.).
'''
line = 'test/default' if not line else line.encode('ascii')
line = line.split(' ')
# Add the web2py path to the Python search path.
web2py_path = os.path.join(os.path.expanduser('~'), 'web2py')
if len(line) > 1:
web2py_path += '-%s' % line[1]
sys.path.insert(1, web2py_path) # Ensure web2py is early in the search path.
import gluon.shell
from gluon.settings import global_settings
from gluon.storage import Storage
line = line[0].split('/')
app = line[0]
controller = line[1] if len(line) > 1 else None
# 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)