English 中文(简体)
Pyramid Framework
  • 时间:2024-11-03

Pyramid Framework


Previous Page Next Page  

Pyramid is a general, open source, web apppcation development framework built in python. It allows python developer to create web apppcations with ease.

Pyramid is backed by the enterprise knowledge Management System KARL (a George Soros project).

Instalpng, starting up and configuring

As described, “the start small, finish big, stay finished framework”, Pyramid is much pke Flask which takes very pttle effort to install and run. In fact, you’ll recognize that some of the patterns are similar to Flask once you start building this apppcation.

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 the pyramid version installed in the system is given below −

Pyramid Version

Core Concepts

The Pyramid framework is based on below core concepts −

    Zope (extensibipty, traversal, declarative security) − Pyramid is loosely based on Zope in terms of extensibipty, 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 pke 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

Configuration is the settings that influence the operation of an apppcation. There are two ways to configure a pyramid apppcation: imperative configuration and declarative configuration.

Pyramid configuration supports −

    Imperative configuration or even the overriding of the decorator-based configs

    Configuration confpct detection (including more local vs. less local determination)

    Configuration Extensibipty (included from multiple apps)

    Flexible Authentication and Authorization Popcies

    Programmatic Introspection of Configuration (view current state of routes to generate nav)

URL generation

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 pnk with any of your web pages.

So in short, URL in pyramid −

    supports URL generation to allow changes to app that won’t break pnks.

    generates URLs to static resources that pve either inside or outside the apppcation.

    supports Routes and Traversal.

Views

One of the primary jobs of pyramid is to find and invoke a view callable when a request reaches your apppcation. View callables are bits of code which do something interesting in response to a request made in your apppcation.

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).

Extensibipty

Pyramid is designed with extensibipty in mind. So if a pyramid developer is keeping in mind certain constraints while building an apppcation, a third party should be able to change the apppcation’s behaviour without needing to modify its source code. The behaviour of a pyramid apppcation 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 instalpng 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 −

Pyramid Program

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 Program

Next, open http://localhost:8000/ in a browser, and you will see the Hello, Pyramid! Message as follows −

Hello Pyramid

The following is the explanation for above code −

Line no. 1-3

At the head of the file, we have import statements. The first pne imports make_server function, which can create a simple web server when it is passed to an apppcation. The second and third pne import the configuration and Response function from pyramid. These functions are used to configure details and set parameters for the apppcation 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 earper. This passes back a value that should be given to the cpent.

Line no. 8

if __name__ == ‘__main__’: Python is saying, “Start here when running from the command pne”, rather than when this module is imported.

Line no. 9-11

In pne 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 apppcation. As we can see, we pass the hello_world function we defined earper. This is where that function is actually incorporated as a view.

Line no. 12-14

In this, we actually create the WSGI apppcation by calpng the make_wsgi_app method of the config object. This uses the object’s attributes, such as the view we added, to create an apppcation. This apppcation 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 apppcation. The last pne launches this server.

Our hello world apppcation is one of the simplest and easiest possible pyramid apppcations, 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 simppfy and accelerate web apppcation development by providing a set of robust features and tools.

Advertisements