Pyramid Inside – p* scripts

by Ruslan Spivak on February 19, 2012

Pyramid 1.3 introduced different p* scripts that replaced the paster commands with Pyramid-specific analogues. Here is a summary table that lists all currently available scripts with their brief descriptions.

pcreate create a Pyramid application using one of the pre-defined scaffolds
pserve run the application
proutes display all application routes
pviews display matching views for a given URL
prequest send a request to the application without starting a server
pshell play in the interactive shell with the app
ptweens display “tweens”

The best way to get to know what those commands do is to try them out. But before we see our p* scripts in action make sure to install the latest Pyramid 1.3 and activate your virtual environment. The steps to do that might look like this:

$ virtualenv --no-site-packages test
$ cd test
$ source bin/activate
$ pip install pyramid

OK, buckle up and let’s get it started.

Create application

Let’s create a simple project called myproject with pcreate command

$ pcreate -s starter myproject
...
Welcome to Pyramid.  Sorry for the convenience.

Run it

In order to run our application we need to install it for development first.

$ cd myproject
$ python setup.py develop

And now we can run it with pserve script

$ pserve development.ini
Starting server in PID 17410.
serving on http://0.0.0.0:6543

Display all application routes

$ proutes development.ini | grep -v debug
Name        Pattern             View
----        -------             ----
__static/   /static/*subpath    <function <pyramid.static  ...>
home        /                   <function my_view at 0x996b09c>

Display matching views for a given URL

$ pviews development.ini /

URL = /

    context: <pyramid.traversal.DefaultRootFactory instance at ...>
    view name: 

    Route:
    ------
    route name: home
    route pattern: /
    route path: /
    subpath: 

        View:
        -----
        myproject.views.my_view

Send a request to our application without starting a server

Let’s make a GET request and show response headers

$ prequest -d development.ini /
200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 3390
<!DOCTYPE html PUBLIC ...>
...

And now a POST request to a non-existent URL

$ echo 'body text' | prequest -mPOST -d development.ini /dummy
404 Not Found
Content-Type: text/html; charset=UTF-8
Content-Length: 158
<html>
 <head>
  <title>404 Not Found</title>
 </head>
 <body>
  <h1>404 Not Found</h1>
  The resource could not be found.<br/><br/>
/dummy

 </body>
</html>

Play in the interactive shell

$ pshell development.ini
Python 2.7.1 (r271:86832, Apr 12 2011, 16:16:18)
[GCC 4.6.0 20110331 (Red Hat 4.6.0-2)] on linux2
Type "help" for more information.

Environment:
  app          The WSGI application.
  registry     Active Pyramid registry.
  request      Active request object.
  root         Root of the default resource tree.
  root_factory Default root factory used to create `root`.

>>> from myproject.views import my_view
>>> from pyramid.request import Request
>>> r = Request.blank('/')
>>> my_view(r)
{'project': 'myproject'}

If we install IPython it will be used instead of the default Python shell.

Display “tweens” (middleware)

$ ptweens development.ini
...

Implicit Tween Chain

Position    Name
--------    ----
-           INGRESS
0           pyramid_debugtoolbar.toolbar_tween_factory
1           pyramid.tweens.excview_tween_factory
-           MAIN

If you’ve read this far and want to dig deeper into those commands check out an excellent official documentation page: Command-Line Pyramid

If you enjoyed this post why not subscribe via email or my RSS feed and get the latest updates immediately. You can also follow me on GitHub or Twitter.

{ 2 comments… read them below or add one }

Tshepang Lekhonkhobe February 20, 2012 at 7:17 AM

Excellent walk-through. Thanks much.

Reply

Ruslan Spivak February 20, 2012 at 10:54 AM

You’re welcome

Reply

Speak your mind

Previous post:

Next post: