Open In App

Pyramid Web Framework

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Pyramid is a general, open-source Python framework used to develop web applications. Pyramid is known as the fastest Python web framework. It is developed by the enterprise knowledge Management System KARL (a George Soros project). It supports small and large projects. It also supports single-file webpages like microframeworks. Pyramid's framework is more explicit than Flask's framework and is also easier to learn because it has less base code to implement a simple web-Application. Pyramid is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine. There is a quote famous for the pyramid framework "Start Small Finish Big".

Required Modules 

pip install pyramid
pip install pyramid_jinja2

Project Structure

Project Structure

Pyramid Web Framework Examples

Basic Hello World Program in Pyramid Web Framework

In this example, we are creating a basic hello world application using Pyramid web Framework.

views.py

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


def Hello_World(request):
    return Response('Hello World')


if __name__ == '__main__':
    with Configurator() as Config:
        Config.add_route('helloworld', '/')
        Config.add_view(Hello_World, route_name='helloworld')
        app = Config.make_wsgi_app()
    server = make_server('0.0.0.0', 5000, app)
    server.serve_forever()

Output : 

Hello world program in Pyramid

Making a GET request in Pyramid Web Framework

Before doing this example one thing that you need to install is pyramid_jinja2 this is a Python library or template engine used to build templated for websites we can say it is used to create HTML, XML or other markup formats that are returned to the user via an HTTP request.

In this example we are creating a basic web application for student registration using the GET method however we know the get method is not suitable for registration and login so in the next example we will be doing the same by using the POST method. Firstly we need to create a Python file I have created it by the name studentregistration.py you can give any name. Here is the code python code for student registration.

views.py 

Python
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config


@view_config(route_name='home', 
             renderer='templates/getindex.jinja2')
def home(request):
    return {}


@view_config(route_name='studentregistration', 
             request_method='GET', 
             renderer='templates/getstudentregistration.jinja2')
def register(request):
    name = request.GET.get('name')
    email = request.GET.get('email')
    password = request.GET.get('password')
    return {}


@view_config(route_name='successful')
def successful(request):
    return Response("Successfully Submitted")


if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    config = Configurator()
    config.include('pyramid_jinja2')
    config.add_route('home', '/')
    config.add_route('studentregistration', '/register')
    config.add_route('successful', '/successful')
    config.scan()
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()

In this code, we have created a different function that is used to render the jinja2 web pages and we have mapped those functions with the route. After writing this code in the python file we need to create two jinja2 files.

getindex.jinja2

HTML
<!DOCTYPE html>
<html>
 <head>
   <title>User Registration</title>
 </head>
 <body>
   <h1>Welcome to User Registration</h1>
   <p>Please fill in the registration form:</p>
   <form action="/register" method="get">
     <button type="submit">Register</button>
   </form>
 </body>
</html>

getstudentregistration.jinja2 

HTML
<!DOCTYPE html>
<html>
<head>
  <title>User Registration</title>
</head>
<body>
  <h1>User Registration</h1>
  <form action="/successful" method="get">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username"><br>
    <label for="email">Email:</label>
    <input type="email" name="email" id="email"><br>
    <label for="password">Password:</label>
    <input type="password" name="password" id="password"><br>
    <button type="submit">Submit</button>
  </form>
</body>
</html>

Output : 

Welcome Page
Welcome Page 
Registering User
Registering User
Redirected

Making a POST Request in Pyramid Web Framework

In this example we are making a post request using Pyramid web framework.

views.py

Python
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config


@view_config(route_name='home', 
             renderer='templates/postindex.jinja2')
def home(request):
    return {}


@view_config(route_name='register',
             request_method='POST',
             renderer='templates/poststudentregistration.jinja2')
def register(request):
    name = request.POST.get('name')
    email = request.POST.get('email')
    password = request.POST.get('password')
    return {}


@view_config(route_name='successful')
def successful(request):
    return Response("Successfully Submitted")


if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    config = Configurator()
    config.include('pyramid_jinja2')
    config.add_route('home', '/')
    config.add_route('register', '/register')
    config.add_route('successful', '/successful')
    config.scan()
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()

postindex.jinja2

HTML
<!DOCTYPE html>
<html>
<head>
  <title>User Registration</title>
</head>
<body>
  <h1>Welcome to User Registration</h1>
  <p>Please fill in the registration form:</p>
  <form action="/register" method="post">
    <button type="submit">Register</button>
  </form>
</body>
</html>

poststudentregistration.jinja2 

HTML
<!DOCTYPE html>
<html>
<head>
 <title>User Registration</title>
</head>
<body>
 <h1>User Registration</h1>
 <form action="/successful" method="post">
   <label for="username">Username:</label>
   <input type="text" name="username" id="username"><br>
   <label for="email">Email:</label>
   <input type="email" name="email" id="email"><br>
   <label for="password">Password:</label>
   <input type="password" name="password" id="password"><br>
   <button type="submit">Submit</button>
 </form>
</body>
</html>

Output : 

Post Request Success
Post Request Success

JSON data rendering in Pyramid Web Framework

In this example, we are rendering JSON data with Pyramid web Framework.

views.py

Python
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
import json


@view_config(route_name='home', 
             renderer='templates/jsondatarendering.jinja2')
def home(request):
    data = {
        'name': 'Vikash Mishra',
        'field': 'Computer Science',
        'graduation': 'btech',
        'college': 'ITM University Gwalior',
        'skills': ['C', 'C++', 'Python', 'Javascript']

    }
    return {'data': data}


if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    config = Configurator()
    config.include('pyramid_jinja2')
    config.add_route('home', '/')
    config.scan()
    app = config.make_wsgi_app()
    server = make_server('localhost', 8000, app)
    server.serve_forever()

In the below code, we create a dictionary in the home function and we are returning it to the jsondatarendering.jinja2 inside the templates folder webpage the JSON data containing field or keys name, field, and graduation will be shown on the webpage.

jsondatarendering.jinja2 

HTML
<!DOCTYPE html>
<html>
 <head>
   <title>portfolio!</title>
   <style>
       body{
         background-color: #EDCABE;
         margin:10px;
         padding: 10px;
       }
       h1,.graduation,.college,.field,.name{
         color: #0F4C81;
         font-weight: bold;
       }
       
   </style>
 </head>
 <body>
   <h1 class="heading">Profile</h1>
   <h3>I am <font class="name">{{ data.name }}</font>!</h1>
   <p>I am currently pursuing <font class="graduation">
     {{data.graduation}}</font>
     with <font class="field">{{ data.field }}
     </font> in <font class="college">{{data.college}}</font>.</p>
   <h4>I have technical skills :</h4>
   <ul>
     {% for skill in data.skills %}
     <li>{{ skill }}</li>
     {% endfor %}
   </ul>
   <hr>
 </body>
</html>

Output :

Json Render in Pyramid
Json Render in Pyramid

Difference between Pyramid, Flask and Django 

        

                 Pyramid                         

               Flask                 

              Django          

     Design      

The pyramid web framework allows 

developers to choose components.

It is a micro web framework 

that provides bare essentials.

It provides everything needed 

to build a web app.

       Size

 Pyramid is very small compared 

to the other two web frameworks.

Flask is bigger than a pyramid

The size is bigger than a flask and 

pyramid.

  Complexity

It is very less complex due to 

its very small size.

It is a bit complex but not 

more than Django.

Django is a very complex web

framework.

  Templating

It does not have a built-in template 

engine but it can use third-party 

template.

Flask also does have built-

in a templating engine.

Django has a built-in template

engine.

     ORM

The pyramid does not have a built-in 

object-relational mapping(ORM).

Flask also does not have a 

built-in ORM.

Django comes with its own 

ORM.

  Community

Smaller.

Smaller.

Large.


Article Tags :
Practice Tags :

Similar Reads