diff --git a/applications/examples/views/default/examples.html b/applications/examples/views/default/examples.html index adbb4840..b6b33de3 100644 --- a/applications/examples/views/default/examples.html +++ b/applications/examples/views/default/examples.html @@ -32,7 +32,7 @@ def hello1(): return "Hello World" """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}} -

If the controller function returns a string, that is the body of the rendered page.
Try it here: hello1

+

If the controller function returns a string, that is the body of the rendered page.
Try it here: hello1

Example {{=c}}{{c+=1}}

In controller: simple_examples.py {{=CODE(""" @@ -40,7 +40,7 @@ def hello2(): return T("Hello World") """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}} -

The function T() marks strings that need to be translated. Translation dictionaries can be created at /admin/default/design
Try it here: hello2

+

The function T() marks strings that need to be translated. Translation dictionaries can be created at /admin/default/design
Try it here: hello2

Example {{=c}}{{c+=1}}

In controller: simple_examples.py {{=CODE(""" @@ -51,7 +51,7 @@ def hello3(): and view: simple_examples/hello3.html {{=CODE(open(os.path.join(request.folder,'views/simple_examples/hello3.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

If you return a dictionary, the variables defined in the dictionery are visible to the view (template). -
Try it here: hello3

+
Try it here: hello3

Actions can also be be rendered in other formsts like JSON, hello3.json, and XML, hello3.xml

@@ -62,7 +62,7 @@ def hello4(): return dict(message=T("Hello World")) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

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

+
Try it here: hello4

Example {{=c}}{{c+=1}}

In controller: simple_examples.py {{=CODE(""" @@ -76,7 +76,7 @@ def hello5():
  • named arguments and name starts with '_'. These are mapped blindly into tag attributes and the '_' is removed. attributes without value like "READONLY" can be created with the argument "_readonly=ON".
  • named arguments and name does not start with '_'. They have a special meaning. See "value=" for INPUT, TEXTAREA, SELECT tags later. -

    Try it here: hello5

    +

    Try it here: hello5

    Example {{=c}}{{c+=1}}

    In controller: simple_examples.py {{=CODE(""" @@ -86,7 +86,7 @@ def hello6(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    response.flash allows you to flash a message to the user when the page is returned. Use session.flash instead of response.flash to display a message after redirection. With default layout, you can click on the flash to make it disappear. -
    Try it here: hello6

    +
    Try it here: hello6

    Example {{=c}}{{c+=1}}

    In controller: simple_examples.py {{=CODE(""" @@ -94,7 +94,7 @@ def status(): return dict(toobar=response.toolbar()) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    Here we are showing the request, session and response objects using the generic.html template. -
    Try it here: status

    +
    Try it here: status

    Example {{=c}}{{c+=1}}

    In controller: simple_examples.py {{=CODE(""" @@ -102,7 +102,7 @@ def redirectme(): redirect(URL('hello3')) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    You can do redirect. -
    Try it here: redirectme

    +
    Try it here: redirectme

    Example {{=c}}{{c+=1}}

    In controller: simple_examples.py {{=CODE(""" @@ -110,7 +110,7 @@ def raisehttp(): raise HTTP(400,"internal error") """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    You can raise HTTP exceptions to return an error page. -
    Try it here: raisehttp

    +
    Try it here: raisehttp

    Example {{=c}}{{c+=1}}

    In controller: simple_examples.py {{=CODE(""" @@ -128,7 +128,7 @@ def servejs(): return 'alert("This is a Javascript document, it is not supposed to run!");' """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    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 served. NOTICE: this is not necessary for static files unless you want to require authorization. -
    Try it here: servejs

    +
    Try it here: servejs

    Example {{=c}}{{c+=1}}

    In controller: simple_examples.py {{=CODE(""" @@ -136,7 +136,7 @@ def servejs(): return response.json(['foo', {'bar': ('baz', None, 1.0, 2)}]) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    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

    +
    Try it here: makejson

    New in web2py 1.63: Any normal action returning a dict is automatically serialized in JSON if '.json' is appended to the URL.

    @@ -152,7 +152,7 @@ def makertf(): response.headers['Content-Type']='text/rtf' return q.dumps(doc) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}} -

    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 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

    Example {{=c}}{{c+=1}}

    In controller: simple_examples.py {{=CODE(""" @@ -179,7 +179,7 @@ def rss_aggregator(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    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

    +
    Try it here: rss_aggregator

    Example {{=c}}{{c+=1}}

    In controller: simple_examples.py @@ -194,7 +194,7 @@ def ajaxwiki_onclick(): return MARKMIN(request.vars.text).xml() """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    The markmin wiki markup is described here. - web2py also includes gluon.contrib.markdown.WIKI helper (markdown2) which converts WIKI markup to HTML following this syntax. In this example we added a fancy ajax effect.
    Try it here: ajaxwiki

    + web2py also includes gluon.contrib.markdown.WIKI helper (markdown2) which converts WIKI markup to HTML following this syntax. In this example we added a fancy ajax effect.
    Try it here: ajaxwiki

    Session Examples

    @@ -207,7 +207,7 @@ def counter(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: session_examples/counter.html {{=CODE(open(os.path.join(request.folder,'views/session_examples/counter.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    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

    +
    Try it here: counter

    Template Examples

    @@ -219,7 +219,7 @@ def variables(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: template_examples/variables.html {{=CODE(open(os.path.join(request.folder,'views/template_examples/variables.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    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

    +
    Try it here: variables

    Example {{=c}}{{c+=1}}

    In controller: template_examples.py {{=CODE(""" @@ -228,7 +228,7 @@ def test_for(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: template_examples/test_for.html {{=CODE(open(os.path.join(request.folder,'views/template_examples/test_for.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    You can do for and while loops. -
    Try it here: test_for

    +
    Try it here: test_for

    Example {{=c}}{{c+=1}}

    In controller: template_examples.py {{=CODE(""" @@ -237,7 +237,7 @@ def test_if(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: template_examples/test_if.html {{=CODE(open(os.path.join(request.folder,'views/template_examples/test_if.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    You can do if, elif, else. -
    Try it here: test_if

    +
    Try it here: test_if

    Example {{=c}}{{c+=1}}

    In controller: template_examples.py {{=CODE(""" @@ -246,7 +246,7 @@ def test_try(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: template_examples/test_try.html {{=CODE(open(os.path.join(request.folder,'views/template_examples/test_try.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    You can do try, except, finally. -
    Try it here: test_try

    +
    Try it here: test_try

    Example {{=c}}{{c+=1}}

    In controller: template_examples.py {{=CODE(""" @@ -255,7 +255,7 @@ def test_def(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: template_examples/test_def.html {{=CODE(open(os.path.join(request.folder,'views/template_examples/test_def.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    You can write functions in HTML too. -
    Try it here: test_def

    +
    Try it here: test_def

    Example {{=c}}{{c+=1}}

    In controller: template_examples.py {{=CODE(""" @@ -264,7 +264,7 @@ def escape(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: template_examples/escape.html {{=CODE(open(os.path.join(request.folder,'views/template_examples/escape.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    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

    +
    Try it here: escape

    Example {{=c}}{{c+=1}}

    In controller: template_examples.py {{=CODE(""" @@ -273,7 +273,7 @@ def xml(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: template_examples/xml.html {{=CODE(open(os.path.join(request.folder,'views/template_examples/xml.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    If you do not want to escape the argument of {{=...}} mark it as XML. -
    Try it here: xml

    +
    Try it here: xml

    Example {{=c}}{{c+=1}}

    In controller: template_examples.py {{=CODE(""" @@ -282,7 +282,7 @@ def beautify(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: template_examples/beautify.html {{=CODE(open(os.path.join(request.folder,'views/template_examples/beautify.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    You can use BEAUTIFY to turn lists and dictionaries into organized HTML. -
    Try it here: beautify

    +
    Try it here: beautify

    Layout Examples

    @@ -298,7 +298,7 @@ def civilized(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: layout_examples/civilized.html {{=CODE(open(os.path.join(request.folder,'views/layout_examples/civilized.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    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

    +
    Try it here: civilized

    Example {{=c}}{{c+=1}}

    In controller: layout_examples.py {{=CODE(""" @@ -310,7 +310,7 @@ def slick(): return dict(message="you clicked on slick") """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: layout_examples/slick.html {{=CODE(open(os.path.join(request.folder,'views/layout_examples/slick.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}} -

    Same here, but using a different template.
    Try it here: slick

    +

    Same here, but using a different template.
    Try it here: slick

    Example {{=c}}{{c+=1}}

    In controller: layout_examples.py {{=CODE(""" @@ -323,7 +323,7 @@ def basic(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}and view: layout_examples/basic.html {{=CODE(open(os.path.join(request.folder,'views/layout_examples/basic.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    'layout.html' is the default template, every application has a copy of it. -
    Try it here: basic

    +
    Try it here: basic

    Form Examples

    @@ -347,7 +347,7 @@ def form(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    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(..) tries 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

    +
    Try it here: form

    Database Examples

    @@ -497,7 +497,7 @@ def cache_in_ram(): return dict(time=t,link=A('click to reload',_href=URL(r=request))) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    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

    +
    Try it here: cache_in_ram

    Example {{=c}}{{c+=1}}

    In controller: cache_examples.py @@ -508,7 +508,7 @@ def cache_on_disk(): return dict(time=t,link=A('click to reload',_href=URL(r=request))) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    The output of lambda:time.ctime() is cached on disk (using the shelve module) for 5 seconds. -
    Try it here: cache_on_disk

    +
    Try it here: cache_on_disk

    Example {{=c}}{{c+=1}}

    In controller: cache_examples.py {{=CODE(""" @@ -519,7 +519,7 @@ def cache_in_ram_and_disk(): return dict(time=t,link=A('click to reload',_href=URL(r=request))) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    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

    +
    Try it here: cache_in_ram_and_disk

    Example {{=c}}{{c+=1}}

    In controller: cache_examples.py @@ -530,7 +530,7 @@ def cache_in_ram_and_disk(): t=time.ctime() return dict(time=t,link=A('click to reload',_href=URL(r=request)))""".strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    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(db().select(db.user.ALL)).xml(). You can read below for an even better way to do it. -
    Try it here: cache_controller_in_ram

    +
    Try it here: cache_controller_in_ram

    Example {{=c}}{{c+=1}}

    In controller: cache_examples.py {{=CODE(""" @@ -541,7 +541,7 @@ def cache_controller_on_disk(): return dict(time=t,link=A('click to reload',_href=URL(r=request))) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    Here the entire controller (dictionary) is cached on disk for 5 seconds. This will not work if the dictionary contains unpickleable objects. -
    Try it here: cache_controller_on_disk

    +
    Try it here: cache_controller_on_disk

    Example {{=c}}{{c+=1}}

    In controller: cache_examples.py {{=CODE(""" @@ -553,7 +553,7 @@ def cache_controller_and_view(): return response.render(d) """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}

    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

    +
    Try it here: cache_controller_and_view

    Example {{=c}}{{c+=1}}

    In controller: cache_examples.py {{=CODE(""" @@ -583,7 +583,7 @@ def data(): In view: ajax_examples/index.html {{=CODE(open(os.path.join(request.folder,'views/ajax_examples/index.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}

    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 id. When called, it sends to the url (via a get) the values of the ids and display the response in the value (of innerHTML) of the target id. -
    Try it here: index

    +
    Try it here: index

    Example {{=c}}{{c+=1}}

    In controller: ajax_examples.py {{=CODE(""" @@ -591,7 +591,7 @@ def flash(): response.flash='this text should appear!' return dict() """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}} -

    Try it here: flash

    +

    Try it here: flash

    Example {{=c}}{{c+=1}}

    In controller: ajax_examples.py {{=CODE(""" @@ -600,7 +600,7 @@ def fade(): """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}} In view: ajax_examples/fade.html
    {{=CODE(open(os.path.join(request.folder,'views/ajax_examples/fade.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}} -

    Try it here: fade

    +

    Try it here: fade

    Excel-like spreadsheet via Ajax

    Web2py includes a widget that acts like an Excel-like spreadsheet and can be used to build forms