This article was originally published on Safari Books Online and can be read in its entirety at Hello Pyramid. An abridged version follows.
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, 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, combinedm are unique amongst frameworks. For a complete list of features in Pyramid, see 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. Mimicing 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, ecommerce or shopping cart application, or even a content management system. You can certainly write any of these specialized applications using Pyramid as a framework, 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 use an object relational mapper (ORM) like SQLAlchemy, or use 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.
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.
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.
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.