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.
- Jinja basic information.
- What is template based library?
- How to install jinja?
- Jinja delimiters.
- Example : Jinja hello world example.
- 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
- Create a new “jinjaTemplate” folder.
- Create the HTML page having template code
- Write the python code by using the template file and data
- 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.
- Python reporting libraries | Python reporting tools to generate interactive and beautiful reports
- pyspark dataframe | python spark dataframe with examples
- A to Z of Python exception handling | Python try catch with example
- File Handling Python With Examples | Python Input-Output Awesome Tips
- Modules in python tactics that can boost your python programming skills
- Python array | numpy for python | How to create numpy or np array in python
- Python Lambda Function With Examples | How To Create A New Anonymous function
- Python Useful Built-in Functions With Examples | How to create a new function
- python iteration | while loop | for loop with examples
- Python Data Types With Examples
- Python – installation indentation variable comments
- Python Programming Introduction
Pingback: Python reporting libraries | Python reporting tools to generate interactive and beautiful reports - Mycloudplace
Pingback: Datapane python reporting library for interactive reports and beautiful charts - Mycloudplace
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 😉
Thank you.
Thank you for sharing this article.
Thank you.
i am pleased you share this article on jinja, nice.
Thank you.
Artificial intelligence creates content for the site, you can also use it to write articles. 100% uniqueness. You can also write article on Jinja.
Thank you.