Hello Pyramid

A 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 web@stevepiercy.com or as stevepiercy in the #pyramid channel on irc.freenode.net.

This article will get you started with Pyramid, in which you will create a simple one-file “Hello World” web application. Before we start writing code, I’ll help you answer the question, “Is Pyramid the right framework for your project?”

Pyramid is a small, fast, open source web application development framework written in the Python language. It is one of many packages under the Pylons Project repositories on GitHub, which is a collection of related technologies.

Web application development with Pyramid is founded upon a “start small, finish big” principle. By keeping things small, focused, and fast in its framework, Pyramid allows developers to quickly create small web applications from the start. Then as the project evolves, Pyramid stays out of the way for developing large, complex applications that are reliable, high-performance, and resilient to changing demands. A few of these websites are listed under “Who’s using Pylons Project software” on the Pylons Project website.

Pyramid has common features that are expected in most web application frameworks, including mapping of URLs to code, templating, security, and serving static assets. Additional features are available through supported add-ons. Pyramid provides a set of features that, combined, are unique among frameworks. For a complete list of features in Pyramid, see the Pyramid Introduction.

One feature in particular, traversal, is worth mentioning. Traversal is best for tree-like or hierarchical data structures of resources, similar to a computer’s file system. (If your data is not tree-like, then a relational database would be the better option in Pyramid.) With traversal, like on a file system, when you move a directory or branch of resources to a new location, it will inherit the permissions of its parent directory. Mimicking this functionality with a relational database would be a challenging endeavor.

Even though Pyramid offers a great deal, it does not try to provide a solution to every scenario. Pyramid is not a blog, e-commerce or shopping cart application, or even a content management system. You can certainly write any of these specialized applications using Pyramid as a framework, thus making your own decisions about data storage, templating language, and other aspects of web application development. But you may prefer to let an application like Django, Plone, or WordPress make decisions for you. Such convenience implies that the framework makes decisions for you, which in turn implies ceding control and flexibility to the framework.

Because Pyramid is small and focused—it does not include the proverbial kitchen sink, such as an administrator interface, relational database, and templating language—the developer is free to choose the appropriate tool for the job. Pyramid developers may extend their applications with supported add-ons under the Pylons Project. For instance, Pyramid itself does not include persistent storage, but the developer may choose from any database, optionally using an object relational mapper (ORM) like SQLAlchemy, or using the Python object database ZODB.

The contributors to Pyramid follow principles that just make good sense when releasing software. All releases must pass automated tests: “If it ain’t tested, it’s broken.” All features are documented, both in the API and narrative documentation. Pyramid was one of the first web frameworks to fully support Python 3 in October 2011.

Now let’s start programming with Pyramid.

Requirements

As of this writing, Pyramid 1.4.5 is compatible with the latest versions of Python 2.6, 2.7, 3.2, and 3.3. For purposes of looking forward, this article will use Python 3.3 in its examples, leaving it to the developer to adapt examples to earlier versions of Python. Pyramid 1.5.x is under development and in alpha release, but we will use the “latest” release of Pyramid 1.4.5.

To start, we need to have a standard Python environment setup. That means install Python, make an isolated environment, and setup packaging tools. For brevity, I’ll provide only commands for Mac or Linux in this article. Details for Windows are available in Requirements for the Quick Tour of Pyramid.

If you are new to Python, virtual environments, or packaging tools, it is strongly recommended that you follow the aforementioned Requirements before continuing. Regardless of your experience, Pyramid uses conventions that make development much easier later on, and developers may benefit from following these conventions.

Install Python 3.3 or greater

Mac OS X users can download and run an installer. Download the latest standard Python 3.3+ release (not development release) from python.org. On that page, you must click the latest version, then scroll down to the “Downloads” section for your operating system.

Linux users can either use their package manager to install Python 3.3 or may build Python 3.3 from source.

Create a virtual environment

Install setuptools (Python packaging tools)

If wget complains with a certificate error, then run this command instead:

1

2

$wget--no-check-certificate

bootstrap/ez_setup.py-O-|$VENV/bin/python

Install Pyramid

1

$$VENV/bin/easy_install"pyramid==1.4.5"

That’s it for installing requirements. Now let’s write some code.

Hello World

Create a new text file in your favorite code editor, and save it in your myproject directory as hello.py. Copy, paste, and save the following Python code into the file:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

1from wsgiref.simple_server import make_server

2from pyramid.config import Configurator

3from pyramid.response import Response

4

5

6def hello_world(request):

7returnResponse('&lt;h1&gt;Hello World!&lt;/h1&gt;')

8

9

10if__name__=='__main__':

11config=Configurator()

12config.add_route('hello','/')

13config.add_view(hello_world,route_name='hello')

14app=config.make_wsgi_app()

15server=make_server('0.0.0.0',6543,app)

16server.serve_forever()

Now let’s run the application.

1

$$VENV/bin/python./hello.py

And let’s view our Pyramid web application in a web browser by visiting http://localhost:6543/. You should see the response “Hello World!”.

We can edit the response that Pyramid sends in our hello.py. Edit line 7 to your liking and save your changes.

1

returnResponse('&lt;h1&gt;I CAN HAZ PYRAMID? YES U CANZ!&lt;/h1&gt;')

If you reload the web browser, your changes do not appear. This is because the server must first reload changes to your development files before it can serve them. One way to do so is to stop the server in your shell with a CTRL-C keyboard shortcut (or whatever you use to interrupt a process), then reissue the command to run your application and reload your changes.

1

$$VENV/bin/python./hello.py

Although single-file web applications are useful to learn the essential steps to start using a framework, most web applications have many files. We’ll learn how to create a typical Pyramid project in our next post.

Conclusion

Pyramid is a web application framework that let’s you start projects quickly. And because it is focused on the essentials, Pyramid will not get in your way as you create larger, more complex projects. Pyramid is thoroughly tested and documented, and was one of the first frameworks to run on Python 3.

In this video, see how to create a simple single-file “Hello World” application using the Pyramid web application framework in PyCharm.

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.