Open In App

Python Pyramid – HTML Form Template

Last Updated : 02 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pyramid is an open-source web application development framework written in Python. It is a flexible and modular framework that is used to build web applications from single-page to large, and complex websites. In this article, you will learn how to create the HTML Form in Python Pyramid. And How Pyramid reads data from HTML Form.

Setting up the project

Installation

Command to install a Pyramid

pip3 install pyramid

Command to install the Jinja2 template engine

pip install pyramid_jinja2

Creating Necessary files

Creating the HTML Form

First, we will create an HTML script as ‘form.html’ which will be used for rendering the Template object.

HTML




<!DOCTYPE html>
<html>
<head>
<title>HTML Form</title>
</head>
<body>
 <div class="container">
   <form method="POST" action="http://localhost:5000/employees">
      <p>Employee Id: <input type="text" name="id" required/> </p>
      <p>Name: <input type="text" name="name" required/> </p>
      <p>Department: <input type="text" name="department" required/> </p>
      <p>Mobile No. <input type="number" name="mobile" required /></p>
      <p>Salary: <input type="text" name="salary" required/> </p>
      <p><input type="submit" value="Submit"> </p>
   </form>
 </div>
</body>
</html>
</html>


src.py

This code contain views to render the HTML Form and routes to define unique URL to each page.

  • ‘make_server’ function is used to create a simple WSGI (Web Server Gateway Interface) server, which can be used to serve web applications.
  • The Configurator class is used to configure a Pyramid application.
  • The Response class is used to create HTTP responses that can be sent back to the client.
  • The ‘view_config’ decorator is used to associate a view function with a specific route or URL pattern.
  • The purpose of index function is to render an HTML form (defined in ‘form.html’) to collect user input. In this case, it returns an empty dictionary {} to the template so that it doesn’t pass any specific data to be displayed on the form.
  • The add function is associated with a route named ’employees’ and uses the ‘templates/marklist.html’ template for rendering. It serves as the handler for the ’employees’ route. The purpose of this function is to process the data submitted by the user through a form, typically via a POST request to the ‘/employees’ URL.
  • Inside the app function, it extracts data from the request’s parameters (request.params) such as ‘id’, ‘name’, ‘department’, ‘mobile No.’ and ‘salary’. It creates a dictionary employee with this extracted data then appends the employee dictionary to a list named employees. This suggests that this function is likely used to collect and store employee data.
  • Finally, it returns a dictionary containing ’employees’ as a key and the employees list as its value. This data will be passed to the ‘marklist.html’ template for rendering, where it may be used to display a list of students or their marks.

Python3




from wsgiref.simple_server import make_server
from pyramid.response import Response
from pyramid.view import view_config
from pyramid.config import Configurator
 
 
employees = [
   {"id": 1, "name": "Sameer","department":"Sales",
            "mobile":6756453456, "salary": 75000},
   
   {"id": 2, "name": "Rahul","department":"Content",
        "mobile":8978675678, "salary": 50000},
   
   {"id": 3, "name": "Mahesh","department":"Marketting",
                "mobile":7989881110, "salary": 30000},
]
 
@view_config(route_name='index', renderer='form.html')
def index(request):
   return {}
 
@view_config(route_name='employees', renderer='marklist.html')
def add(request):
   employee={'id':request.params['id'], 'name':request.params['name'],
             'department':request.params['department'],
             'mobile':request.params['mobile'],
             'salary':int(request.params['salary'])}
   employees.append(employee)
   return {'employees':employees}
 
if __name__ == '__main__':
   with Configurator() as config:
      config.include('pyramid_jinja2')
      config.add_jinja2_renderer(".html")
      config.add_route('index', '/')
      config.add_route('employees','/employees')
      config.scan()
      app = config.make_wsgi_app()
   server = make_server('0.0.0.0', 5000, app)
   server.serve_forever()


marklist.html

This ‘marklist.html’ web template displays a table of employee data along with the computed result column.

HTML




<html>
<head>
<title>Employee List</title>
</head>
<body>
 
   <table border=1>
      <thead>
         <tr>
            <th>Employee ID</th>
            <th>Name</th>
            <th>Department</th>
            <th>Mobile No.</th>
            <th>Salary</th>
         </tr>
      </thead>
      <tbody>
         {% for Employee in employees %}
            <tr>
               <td>{{ Employee.id }}</td>
               <td>{{ Employee.name }}</td>
               <td>{{ Employee.department }}</td>
               <td>{{ Employee.mobile }}</td>
               <td>{{ Employee.salary }}</td>
            </tr>
         {% endfor %}
      </tbody>
   </table>
   <a href="/" style="text-decoration: none;"><button>Back</button></a>
 
</body>
</html>


Deployment

Run the ‘src.py’ file in the terminal. In your browser, visit http://localhost:5000/ to get the form as shown below −

Output:

form

Fill the employee data and click the submit button –

form-fill

After submitting the form, it invokes add view and display the list of employees when you click ‘Back’ button it will redirect you to form page-

Screenshot-from-2023-09-27-11-43-35



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads