Web Application Programming in Pyramid Framework

| |

Home » Web Application Programming in Pyramid Framework
Spread the love
  •  
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  

Pyramid is a Python-based web framework. It was designed to simplify the creation of web applications. Many features make it easier to write complex software. The Pyramid framework follows the MVC software pattern. It basically separates the functions of each application to make it easier to design and allows for individual modification. In this tutorial, we’re going to learn web application programming in Pyramid framework using Python.

With Pyramid, you’ll be able to create whatever comes to mind. Whether it’s a small application or a large project, the servers will rock! Pyramid will get you off to a great start because it is so easy to learn and use. It will allow you to grow as both an individual and representative of your company by never holding you back from getting work done right away.

This quick tutorial will walk you through the steps of creating a single file application, forms, and database integration.

Why choose Pyramid framework?

Many tools are available to help you develop your website. Why would someone choose a Pyramid over others? What makes Pyramid so special?

Well, first of all, Pyramid’s official project documentation is excellent and easy to navigate. Even if you don’t know everything, it is easy to get started. Pyramid will help you when you are ready to learn more.

Out of the box, Pyramid comes with the core tools required for almost all web applications. These include security, mapping URLs to code, and serving static assets (files such as JavaScript and CSS). Other tools include database integration, templating, and many other features.

These are some of the things that makes Pyramid a suitable choice for beginners:

  • As a beginner, single-file module With little to no complexity in the first hour
  • Use the following Convenient scaffoldTo generate a sample project using your subsystems combination
  • You can choose from a range of Security solutions, templating, and database Pyramids add-on systems offer more convenience and quality.
  • You can tap into a wide range of High-quality documentation Pyramid is available for evaluation, testing, and advanced development.

This is why web application programming in Pyramid framework using Python is easy and best.

Intall the Dependencies

You will need to have the prerequisite packages installed before you can start. Pyramid is a Python framework. You will need to have the Python version that is used in your application.

Installing a Python interpreter in Windows will be necessary to install to use the Pyramid framework.  if it doesn’t exist on your system, go to python.org and download the Python 3.x series executable.

Additionally, you’ll need to make sure you have pip installed on your system. If pip isn’t already installed, you should first try to get it from the standard library:

py -m ensurepip --default-pip

If this is still not enough to allow you to run Python -m pip, do the following steps:

  • Run python get-pip.py. This will upgrade or install pip. If setuptools or wheel are not already installed, it will also install them.

You can now install Pyramid after you have installed Python and satisfied the requirements for installing packages.

Use pip to install Pyramid. This will also install Pyramid direct dependencies:

pip3 install pyramid

Alternatively, you can also install Pyramid inside a virtual environment.

When you install Pyramid, various libraries such as WebOb, PasteDeploy, and others are installed.

Building a single-file application: Imperative Configuration

After the necessary installation of the Pyramid framework, the simplest program to verify that everything is working fine is to run a hello world program. The code can be written as simple as this. The following example can be saved in a file called “config.py”

# config.py# Import functions to run the web app
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

# This function receives a request from the server, and returns the response
def config(request):
    return Response('<h1>Hello world!</h1>')

#This function will create a localhost server and define the hostname. #You can add routes to your application here and also a view to be #displayed to the user
if __name__ == '__main__':
    config = Configurator()
    config.add_view(hello_world)
    app = config.make_wsgi_app()
# 8080 is the port number through which requests of our app will be served
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()

This code can be executed by a Python interpreter that has the Pyramid software installed. An HTTP server will then be started on TCP port 8080.

To do that you’ll have to run:

\python config.py  #python followed by the name of your file

If your application is running on your local system and you have copied the code from above, using http://localhost:8080/ in a browser you can see your return response.

Note: When this command will be executed nothing will appear on the console. But when port 8080 will be visited, it will display the text aka your return response, and only then the terminal will show any results. Also, the server will not log any changes you make to the app automatically. To see your changes take place, you will need to shut down (you can press Ctrll+C for that) the server and restart it.

Now, the type of configuration in which we have created above is something called an imperative configuration. Imperative configuration is simply a configuration that is done by only using Python statements, one after another. It is almost never used in production, it’s simply a way for beginners to test out and experiment with things

Multi-file application : Declarative Configuration

Declarative Configuration is the more professionally used way of doing things. It separates the file that contains the implementation objects separate from the main config file. It is simple to extend it, test it, and then reuse it. You can also instruct the framework on how to ignore certain decorators. To write your configuration in an imperative style, you can skip decorators completely as well. This allows you to modularize your code and will allow you to add new views more easily.

To demonstrate this in action here is an example. In this file, we will remove the existing views and place them in a new file (views.py).

# views.py
# Import functions to run the web app
from pyramid.compat import escape
from pyramid.response import Response
from pyramid.view import view_config

# View_config functions tells Pyramid which view will be defined by the function following the function's name.
@view_config(route_name='new_site')
def home_page(request):
    header = '<h2 style="text-align: center;">Home</h2>'
    body = '<br><br><p style="text-align: center; font-family: poppins; color: black;">Hey, that code worked!!.</p>'
    body += '<p style="text-align: center; font-family: poppins;"> This is your multi view site.</p>'
    footer = '<p style="text-align: center; font-family: poppins;">Go on and make more.</p>'

# It is simply telling the view to navigate to that route, and run whatever code is in that view
    return Response(header + body + footer)

And to make this all work, We’ll need to edit the config.py file.

# config.py
# Import functions to run the web app

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

def main():
    with Configurator() as config:
        # The first parameter of add_route function is the name of the route and the second parameter defines the page location
        config.add_route('new_site', '/')

        # The scan function scans our project directory looking for a file called views.py. It then connects the routes that we have provided.
        config.scan('all_views')

        application = config.make_wsgi_app()

    server = make_server('0.0.0.0', 8080, application)
    server.serve_forever()

main()

You can see that now, both files are now very tidy and serve a single purpose.

Conclusion

We have just skimmed the surface of what a Pyramid can do. Hopefully, by this point you can see that it has a robust structure made to suit Python’s conventions and is flexible enough to accommodate any type of web template starting from extremely simple layout designs all the way up to complete, complex layouts compatible with multiple levels of interaction and functionality. I hope this tutorial helped you to learn web application programming in Pyramid framework using Python.

Here are some useful tutorials that you can read:

  •  
    1
    Share
  •  
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
Previous

Create API in Django Rest Framework Viewset

Leave a Comment