{{extend 'layout.html'}} {{import os}}
Here are some working and complete examples that explain the basic syntax of the framework.
You can click on the web2py keywords (in the highlighted code!) to get documentation.
If the controller function returns a string, that is the body of the rendered page.
Try it here: hello1
The function T() marks strings that need to be translated. Translation dictionaries can be created at /admin/default/design
Try it here: hello2
If you return a dictionary, the variables defined in the dictionery are visible to the view (template).
Try it here: hello3
You can change the view, but the default is /[controller]/[function].html. If the default is not found web2py tries to render the page using the generic.html view.
Try it here: hello4
You can also generate HTML using helper objects HTML, BODY, H1, etc. Each of these tags is an class and the views know how to render the corresponding objects. The method .xml() serializes them and produce html/xml code for the page. Each tag, DIV for example, takes three types of arguments:
Try it here: hello5
Here we are showing the request, session ad response objects using the generic.html template.
Try it here: status
You can do redirect.
Try it here: redirectme
You can raise HTTP exceptions to return an error page.
Try it here: raisehttp
If an exception occurs (other than HTTP) a ticket is generated and the event is logged for the administrator. These tickets and logs can be accessed, reviewed and deleted and any later time.
Try it here: raiseexception
You can serve other than HTML pages by changing the contenttype via the response.headers. The gluon.contenttype module can help you figure the type of the file to be server. NOTICE: this is not necessary for static files unless you want to require authorization.
Try it here: servejs
If you are into Ajax, web2py includes gluon.contrib.simplejson, developed by Bob Ippolito. This module provides a fast and easy way to serve asynchronous content to your Ajax page. gluon.simplesjson.dumps(...) can serialize most Python types into JSON. gluon.contrib.simplejson.loads(...) performs the reverse operation.
Try it here: makejson
web2py also includes gluon.contrib.pyrtf, developed by Simon Cusack and revised by Grant Edwards. This module allows you to generate Rich Text Format documents including colored formatted text and pictures.
Try it here: makertf
web2py includes gluon.contrib.rss2, developed by Dalke Scientific Software, which generates RSS2 feeds, and
gluon.contrib.feedparser, developed by Mark Pilgrim, which collects RSS and ATOM feeds. The above controller collects a slashdot feed and makes new one.
Try it here: rss_aggregator
web2py also includes gluon.contrib.markdown (markdown2) which converts WIKI markup to HTML following this syntax. In this example we added a fancy ajax effect.
Try it here: ajaxwiki
Click to count. The session.counter is persistent for this user and application. Every applicaiton within the system has its own separate session management.
Try it here: counter
A view (also known as template) is just an HTML file with {{...}} tags. You can put ANY python code into the tags, no need to indent but you must use pass to close blocks. The view is transformed into a python code and then executed. {{=a}} prints a.xml() or escape(str(a)).
Try it here: variables
You can do for and while loops.
Try it here: test_for
You can do if, elif, else.
Try it here: test_if
You can do try, except, finally.
Try it here: test_try
You can write functions in HTML too.
Try it here: test_def
The argument of {{=...}} is always escaped unless it is an object with a .xml() method such as link, A(...), a FORM(...), a XML(...) block, etc.
Try it here: escape
If you do not want to esacpe the argument of {{=...}} mark it as XML.
Try it here: xml
You can use BEUTIFY to turn lists and dictionaries into organized HTML.
Try it here: beautify
You can specify the layout file at the top of your view. civilized Layout file is a view that somewhere in the body contains {{include}}.
Try it here: civilized
Same here, but using a different template.
Try it here: slick
'layout.html' is the default template, every applicaiton has a copy of it.
Try it here: basic
You can use HTML helpers like FORM, INPUT, TEXTAREA, OPTION, SELECT to build forms. the "value=" attribute sets the initial value of the field (works for TEXTAREA and OPTION/SELECT too) and the requires attribute sets the validators.
FORM.accepts(..) trys to validate the form and, on success, stores vars into form.vars. On failure the error messages are stored into form.errors and shown in the form.
Try it here: form
You can find more examples of the web2py ORM here
Let's create a simple model with users, dogs, products and purchases (the database of an animal store). Users can have many dogs (ONE TO MANY), can buy many producs and every product can have many buyers (MANY TO MANY).
Tables are created if they do not exist (try... except).
Here "purchased" is an SQLQuery object, "dba(purchased)" would be a SQLSet obejcts. A SQLSet object can be selected, updated, deleted. SQLSets can also be intersected. Allowed field types are string, integer, password, text, blob, upload, date, time, datetime, references(*), and id(*). The id field is there by default and must not be declared. references are for one to many and many to many as in the example above. For strings you should specify a length or you get length=32.
You can use dba.tablename.fieldname.requires= to set restrictions on the field values. These restrictions are automatically converted into widgets when generating forms from the table with SQLFORM(dba.tablename).
define_tables creates the table and attempts a migration if table has changed or if database name has changed since last time. If you know you already have the table in the database and you do not want to attemt a migration add one last argument to define_table migrate=False.
This is a simple user registration form. SQLFORM takes a table and returns the corresponding entry form with validators, etc. SQLFORM.accepts is similar to FORM.accepts but, if form is validated, the corresponding insert is also performed. SQLFORM can also do update and edit if a record is passed as its second argument.
SQLTABLE instead turns a set of records (result of a select) into an HTML table with links as specified by its optional parameters.
The response.menu on top is just a variable used by the layout to make the navigation menu for all functions in this controller.
Try it here: register_user
Here is a dog registration form. Notice that the "image" (type "upload") field is rendered into a <INPUT type="file"> html tag. SQLFORM.accepts(...) handles the upload of the file into the uploads/ folder.
Try it here: register_dog
Nothing new here.
Try it here: register_product
Here is a rather sophisticated buy form. It checks that the buyer and the product are in the database and updates the corresponding record or inserts a new purchase. It also does a JOIN to list all purchases.
Try it here: buy
This is an update on an SQLSet. (dba.purchase.id>0 identifies the set containing only table dba.purchases.)
Try it here: reset_purchased
This controller allows users to download the uploaded pictures of the dogs. Remember the upload=URL(...'download'...) statement in the register_dog function. Notice that in the URL path /application/controller/function/a/b/etc a, b, etc are passed to the controller as request.args[0], request.args[1], etc. Since the URL is validated request.args[] always contain valid filenames and no '~' or '..' etc. This is usefult to allow visitors to link uploaded files.
The output of lambda:time.ctime() is cached in ram for 5 seconds. The string 'time' is used as cache key.
Try it here: cache_in_ram
The output of lambda:time.ctime() is cached on disk (using the shelve module) for 5 seconds.
Try it here: cache_on_disk
The output of lambda:time.ctime() is cached on disk (using the shelve module) and then in ram for 5 seconds. web2py looks in ram first and if not there it looks on disk. If it is not on disk it calls the function. This is useful in a multiprocess type of environment. The two times do not have to be the same.
Try it here: cache_in_ram_and_disk
Here the entire controller (dictionary) is cached in ram for 5 seconds. The result of a select cannot be cached unless it is first serialized into a table lambda:SQLTABLE(dba().select(dba.users.ALL)).xml(). You can read below for an even better way to do it.
Try it here: cache_controller_in_ram
Here the entire controller (dictionary) is cached on disk for 5 seconds. This will not work if the dictionary contains unpickleble objects.
Try it here: cache_controller_on_disk
response.render(d) renders the dictionary inside the controller, so everything is cached now for 5 seconds. This is best and fastest way of caching!
Try it here: cache_controller_and_view
The results of a select are complex unpickable objects that cannot be cached using the previous method, but the select command takes an argument cache=(cache_model,time_expire) and will cache the result of the query accordingly. Notice that the key is not necessary since key is generated based on the database name and the select string.
Try it here: cache_db_select
The javascript function "ajax" is provided in "web2py_ajax.html" and included by "layout.html". It takes three arguments, a url, a list of ids and a target it. When called it send to the url (via a get) the values of the ids and display the respose in the value (of innerHTML) of the target id.
Try it here: index
Try it here: flash
Try it here: fade
Using the Python doctest notation it is possible to write tests for all controller functions. Tests are then run via the administrative interface which generates a report. Here is an example of a test in the code: {{=CODE(""" def index(): ''' This is a docstring. The following 3 lines are a doctest: >>> request.vars.name='Max' >>> index() {'name': 'Max'} ''' return dict(name=request.vars.name) """.strip(),language='web2py',link='/examples/global/vars/')}}
It is very easy in web2py to stream large files. Here is an example of a controller that does so:
{{=CODE(""" def streamer(): return response.stream(open('largefile.mpg4','rb'),chunk_size=4096) """.strip(),language='web2py',link='/examples/global/vars/')}}Web2py has native support for the XMLRPC protocol. Below is a controller function "handler" that exposes two functions, "add" and "sub" via XMLRPC. The controller "tester" executes the two function remotely via xmlrpc.
{{=CODE(""" def add(a,b): return a+b def sub(a,b): return a-b def handler(): return response.xmlrpc(request,[add,sub]) def tester(): import xmlrpclib server=xmlrpclib.ServerProxy('http://hostname:port/app/controller/handler') return str(server.add(3,4)+server.sub(3,4)) """.strip(),language='web2py',link='/examples/global/vars/')}}