Pyramid Requests, Responses and Views

This article was originally published on Safari Books Online and can be read in its entirety at Pyramid Requests, Responses and Views. An abridged version follows.

Introduction

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 and installed Pyramid, please follow the Hello Pyramid tutorial 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 Pyramid add-ons, so you can quickly start developing your project. The list of available scaffolds can be shown with the following command:

1
2
3
4
5
6
7
# Use the $VENV environment variable we set in the "Hello Pyramid" article.
# $VENV should be set to your virtual environment path.
$ $VENV/bin/pcreate --list
Available scaffolds:
  alchemy:  Pyramid SQLAlchemy project using url dispatch
  starter:  Pyramid starter project
  zodb:     Pyramid ZODB project using traversal

Let's use the starter scaffold.

1
$ $VENV/bin/pcreate --scaffold starter hello_world

Note

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

Running tests and coverage

Pyramid is fully tested, and we should always run tests on our 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 tests.py 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.

1
2
$ $VENV/bin/python easy_install nose
$ $VENV/bin/python easy_install coverage

Now let's edit our package configuration in setup.py (located in myproject/hello_world/) in order to run tests and coverage.

1
2
3
4
5
6
7
setup(name='hello_world',
      # ...omitted stuff...
      # add the next three lines before the closing ")"
      extras_require={
          'testing': ['nose', 'coverage'],
      }
)

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

1
2
$ cd hello_world
$ $VENV/bin/python ./setup.py develop

Now let's run our tests.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ $VENV/bin/nosetests hello_world/tests.py
.
Name                Stmts   Miss  Cover   Missing
-------------------------------------------------
hello_world             7      5    29%   7-11
hello_world.tests      12      0   100%
hello_world.views       3      0   100%
-------------------------------------------------
TOTAL                  22      5    77%
----------------------------------------------------------------------
Ran 1 test in 0.077s

OK

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.

Continue Reading

Read the entire article Pyramid Requests, Responses and Views at Safari Books Online.

Watch accompanying video

Pyramid in PyCharm in 5 minutes

Did you like this article? Please send me a Gratipay, as little as 25¢ per week!

Updates, suggestions and comments regarding this article may be sent to Steve Piercy, [email protected] or comment using Disqus.


Written by Steve Piercy in Pyramid on Tue, Jan 14, 2014.
Last modified: Thu, Jun 19, 2014
Tags: Pyramid, Python, web framework

Comments

comments powered by Disqus