Open In App

Python Pyramid – HTML Form Template

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.






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




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>
<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:

Fill the employee data and click the submit button –

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-


Article Tags :