Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Steve Piercy, a freelance web application developer in Soquel, California, where he grows fruits and veggies, and rides his bicycle for everyday transportation. Steve gives introductions to Pyramid at SFPython Hack Night Meetups and PyLadiesSF South Bay Meetups, and frequently contributes to open source projects including Pyramid and Knop. Contact Steve at or as stevepiercy in the #pyramid channel on

In my previous article, Hello Pyramid, I introduced Pyramid as a web application framework and you created a simple single-file web application. In this article, you will follow a typical development process and create a project using a scaffold. While doing so, you will learn how to run tests, use an application runner, and become familiar with how Pyramid works with requests, responses, and views.

If you have not yet established a standard Python environment nor installed Pyramid, please follow the Hello Pyramid post at this time. Now let’s get started!

Create a project using a scaffold

A scaffold generates a few of the major parts of a Pyramid application from a project template and from Pyramid add-ons, so you can quickly start developing your project. The list of available scaffolds can be shown with the following command:

Let’s use the starter scaffold.

NOTE: The alchemy and zodb scaffolds are used in the Pyramid tutorials: SQLAlchemy + URL Dispatch Wiki Tutorial and the ZODB + Traversal Wiki Tutorial, respectively.

Running tests and coverage

Pyramid is fully tested, and you should always run tests on your package during development to ensure everything continues to work as expected. “Fail early and often” is a better mantra than “What the heck did I do to cause this error?” The starter scaffold generated a module (located in myproject/hello_world/hello_world/), which contains the unit tests.

We will install both nose to run tests and coverage to warn us about any code that lacks test coverage.

Now let’s edit our package configuration in (located in myproject/hello_world/) in order to run tests and coverage:

Any time you change, you must run it. You also need to run before you can do any development. Now let’s set up our package for development, using a standard Python practice.

Now let’s run our tests.

Oops. Some of our code lacks test coverage, but the one test that ran passed. Although it would be good to add test coverage, we will focus on learning more about Pyramid than how to write tests, and have 100% test coverage of our code at this time. Let’s move on.

Application running with pserve

Previously, we ran our application using Python. This time we will make use of Pyramid’s application runner pserve. Let’s see its options.

One of its options, --reload, will monitor saved changes to files and automatically reload the application. That will save a couple of manual steps that we had to do in the previous post. We will also tell pserve to use application settings in the development.ini file.

Finally, open a web browser and visit the http://localhost:6543 URL. You should see the default Pyramid hello_world application. On the right-hand side, there is the Pyramid Debug Toolbar.

NOTE: Do not use --reload or the Pyramid Debug Toolbar in a production environment.

View the contents of development.ini and Environment Variables and .ini File Settings for an explanation of settings. For the settings in a typical production environment, see the contents of production.ini.

Requests, Responses, and Views

All web applications receive a request and return a response. Pyramid uses the Python library WebOb to handle requests and responses.

In Pyramid, a view callable accepts a request and returns a response. We’ll use the term “view” as shorthand for a view callable. Views can be function-based or class-based in Pyramid.

To tie requests to a view, we add a route to our application configuration. A route has a name and an URL pattern, such as /aliens.

We will take advantage of Pyramid’s decorator-based configuration, so that our configuration and code are next to each other and we won’t have to switch back and forth between files.

Now, let’s write some code to see how we would create a new view that will accept a request and return a response.

Edit the file myproject/hello_world/hello_world/, and add a new route to the application configuration as indicated on line 10.

In the above example, we add a route named aliens with the URL pattern of /aliens. Any request that matches this URL will be routed to the view, which we will now create.

Edit the file myproject/hello_world/hello_world/, and add a new view using a decorator as indicated on lines 9-11:

  • Line 9 is a decorator that configures our view using the route named aliens.
  • Line 10 is a Python function that accepts the request.
  • Line 11 sets a variable action to the value of the parameter action from the GET request. If the parameter action is not part of the request, then the value " do nothing" will be assigned to the variable by default.
  • Line 12 sets a variable body to an expression containing some text, HTML, and a string placeholder %s.
  • Line 13 evaluates the expression body by replacing the string placeholder with the value of action. The result is returned as a response, which is viewed in the web browser.

Let’s run our modified application.

Open a web browser, and request the URL. You should see the response "Aliens do nothing."

But we want aliens to take action, so let’s modify that URL by appending to the URL in the web browser’s address bar "?action=ate my Buick". You should see the response "Aliens ate my Buick."


Pyramid comes with scaffolds that help you quickly start your projects. It’s a good practice to write tests and ensure that your code passes those tests. Pyramid uses the application runner pserve, which gives you many options that can facilitate development. Finally, Pyramid routes requests to views that return a response.

From here, you can explore some of the official Pyramid Tutorials, try out some recipes from the Pyramid Cookbook, or walk through the Quick Tour of Pyramid.

PyCharm is an intelligent Python IDE, and this video on PyCharm shows how to get started with the Pyramid web application framework using PyCharm in under five minutes.

For more details about Python, see the Safari Books Online resources referenced below.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Learning Python, 5th Edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.
Hello! Python fully covers the building blocks of Python programming and gives you a gentle introduction to more advanced topics such as object-oriented programming, functional programming, network programming, and program design. New (or nearly new) programmers will learn most of what they need to know to start using Python immediately.
Violent Python shows you how to move from a theoretical understanding of offensive computing concepts to a practical implementation. This book demonstrates how to write Python scripts to automate large-scale network attacks, extract metadata, and investigate forensic artifacts. It also shows how to write code to intercept and analyze network traffic using Python, craft and spoof wireless frames to attack wireless and Bluetooth devices, and how to data-mine popular social media websites and evade modern anti-virus.

Tags: alchemy, pserve, Pyramid, Python, Requests, Responses, scaffold, tests, views, zodb,

Comments are closed.