Nevow: A Web Application Construction Kit

Donovan Preston developed nevow for Divmod, where he uses it to develop the web interface for Quotient, a personal communications management server. He previously worked for InterSight, where he developed Woven for PublishWorks, an automated publishing system using Python, and two smaller applications in both Zope and WebWare.

Nevow is a next-generation web application templating system, based on the ideas developed in the Twisted Woven package. Its main focus is on separating the HTML template from both the business logic and the display logic, while allowing the programmer to write pure Python code as much as possible. It separates your code into 'data' and 'render' functions, a simplified implementation of traditional MVC. It has various parts which can be used individually or as a whole, integrated web solution:

formless: For describing the types of objects which may be passed to methods of your classes, validating and coercing string input from either web or command-line sources, and calling your methods automatically once validation passes

freeform: For rendering web forms based on formless type descriptions, accepting form posts and passing them to formless validators, and rendering error forms in the event validation fails

livepage: Cross-browser JavaScript glue for sending client side events to the server and server side events to the client after the page has loaded, without causing the entire page to refresh

Nevow includes the ability to load templates off disk. These templates may have processing directives which cause the execution of python methods at render time. The attribute technique was inspired by the attributes used by ZPT. However, no actual code may be embedded in the HTML template:

To allow clean isolation between code which fetches data from a data source and code which renders the data into HTML, nevow allows you to write both 'data' methods and 'render' methods. These concepts are inspired by MVC, but simpler, since the framework can handle most of the controller aspect. An example:

One of the most powerful things about nevow is stan, an s-expression-like syntax for producing XML fragments in pure Python syntax. Stan is not required for using nevow, but it is both a simple and powerful way to both lay out one's XHTML templates and express one's display logic. A brief example will illustrate its utility:

Python is dynamically typed, which means it has no built-in controls for enforcing the types of objects which are passed to one's methods. This is great for programmers, but not necessarily great if you are going to be passing user-entered input to those methods. Formless is a simple way to describe the types of objects that can be passed to one's methods, as well as coerce from string input to those types. Other code can then accept user input from a command line or from a web form, validate the input against the types described using formless, and call the method once validation has passed. A simple example:

Freeform is a nevow module which will automatically render web forms and accept form posts based on types described using the classes in formless. Used in conjunction with the twisted.web HTTP server, the process is almost automatic:

Exposing this resource instance to the web using twisted.web and visiting it will cause a form with two input boxes to be rendered. Posting the form will cause form validation to occur. Upon error, the user will be returned to the original page, with the form annotated with error messages. Upon success, the "simple" method of the Implementation instance will be called and passed a string and an integer.

LivePage was a Woven technology which allowed programmers to receive server-side notification of client-side JavaScript events, and to send JavaScript to the client in response to a server-side event. It has not yet been fully implemented for nevow, but an implementation is planned in the near future. When implemented, the usage would look something like this:

The nevow package contains many tools which are useful for performing web and XML related tasks, from generating simple XML documents using easy-to-write pure-python syntax to building a full-blown, highly interactive web application. Nevow was designed to allow application programmers to remove all logic constructs from HTML templates and to give them the power of pure Python as often as possible. It attempts to provide tools for writing one's application which allow you to do so as expressively as possible, so you can focus on the important parts of one's application instead of the mechanics of the web, while still allowing access to the low level details of HTML and HTTP when necessary.