Pyramid Explained

What is Pyramid

Pyramid is a Python web framework created from the combination of Pylons and repoze.bfg, resulting in a flexible, easy to use framework. Pyramid puts much of its focus in being flexible, so no application will be constrained by decisions made by the Pyramid creators. For example, you can use Mako or Chameleon for templating, just about any type of database for persistence, and a number of different methods for view routing (the list goes on). Many features of Pyramid are configurable or pluggable, so even if something you need isn't currently supported by Pyramid, that doesn't mean you can't easily extend the framework to do what you need.


Why is Pyramid Useful

Pyramid seems to have found a happy medium between flexibility and functionality. Not only can you easily configure and extend the framework, but it also provides plenty of helpful features. From file uploads to authentication to HTTP responses, Pyramid provides a way to handle just about anything you'd need in a website or web service.


How to Use Pyramid

It is typically best practice to use a virtual environment for projects like this. So once you have one set up (assuming your virtual environment is named 'env'), install Pyramid with:

$ cd env
$ bin/easy_install "pyramid==1.4.5"

Now that Pyramid is installed, you can create a project using one of the provided scaffolds. These scaffolds are basically just template projects with various configurations. The scaffolds provided by Pyramid are:

  • starter
    • URL mapping via URL dispatch and no persistence mechanism.
  • zodb
    • URL mapping via traversal and persistence via ZODB. According to Pyramid, this scaffold will not run under Python 3, only under Python 2.
  • alchemy
    • URL mapping via URL dispatch and persistence via SQLAlchemy.

Other scaffolds are available from third parties, like Niall O’Higgins' pyramid_mongodb, so search around if the provided scaffolds don't fit your needs.

And finally, to create the project, use:

$ bin/pcreate -s starter myapp

This command will use the starter scaffold to create your project. For more information on Pyramid projects, see their documentation.


Resources