Pillars of Python: Web2py Web framework


Become An Insider

Sign up now and get free access to hundreds of Insider articles, guides, reviews, interviews, blogs, and other premium content from the best tech brands on the Internet: CIO, CSO, Computerworld, InfoWorld, IT World and Network World Learn more.

Web2py combines abundant features and powerful tools, backed by excellent documentation

Web2py was created by Massimo Di Pierro, a professor of computer science at DePaul University, as his attempt to build a Python-based Web framework that was easy to use as well as powerful. It's a well-crafted, rock-solid framework that, like Zope 2, provides its own development environment.

An example of Web2py's convenience is its uncluttered API. The Web2py core is trimmed to an essential 12 objects, largely thanks to the fact that the API was completely designed before being implemented. In addition, Web2py boasts a small footprint: about 300KB of code for the system core, database abstraction layer, templating language, and supporting functions.

Examples of Web2py's power are its Web-based soup-to-nuts administration and development console, the database abstraction layer that supports virtually every RDBMS that Python can support, and its "components" -- Web-page entities that are provided with their own view/controller code, but are manipulated via AJAX so that component updates don't require full-page updates.

Web2py lives happily with any 2.x version of Python, though the official supported version is 2.5. It will also run on Jython. It provides its own built-in Web server -- specifically, the CherryPy Web server -- though any WSGI-compliant Web server will suffice.

Web2py: Powerful administration
When you start the Web-based admin application for the first time, it prompts for an administrator's password, after which access to the admin console is secure. From the console you can create a new application, edit the various elements of an application (models, controllers, views), and even "clean up" an application -- an operation that removes temporary application data such as sessions and cached information. You can also package an application for deployment. The admin console's packaging system will bundle all application files (including database definitions) into a tar gzipped file referred to as a "w2p" file. Of course, the admin console can also be used to install a packaged application.

Constructing a new application is easy with the help of the admin console's application wizard. It walks you through the process a step at a time, beginning with simple information like the application's name, opening page title, author's name and email, and a URI to the app's database. The wizard also gives you well over two dozen view themes to choose from.

Subsequent creation steps let you define your application's database tables, the fields in each table, and the Web pages in the application. Once all the application definitions are done, you can choose to generate the application's code. Or if you feel like manual labor, you can choose to generate only portions of the application and build the rest yourself. The admin console includes its own editor, though you can use any editor you wish.

Once you've created your application's initial infrastructure, the admin console presents links to subwizards for editing your application's models, views, controllers, plug-ins, and more. The admin console even assists in adding internationalization support. If, while testing your application, an error occurs, the admin console provides a link to an error ticket page where you can peruse a wealth of contextual information. Not only is traceback available, but you can also examine the application's locals, as well as the request, session, and response objects in the context of the failure.

Web2py: Excellent documentation
Web2py's principal documentation is "The Official Web2py Book," available free and online. It is an excellent resource, well organized and illustrated, and provides a searchable interface. The website also allows access to an Epydoc interface for the framework's API. Epydoc is a tool that produces a searchable interface into Python module APIs based on their docstrings.

Developers will probably find the Quick Examples section of the website most useful. This is a treasure trove of simple example applications, each addressing a separate development topic. There are examples that illustrate each of the model, view, and controller components of Web2py. Each of the view examples provides a Try It Here link, so you can manipulate view code and immediately see its effects.

Web2py promises total backward compatibility. Since its introduction in late 2007, that promise has been broken only occasionally. And when it has, the Web2py engineers have treated it as a bug. The only changes to the API are made for security issues. All this has been accomplished in spite of significant new additions such as role-based access control, pluggable authentication modules, RESTful Web services, and more.

Web2py is a powerfully capable Web framework with plenty of pleasant surprises under its hood. For example, its rendering engine is not limited to HTML, though HTML is the default. The framework provides view rendering in HTML, XML, JSON, RSS, CSV, RTF, and several more custom protocols. Web2py's templating engine lets you embed Python code directly in HTML, with little restriction on what elements of the Python language can be used.

The abundance of features in Web2py can be initially daunting, and mastery of the framework requires time, mainly spent working through the administration console. Nevertheless, Web2py is among the best of the Python Web frameworks, thanks largely to its creator's careful up-front design and ongoing improvements.

To continue reading, please begin the free registration process or sign in to your Insider account by entering your email address:
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
You Might Like
Join the discussion
Be the first to comment on this article. Our Commenting Policies