Jinja a template based python library for beautiful and interactive reports

Jinja is a python library used for reporting purposes. First, we design the report on the template file. While designing we use placeholders and when we actually bind this template with data the placeholder value replace with the original value.

  1. Jinja basic information.
  2. What is template based library?
  3. How to install jinja?
  4. Jinja delimiters.
  5. Example :  Jinja hello world example.
  6. Example: Generating tabular reports using python library jinja2.

1. Jinja basic information

About Jinja is a templating language. Jinja templates are just HTML files that usually reside inside the /templates directory of a Flask project.
install pip install Jinja2
import from jinja2 import Template
Url https://palletsprojects.com/p/jinja/
PyPI Releases https://pypi.org/project/Jinja2/
Is Open Source YES
Source Code https://github.com/pallets/jinja/
Latest Version Version 3.1.2

Jump to the top of the page↵

2. What is template based library?

In template based library, we use fixed layout to generate our reports. In template based design we create HTML page while few data is filled programmatically. We can also use loops to manipulate the output. In this we have pre-written markup template and pre-defined delimiters where data is inserted.

Jump to the top of the page↵

3. How to install Jinja?

First ensure pip is installed on the system, if pip is not installed then there are 2 ways to get it installed on the system

Once pip is setup on system then by using pip install  the Jinja2 library.

pip install Jinja2

Jump to the top of the page↵

4. Jinja delimiters

{% %} Used for statements
{{ }} Used for expressions to print to the template output
{# #} Used for comments which are not included in the template output
# ## Used for line statements

Jump to the top of the page↵

5. Example :  Jinja hello world example

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 24 2022
website : https://www.mycloudplace.com
@author: Prem Shanker Verma
"""

#import jinja2 library
import jinja2

if __name__ == "__main__":
    #Create jinja2 environment
    env = jinja2.Environment()
    #Create hello world template
    templ = env.from_string("Hello, {{ value }}")
    output = templ.render(value="World")
    print(output)
    #Hello, World

Jump to the top of the page↵

6. Example: Generating tabular reports using python library jinja2

In this example we will generate the list of blockbuster Bollywood movies

Below are the steps to generate tabular reports using jinja2

  1. Create a new “jinjaTemplate” folder.
  2. Create the HTML page having template code
  3. Write the python code by using the template file and data
  4. Create a new output HTML file with filled data
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 10 15:49:49 2022
website : http://mycloudplace.com
@author: Prem Shanker Verma
"""
from jinja2 import Environment, FileSystemLoader

if __name__ == "__main__":
    Movies = [
        {'Movie':'Baahubali 2 The Conclusion', 'Budget':'250', 'Verdict':'All Time Blockbuster'},
        {'Movie':'KGF Chapter 2', 'Budget':'100', 'Verdict':'All Time Blockbuster'},
        {'Movie':'Dangal', 'Budget':'70', 'Verdict':'All Time Blockbuster'},
        {'Movie':'Sanju', 'Budget':'100', 'Verdict':'All Time Blockbuster'},
        {'Movie':'PK', 'Budget':'85', 'Verdict':'All Time Blockbuster'},
        {'Movie':'Tiger Zinda Hai', 'Budget':'210 ', 'Verdict':' Blockbuster'},
        {'Movie':'Bajrangi Bhaijaan', 'Budget':' 90 ', 'Verdict':'All Time Blockbuster'},
        {'Movie':'War', 'Budget':'150', 'Verdict':'Blockbuster'},
        {'Movie':'Padmaavat ', 'Budget':'215', 'Verdict':' Blockbuster'},
        {'Movie':'Sultan', 'Budget':'80', 'Verdict':' Blockbuster'},
        {'Movie':'Kabir Singh', 'Budget':'55', 'Verdict':' All Time Blockbuster'},
        {'Movie':'Tanhaji: The Unsung Warrior', 'Budget':'150', 'Verdict':' Blockbuster'},
        {'Movie':'Dhoom 3', 'Budget':' 175 ', 'Verdict':'All Time Blockbuster '},
        {'Movie':'The Kashmir Files', 'Budget':' 20', 'Verdict':'All Time Blockbuster'},
        {'Movie':'URI The Surgical Strike', 'Budget':'70 ', 'Verdict':'All Time Blockbuster'}
        ]
    
    file_loader = FileSystemLoader('jinjaTemplate')
    env = Environment(loader=file_loader)
    template = env.get_template('movieList.html')
    output = template.render(Movies=Movies)
    print(output)
    
    with open("listofmovies.html", 'w') as htmlOutput:
        htmlOutput.write(output)

Template code:


 OUTPUT:

Movie Budget Verdict
Baahubali 2 The Conclusion 250 All Time Blockbuster
KGF Chapter 2 100 All Time Blockbuster
Dangal 70 All Time Blockbuster
Sanju 100 All Time Blockbuster
PK 85 All Time Blockbuster
Tiger Zinda Hai 210 Blockbuster
Bajrangi Bhaijaan 90 All Time Blockbuster
War 150 Blockbuster
Padmaavat 215 Blockbuster
Sultan 80 Blockbuster
Kabir Singh 55 All Time Blockbuster
Tanhaji: The Unsung Warrior 150 Blockbuster
Dhoom 3 175 All Time Blockbuster
The Kashmir Files 20 All Time Blockbuster
URI The Surgical Strike 70 All Time Blockbuster

For all python reporting library please visit my previous post.

10 thoughts on “Jinja a template based python library for beautiful and interactive reports”

  1. Pingback: Python reporting libraries | Python reporting tools to generate interactive and beautiful reports - Mycloudplace

  2. Pingback: Datapane python reporting library for interactive reports and beautiful charts - Mycloudplace

  3. I must thank you for the efforts youve put in writing this site. I really hope to check out the same high-grade content from you in the future as well. In fact, your creative writing abilities has inspired me to get my very own site now 😉

Leave a Comment

Your email address will not be published. Required fields are marked *