Pyramid is a general, open source, web application development framework built in python. It allows python developer to create web applications with ease.Pyramid is backed by the enterprise knowledge Management System KARL (a George Soros project).
As described, “the start small, finish big, stay finished framework”, Pyramid is much like Flask which takes very little effort to install and run. In fact, you’ll recognize that some of the patterns are similar to Flask once you start building this application.
Following are the steps to create pyramid framework environment −
First, create a project directory. Here, we have created a directory named pyramidProject (you can choose any name you want).
Next, create a virtual environment where you will install all the project specific dependencies. Here, we created a virtual environment folder named pyramidEnv where Pyramid is installed.
Then, go to the directory, pyramidEnv and install the pyramid with pip install pyramid.
Once everything is done as mentioned above, your directory structure will be as shown below −
And the pyramid version installed in the system is given below −
The Pyramid framework is based on below core concepts −
Zope (extensibility, traversal, declarative security) − Pyramid is loosely based on Zope in terms of extensibility, the concept of traversal and the declarative security.
Pylons (URL dispatch, non-opinionated view of persistence, templating, etc.) − Another area from where pyramid draws its concept is the pylons project. Pylons have that concept of routes, that calls the URL dispatch inside the pyramid framework and they also have the non-opinionated view of persistence layer or templating.
Django (View, level of documentation) − Pyramid also gets hint from Django. The way we take our view, routed our URL and the level of documentation is very Django way.
The following are the features of the Pyramid framework −
It is the fastest known Python web framework.
It supports small and large projects (why rewrite when you outgrow your small framework).
It supports single file webapps like microframeworks.
It has built-in sessions.
It supports events similar to Plone/Zope.
It provides Transaction Management (if already have noticed that we have used Zope before).
Configuration is the settings that influence the operation of an application. There are two ways to configure a pyramid application: imperative configuration and declarative configuration.
Pyramid configuration supports −
Imperative configuration or even the overriding of the decorator-based configs
Configuration conflict detection (including more local vs. less local determination)
Configuration Extensibility (included from multiple apps)
Flexible Authentication and Authorization Policies
Programmatic Introspection of Configuration (view current state of routes to generate nav)
In pyramid, we can generate URLs for routes, resources and static assets. It is easy and flexible to work with URL generation APIs. By generating URLs through pyramid’s various APIs, users can change the configuration arbitrarily without much worry of breaking a link with any of your web pages.
So in short, URL in pyramid −
supports URL generation to allow changes to app that won’t break links.
generates URLs to static resources that live either inside or outside the application.
supports Routes and Traversal.
One of the primary jobs of pyramid is to find and invoke a view callable when a request reaches your application. View callables are bits of code which do something interesting in response to a request made in your application.
When you map your views onto your URL dispatch or python code, there can be any kind of call. Views can be a function declaration or an instance, it can be used as a view in the pyramid.
Some important points about Views are given below −
Views are generated from any callable.
Renderer based views can simply return dictionaries (not required to return a webby style object).
Support multiple views per route (GET vs. POST vs. HTTP Header check, etc.).
View response adapters (when you want to specify how view returns values should be handled vs. response objects).
Pyramid is designed with extensibility in mind. So if a pyramid developer is keeping in mind certain constraints while building an application, a third party should be able to change the application’s behaviour without needing to modify its source code. The behaviour of a pyramid application that obeys certain constraints can be overridden or extended without any modification. It is designed for flexible deployments to multiple environments (No Singletons). Pyramid has “Tweens” middleware support (WSGI middle ware, but runs in the context of Pyramid itself).
The simplest program we can think after installing pyramid framework to check if everything is working fine, is to run a simple “Hello, World” or “Hello, Pyramid” program.
Below is my pyramid “Hello, Pyramid” program on 8000 port number −
Above simple example is easy to run. Save this as app.py (In this, we have given the name pyramid_helloW.py).
Running the simplest program: −
Next, open http://localhost:8000/ in a browser, and you will see the Hello, Pyramid! Message as follows −
The following is the explanation for above code −
At the head of the file, we have import statements. The first line imports make_server function, which can create a simple web server when it is passed to an application. The second and third line import the configuration and Response function from pyramid. These functions are used to configure details and set parameters for the application and respond to requests, respectively.
Now we have a function definition called hello_world. Implement view code that generates the response. A function that fulfils the requirement of a view is responsible for rendering the text that will be passed back to the requesting entity. In the above case, the function, when called, uses the Response function we imported earlier. This passes back a value that should be given to the client.
if __name__ == ‘__main__’: Python is saying, “Start here when running from the command line”, rather than when this module is imported.
In line no. 9, we create a variable called config out of the object created by the configurator function that we imported at the top of the program. Line 10 and 11 call the add_route and add_view method of this object. This method is used to define a view that can be used by the application. As we can see, we pass the hello_world function we defined earlier. This is where that function is actually incorporated as a view.
In this, we actually create the WSGI application by calling the make_wsgi_app method of the config object. This uses the object’s attributes, such as the view we added, to create an application. This application is then passed to the make_server function we imported in order to create an object that can launch a web server to serve our application. The last line launches this server.
Our hello world application is one of the simplest and easiest possible pyramid applications, configured “imperatively”. It is imperative because the full power of Python is available to us as we perform configuration tasks.
To summarize, Pyramid is an open source python web framework with a large and active community. This large community contributes towards making the python web framework popular and relevant. Pyramid web framework simplify and accelerate web application development by providing a set of robust features and tools.