Pillars of Python: Pyramid 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.

Built on the foundation of repoze.bfg, Pyramid is powerfully flexible but takes time to figure out

Pyramid is a Python Web framework being developed as part of the Pylons Project. Pyramid 1.0 was released this year. Prior to that, the Pylon Project's Web framework was, not surprisingly, Pylons. But Pylons has now entered legacy mode; the project website promises that Pylons will "continue to be maintained, but not enhanced."

Pyramid will run on any 2.x Python that is later than version 2.4. In addition, Pyramid can be run on the Java-based Python variant, Jython. The framework is based on the repoze.bfg framework -- sometimes simply called BFG -- with some additions. Pyramid incorporates concepts inspired by Zope, Pylons, and Django. It's important to note that Pyramid is not an update of Pylons; the two frameworks share no common code. Pylons programmers need not be left out in the cold, however. The Pylons Project provides a fallback wrapper that allows Pylons applications to run inside Pyramid, thus permitting Pylons developers to incrementally port their applications to Pyramid.

Pyramid is "policy free," meaning that Pyramid makes no assertions about the database you should use or place any strict requirements on the templating system employed. In fact, documentation says that Pyramid's template system is included only for convenience. Nor does Pyramid fit neatly into the MVC paradigm -- as with Django, there's no clear controller component. However, Pyramid goes a step further with its policy-free attitude: It's hard to find a model component.

Pyramid provides two mechanisms for mapping an URL to executable code. The first is a simple dispatch system that matches an URL -- or a set of URLs, as determined by some mechanism such as a regular expression -- to executable code. This technique is used in many frameworks, such as the Rails system and its Python implementation, Routes.

Pyramid: URLs to resources
Resources provide structure to an application, and in a very real sense, a Pyramid resource corresponds to the classic definition of that word: any entity that can be named and acted upon. Typically, a resource is either the representation of a persistent object (a collection of attributes), a container, or both. In processing an HTTP request's URI, the framework ultimately determines the context resource -- the one that will be used to process the request.

When you use the traversal URL mapping mechanism in Pyramid, resources act much like Zope 2's Zope objects. Pyramid parses the components of the URL, using them to describe a path beginning with the root resource and proceeding through resources issuing from the root. Because of this, Pyramid's creators often refer to the framework as supporting an RV (resource-view) architecture, rather than an MVC (model-view-controller) architecture.

When building Pyramid applications, you'll find yourself making heavy use of paster, a component of the Python Paste project. Paster is how one runs scripts that employ the paste module, which functions as a sort of glue layer between a WSGI-compliant Web server and a Web application.

Paste encapsulates a great deal of functionality. For example, paste provides code for authenticating user logins, validating WSGI compliance, and catching HTTP-related exceptions. Paste also provides a convenient way of executing Python scripts encapsulated as Python eggs -- a Python egg being an easily distributed Python application package, roughly analogous to the JAR file of the Java world.

Pyramid: Application decoration
Pyramid provides an unusual approach to application configuration. Rather than simply writing configuration information into a configuration file, you can employ "decoration." Using Python's decorator syntax, you can attach characteristics to an application's functions. The Pyramid framework scans the application source and processes its decorators for use at runtime. These decorators allow you to specify characteristics to functions that will allow the framework -- specifically, the view lookup subsystem -- to determine which "view callable" functions will respond to a given HTTP request. The view lookup subsystem is passed a context resource object and a request object, and from these -- as well as characteristics attached to view callable methods -- it determines which view callable will be executed in response to a given request.

Pyramid boasts a substantial collection of add-in packages. They include an XML-RPC package, an email system, a session and caching package, a transaction management package, and more. The website's documentation includes both narrative and reference manuals, as well as tutorials that illustrate how to create applications using either of Pyramid's supported URL mapping techniques, and those for using either ZODB as the back-end database or the Python SQLAlchemy package.

Pyramid is by no means an easy system to master. It includes a great deal of convention and terminology, mainly revolving around the paste tools. You must follow the introductory tutorial closely to figure out where everything goes and why. Nevertheless, it boasts an impressive pedigree and an equally impressive array of add-ons, and the "policy free" approach makes it adaptable -- with effort, of course -- to vastly different Web application architectures.

Read reviews of other Python Web frameworks:

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