CherryPy 3ΒΆ

To run this example you need to install CherryPy, Genshi and tw.forms:

$ easy_install "CherryPy>=3.0.0" Genshi tw.forms
from wsgiref.validate import validator
from pprint import pformat

from pkg_resources import require
require('CherryPy>=3.0.0', 'tw.forms', 'Genshi')

import cherrypy
from genshi.template import Context, MarkupTemplate
from genshi.output import DocType
from formencode import Invalid
from tw.forms.samples import AddUserForm

template = MarkupTemplate("""\
<html xmlns=""
  <head> <title>SimpleWSGI app using tw.forms and ToscaWidgets</title></head>

# Dummy model objects to prefill the form.
class Address(object):
    def __init__(self, **kw):

class Person(object):
    name = "Peter"
    email = "peter@example"
    age = 2
    address = [Address(street="Elm Street")]

# The form instance. Note that widgets are usually module-level singletons.
# Once initialized they are state-less so they are safe to use in several
# threads while serving concurrent requests.
myform = AddUserForm('form')

class Controller(object):
    def index(self, **form_data):
        if form_data:
                value = myform.validate(form_data)
                return ('<h1>Validated data:</h1>'
                        '<a href="./">Again</a>' % pformat(value))
            except Invalid, error:
        ctx = Context(value=Person(), form=myform)
        t = template.generate(ctx)
        cherrypy.response.headers['Content-Type'] = 'text/html; charset=UTF8'
        return  t.render(method='xhtml', doctype=DocType.XHTML) = True

#XXX I'm not sure how can I pass args. to the middleware with CP's config
#    pipeline API, hence this hacky wrapper. Any better way of doing this?
def tw_middleware(app):
    from tw.api import make_middleware
    conf = {'toscawidgets.framework.default_view':'genshi',
            'toscawidgets.middleware.inject_resources': 'true'}
    return make_middleware(app, conf, stack_registry=True)

cp_config = {
    '/': {'wsgi.pipeline': [
	('validator1', validator),
        ('tw', tw_middleware),
	('validator2', validator),

if __name__ == "__main__":
    cherrypy.quickstart(Controller(), '', cp_config)

Previous topic


Next topic

This Page

Quick search