Here is updated code. Note, it assumes the web2py folder is at the top 
level within the home directory of the current user (you can change that 
back to the old behavior if desired). It also allows you to have multiple 
versions in different folders, such as "/web2py-2.15", "web2py-master", etc.

The new code updates some of the global_settings -- including the web2py 
version, which is causing the error you see.

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)

Anthony

On Thursday, November 9, 2017 at 5:16:14 AM UTC-5, Morganti wrote:
>
> Hi,
>
> I am using the web2py version 2.15.4 and used the these intructions to use 
> web2py with ipython. I am having the error below:
>
> if request.global_settings.web2py_version < "2.14.1":
> ----> 9     raise HTTP(500, "Requires web2py 2.13.3 or newer")
>      10 
>      11 ## if SSL/HTTPS is properly configured and you want all HTTP 
> requests to
>
> HTTP: 500 INTERNAL SERVER ERROR
>
> How to fix it?
>
> Thanks
> Best regards
> André
>
> Em segunda-feira, 12 de maio de 2014 19:31:04 UTC-3, Anthony escreveu:
>>
>> Sorry, I've been meaning to release the relevant code. Note, I did not 
>> create the web2py shell environment that way in IPython Notebook. Instead, 
>> the %w2p magic function directly creates a web2py environment (using one of 
>> the functions from gluon/shell.py). I created a web2py_magic.py file 
>> (attached) and put it in the .ipython/profile_nbserver/startup folder. 
>> Below is the code in that file:
>>
>> 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))
>>
>> 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).
>>
>> Note, you may have to edit the hard-coded path provided in the 
>> WEB2PY_PATH section of the code based on your server setup.
>>
>> The above code also defines the pp() and render() functions.
>>
>> Also, attached is the .ipynb file for the notebook I showed during the 
>> presentation.
>>
>> Finally, I have also attached a PDF of the slides, though it's not really 
>> intended to be read as a standalone document.
>>
>> Anthony
>>
>> On Monday, May 12, 2014 4:59:06 PM UTC-4, Johann Spies wrote:
>>>
>>> Thanks to Anthony for his talk about the hard way to learn Web2py.  
>>>
>>> Anthony I have been thinking of writing to you to ask about how you got 
>>> to know the inner workings of Web2py and you have answered about everything 
>>> in your talk at the conference - of which I saw the video today.
>>>
>>> I did not know about ipython notebook before I saw your presentation so 
>>> I tried it out but it complained a little bit:
>>>
>>> %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)
>>>
>>> In : 
>>>
>>>
>>> I have a further question - maybe because I do not have the time to 
>>> study the source code of ipython notebook as you would probably do :)
>>>
>>> How did you define the %w2p  macro or magic command?
>>>
>>> And 
>>>
>>> Your "render" function with which you could show the form - how did you 
>>> do it?
>>>
>>> Regards
>>> Johann
>>>
>>> -- 
>>> Because experiencing your loyal love is better than life itself, 
>>> my lips will praise you.  (Psalm 63:3)
>>>
>>

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to