All Courses

What is a Pyramid?

Ashish Katri

3 years ago

Pyramid
Table of Content
  • How to Install and configure pyramid.
  • What are the Core Concepts?
  •  features of the Pyramid framework
  • How do we generate URL?
  • What is View in pyramid framework?
  • What is Extensibility in pyramid framework?
Pyramid is a python framework which is general purpose, open source and used for web application framework. It allows developer to create web applications with ease.
Pyramid is backed by the enterprise knowledge Management System KARL (a George Soros project).
How to Install and configure pyramid.     
Pyramid is similar as Flask framework 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 your own 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 −
directory structure
And you can check the version installed in the system as shown below –
check the version installed
What are the Core Concepts?
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 −
·        One of the fastest known Python web frameworks.
·        Pyramid supports small and large projects (why rewrite when you outgrow your small framework).
·        Pyramid supports single file webapps like microframeworks.
·        Pyramid has built-in sessions.
·        Pyramid supports events similar to Plone/Zope.
·        Pyramid provides Transaction Management (if already have noticed that we have used Zope before).
Configuration
Configuration is the settings that influence the operation of an application. There are two ways by which we can 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)
How do we generate URL?
      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 −
·        It supports URL generation to allow changes to app that won’t break links.
·        It generates URLs to static resources that live either inside or outside the application.
·        It supports Routes and Traversal.
What is View in pyramid framework?
One of the primary jobs of pyramid framework is to find and invoke a view callable when a request reaches your application. View callable 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 as follows −
·        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).
What is Extensibility in pyramid framework?
         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 behavior without needing to modify its source code. The behavior 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).
Running a Hello, Pyramid Program
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 −
“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: −
Running the simplest program
Next, open http://localhost:8000/ in a browser, and you will see the Hello, Pyramid! Message as follows −
Output of Hello, Pyramid Message
The following is the explanation for above code −
Line no. 1-3
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.
Line no. 5-6
      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.
Line no. 8
if __name__ == ‘__main__’: Python is saying, “Start here when running from the command line”, rather than when this module is imported.
Line no. 9-11
            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.

Conclusion

        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.
Liked what you read? Then don’t break the spree. Visit our insideAIML blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our InsideAIML Youtube Page to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning. 
Keep Learning. Keep Growing.

Submit Review