wheezy.web / doc / examples.rst

Examples

We start with a simple example. Before we proceed let setup virtualenv environment:

$ virtualenv env

Since :ref:`wheezy.web` is template engine agnostic, you need specify extra requirements (per template engine of your choice):

$ env/bin/easy_install wheezy.web[jinja2]
$ env/bin/easy_install wheezy.web[mako]
$ env/bin/easy_install wheezy.web[tenjin]
$ env/bin/easy_install wheezy.web[wheezy.template]

Templates

Template application serves template purpose for you. It includes:

  • Integration with both mako and tenjin template system.
  • User registration and authentication.
  • Form validation.

If you are about to start a new project it is a good starting point.

Hello World

hello.py shows you how to use :ref:`wheezy.web` in a pretty simple WSGI application. It no way pretend to be shortest possible and absolutely not magical:

Handler Contract

Let have a look through each line in this application. First of all let take a look what is a handler:

This one is not changed from what you had in wheezy.http so you are good to keep it minimal. However there is added another one (that actually implements the same handler contract internally):

What is get method here? It is your response to HTTP GET request. You have post for HTTP POST, etc.

Routing

Routing is inherited from wheezy.routing. Note that both handlers are working well together:

Application

WSGIApplication is coming from wheezy.http. Integration with wheezy.routing is provided as middleware factory (:py:meth:`~wheezy.web.middleware.path_routing_middleware_factory`):

Functional Tests

You can easily write functional tests for your application using WSGIClient from wheezy.http (file test_hello.py).

For more advanced use cases refer to wheezy.http documentation, please.

Benchmark

You can add benchmark of your functional tests (file benchmark_hello.py):

Let run benchmark tests with nose (to be run from demos/hello directory):

$ ../../env/bin/nosetests-2.7 -qs -m benchmark benchmark_hello.py

Here is output:

hello: 2 x 20000
baseline throughput change target
  100.0%   11518rps  +0.0% test_welcome
   91.0%   10476rps  +1.1% test_home
----------------------------------------------------------------------
Ran 1 test in 3.686s
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.