Skip to content

Instantly share code, notes, and snippets.

@wgong
Created December 17, 2012 00:27
Show Gist options
  • Save wgong/4314583 to your computer and use it in GitHub Desktop.
Save wgong/4314583 to your computer and use it in GitHub Desktop.
use web2py inside IPython Notebook
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "web2py"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"use web2py module in other programs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"this tutorial is based on [Massimo's video](https://vimeo.com/21185623) \n",
"<p>(local=C:\\downloads\\web-framework\\python\\web2py\\videos\\web2py_plays_with_tornado,_cherrypy,_paste,_mako,_and_the_regular_python_shell_640x480.mp4)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import HTML\n",
"#HTML('<iframe src=https://vimeo.com/21185623 width=630 height=630></iframe>')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 37
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sys"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"use web2py source version"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sys.path.append('/web2py_git')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print sys.path"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['', 'D:\\\\Enthought\\\\lib\\\\site-packages\\\\tornado-2.4.post1-py2.7.egg', 'D:\\\\Enthought\\\\lib\\\\site-packages\\\\pip-1.2.1-py2.7.egg', 'D:\\\\Enthought\\\\lib\\\\site-packages\\\\ipython-0.13.2.dev-py2.7.egg', 'D:\\\\Enthought\\\\python27.zip', 'D:\\\\Enthought\\\\DLLs', 'D:\\\\Enthought\\\\lib', 'D:\\\\Enthought\\\\lib\\\\plat-win', 'D:\\\\Enthought\\\\lib\\\\lib-tk', 'D:\\\\Enthought', 'D:\\\\Enthought\\\\lib\\\\site-packages', 'D:\\\\Enthought\\\\lib\\\\site-packages\\\\PIL', 'D:\\\\Enthought\\\\lib\\\\site-packages\\\\win32', 'D:\\\\Enthought\\\\lib\\\\site-packages\\\\win32\\\\lib', 'D:\\\\Enthought\\\\lib\\\\site-packages\\\\Pythonwin', 'D:\\\\Enthought\\\\lib\\\\site-packages\\\\ipython-0.13.2.dev-py2.7.egg\\\\IPython\\\\extensions', '/web2py_git']\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"import the database abstraction layer"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from gluon.dal import DAL, Field"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"connect to SQLite db (db file is found at IPyNotebook folder: D:\\ipython\\ipython\\ipynb-examples)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db=DAL('sqlite://ipynb.sqlite')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"create a table to store my notebooks"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.define_table('ipynotebooks',Field('name'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "%d format: a number is required, not NoneType",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32mD:\\Enthought\\lib\\site-packages\\ipython-0.13.2.dev-py2.7.egg\\IPython\\core\\interactiveshell.pyc\u001b[0m in \u001b[0;36mrun_code\u001b[1;34m(self, code_obj)\u001b[0m\n\u001b[0;32m 2730\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mCustomTB\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0metype\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2731\u001b[0m \u001b[1;32mexcept\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2732\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshowtraceback\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2733\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2734\u001b[0m \u001b[0moutflag\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mD:\\Enthought\\lib\\site-packages\\ipython-0.13.2.dev-py2.7.egg\\IPython\\core\\interactiveshell.pyc\u001b[0m in \u001b[0;36mshowtraceback\u001b[1;34m(self, exc_tuple, filename, tb_offset, exception_only)\u001b[0m\n\u001b[0;32m 1699\u001b[0m \u001b[1;31m# Though this won't be called by syntax errors in the input\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1700\u001b[0m \u001b[1;31m# line, there may be SyntaxError cases with imported code.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1701\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshowsyntaxerror\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1702\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0metype\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mUsageError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1703\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite_err\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"UsageError: %s\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mD:\\Enthought\\lib\\site-packages\\ipython-0.13.2.dev-py2.7.egg\\IPython\\core\\interactiveshell.pyc\u001b[0m in \u001b[0;36mshowsyntaxerror\u001b[1;34m(self, filename)\u001b[0m\n\u001b[0;32m 1756\u001b[0m \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1757\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1758\u001b[1;33m \u001b[0mstb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSyntaxTB\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstructured_traceback\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0metype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1759\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_showtraceback\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0metype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1760\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mD:\\Enthought\\lib\\site-packages\\ipython-0.13.2.dev-py2.7.egg\\IPython\\core\\ultratb.pyc\u001b[0m in \u001b[0;36mstructured_traceback\u001b[1;34m(self, etype, value, elist, tb_offset, context)\u001b[0m\n\u001b[0;32m 489\u001b[0m \u001b[0mout_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_format_list\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0melist\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 490\u001b[0m \u001b[1;31m# The exception info should be a single entry in the list.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 491\u001b[1;33m \u001b[0mlines\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m''\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_format_exception_only\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0metype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 492\u001b[0m \u001b[0mout_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlines\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 493\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mD:\\Enthought\\lib\\site-packages\\ipython-0.13.2.dev-py2.7.egg\\IPython\\core\\ultratb.pyc\u001b[0m in \u001b[0;36m_format_exception_only\u001b[1;34m(self, etype, value)\u001b[0m\n\u001b[0;32m 571\u001b[0m (Colors.normalEm,\n\u001b[0;32m 572\u001b[0m \u001b[0mColors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfilenameEm\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mColors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnormalEm\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 573\u001b[1;33m Colors.linenoEm, value.lineno, Colors.Normal ))\n\u001b[0m\u001b[0;32m 574\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtext\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 575\u001b[0m \u001b[0mi\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: %d format: a number is required, not NoneType"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.tables"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 11,
"text": [
"['ipynotebooks']"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.tables[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 14,
"text": [
"'ipynotebooks'"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.ipynotebooks"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 15,
"text": [
"<Table ipynotebooks (id,name)>"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from gluon.contrib.populate import populate"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"populate(db.ipynotebooks,3)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for row in db(db.ipynotebooks).select():\n",
" print row.name"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Dusamodu\n",
"Comososa\n",
"Dududuto\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"use web2py helpers from a regular python program"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from gluon.html import *\n",
"d = DIV('hello',SPAN('world',_id='hello'))\n",
"print d"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<div>hello<span id=\"hello\">world</span></div>\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d.append(A('click me',_href='http://web2py.com'))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print d"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<div>hello<span id=\"hello\">world</span><a href=\"http://web2py.com\">click me</a></div>\n"
]
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d.element('span#hello')[0]='WORLD'\n",
"print d"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<div>hello<span id=\"hello\">WORLD</span><a href=\"http://web2py.com\">click me</a></div>\n"
]
}
],
"prompt_number": 29
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"use forms and validators from a regular python program"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from gluon.sqlhtml import SQLFORM"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 31
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from gluon.validators import IS_NOT_EMPTY"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from gluon.languages import *"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.ipynotebooks.name.requires=IS_NOT_EMPTY()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"form = SQLFORM(db.ipynotebooks)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'thread._local' object has no attribute 'T'",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-36-c6b1cb76b770>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mform\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mSQLFORM\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mipynotebooks\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32mD:\\web2py_git\\gluon\\sqlhtml.pyc\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, table, record, deletable, linkto, upload, fields, labels, col3, submit_button, delete_label, showid, readonly, comments, keepopts, ignore_rw, record_id, formstyle, buttons, separator, **attributes)\u001b[0m\n\u001b[0;32m 911\u001b[0m \u001b[0mlinkto\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mURL\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'table/db/'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 912\u001b[0m \"\"\"\n\u001b[1;32m--> 913\u001b[1;33m \u001b[0mT\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcurrent\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 914\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 915\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mignore_rw\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mignore_rw\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mAttributeError\u001b[0m: 'thread._local' object has no attribute 'T'"
]
}
],
"prompt_number": 36
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment