The Wayback Machine - https://web.archive.org/web/20240930083409/https://www.geeksforgeeks.org/flask-rendering-templates/
Open In App

Flask Rendering Templates

Last Updated : 21 Jun, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Flask is a backend web framework based on the Python programming language. It basically allows the creation of web applications in a Pythonic syntax and concepts. With Flask, we can use Python libraries and tools in our web applications. Using Flask we can set up a web server to load up some basic HTML templates along with Jinja2 templating syntax. In this article, we will see how we can render the HTML templates in Flask.

Rendering a Template in a Flask Application

Setting up Flask is quite easy. We can use a virtual environment to create an isolated environment for our project and then install the Python packages in that environment. After that, we set up the environment variables for running Flask on the local machine. This tutorial assumes that you have a Python environment configured, if not please follow through for setting up Python and pip on your system. Once you are done, you are ready to develop Flask applications.

Setting up the Virtual Environment

To set up a virtual environment, we can make use of the Python Package Manager “pip” to install the “virtualenv” package.

pip install virtualenv

This will install the package “virtualenv” on your machine. The pip command can be different on the version of your Python installed so please do look at the different syntax of the pip for your version here.

Creating Virtual Environment: 

After the package has been installed we need to create a virtual environment in our project folder. So you can locate an empty folder where you want to create the Flask application or create an empty folder in your desired path. To create the environment we simply use the following command.

virtualenv venv

Here, venv is the name of the environment, after this command has been executed, you will see a folder named “venv” in the current folder. The name “venv” can be anything(“env”) you like but it is standard to reference a virtual environment at a production level. 

Activating Virtual Environment: 

Now after the virtual env has been set up and created, we can activate it by using the commands in CMD\Powershell or Terminal:

Note: You need to be in the same folder as the “venv” folder.

For Windows:

venv\Scripts\activate

For Linux/macOS:

source venv/bin/activate

This should activate the virtualenv with “(venv)” before the command prompt. 

Screenshot of the entire virtualenv setup

As we can see we have successfully created the virtualenv in Windows Operating System, in Linux/macOS the process is quite similar. The (venv) is indicating the current instance of the terminal/CMD is in a virtual environment, anything installed in the current instance of a terminal using pip will be stored in the venv folder without affecting the entire system. 

Installing Flask: 

After the virtual environment has been set up, we can simply install Flask with the following command:

pip install flask

This should install the actual Flask Python package in the virtual environment.

Adding Flask to Environment Variables: We need to create an app for Flask to set it as the starting point of our application. We can achieve this by creating a file called “server.py” You can call this anything you like, but keep it consistent with other Flask projects you create. Inside the server.py paste the following code:

Python




from flask import Flask
 
app = Flask(__name__)
 
if __name__ == "__main__":
    app.run()


This is the code for actually running and creating the Flask app. This is so-called the entry point of a Flask web server. As you can see we are importing the Flask module and instantiating with the current file name in “Flask(__name__)”. Hence after the check, we are running a function called run().

After this, we need to set the file as the Flask app to the environment variable.

For Windows:

set FLASK_APP=server

For Linux/macOS:

export FLASK_APP=server

Now, this will set up the Flask starting point to that file we created, so once we start the server the Flask server will find the way to the file “server.py”

To run the server, enter the command :

flask run

This will run the server and how smartly it detected the server.py file as our actual flask app. If you go to the URL “http://localhost:5000”, you would see nothing than a Not Found message this is because we have not configured our web server to serve anything just yet. You can press CTRL + C to stop the server

Flask set up for webserver

Creating Templates in a Flask Application

Now, we can move on to the goal of this article i.e. to render the template. To do that we need to first create the templates, you can use any HTML template but for simplicity, I am going with a basic HTML template. Before that, create a folder called “templates” in the current folder. Inside this “templates” folder, all of the templates will be residing. Now let us create a basic HTML template:  This template must have some Jinja blocks that can be optionally replaced later. We start with a single block called the body.

templates\index.html

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>FlaskTest</title>
</head>
 
<body>
    <h2>Welcome To GFG</h2>
    <h4>Flask: Rendering Templates</h4>
 <!-- this section can be replaced by a child document -->  
{% block body %}
 
 
<p>This is a Flask application.</p>
 
 
{% endblock %}
 
 
</body>
 
</html>


Adding Routes and Rendering Templates

A route is a mapping of a URL with a function or any other piece of code to be rendered on the webserver. In the Flask, we use the function decorate @app.route to indicate that the function is bound with the URL provided in the parameter of the route function.

Creating the basic route: In this case, we are binding the URL “/” which is the base URL for the server with the function “index”, you can call it whatever you like but it makes more sense to call it index here. The function simply returns something here it calls the function render_template. The render_template finds the app by default in the templates folder. So, we just need to provide the name of the template instead of the entire path to the template. The index function renders a template index.html and hence we see the result in the browser.

Now, we need a way to actually link the template with a specific route or URL. This means whenever the user goes to a specific URL then a specific template should be rendered or generated. Now, we need to change the “server.py” with the following:

Python




from flask import Flask, render_template
 
app = Flask(__name__)
 
 
@app.route("/")
def index():
    return render_template("index.html")
 
  if __name__ == "__main__":
    app.run()


Output:

We have imported the render_template function from the Flask module and added a route.

render the basic template

Templating With Jinja2 in Flask

Now, we’ll create a new route for demonstrating the usage of the Jinja template. We need to add the route, so just add one more chunk of the code to the “server.py file”

Python




@app.route("/<name>")
def welcome(name):
    return render_template("welcome.html", name=name)


Now, this might look pretty easy to understand, we are simply creating a route “/<name>” which will be bound to the welcome function. The “<name>” is standing for anything after the “/”. So we take that as the parameter to our function and pass it to the render_template function as name. So, after passing the variable name in the render_template function, it would be accessible in the template for us to render that variable. You can even perform an operation on the variable and then parse it.

No, we need to create another template called “welcome.html” inside the template folder. This file should contain the following markup

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>FlaskTest</title>
</head>
 
<body>
    <h2>Welcome To GFG</h2>
    <h3>Welcome, {{name}}</h3>
</body>
 
</html>


Using Jinja template

Flask – Jinja Template Inheritance Example

Now, we need a way to actually inherit some templates instead of reusing them, we can do that by creating the blocks in Jinja. They allow us to create a template block and we can use them in other templates with the name given to the block. 

So, let us re-use our “index.html” and create a block in there. T do that we use “{% block <name> %} (where name = ‘body’) to start the block, this will take everything above it and store it in a virtual block of template, to end the block you simply use “{% endblock %}” this will copy everything below it. 

templates/index.html 

HTML




<!DOCTYPE html>
<html>
<head>
<title>FlaskTest</title>
</head>
<body>
<h2>Welcome To GFG</h2>
<h4>Flask: Rendering Templates</h4>
<a href="{{ url_for('home') }}">Home</a>
<a href="{{ url_for('index') }}">Index</a>
{% block body %}
 
 
<p>This is a Flask application.</p>
 
 
{% endblock %}
</body>
</html>


So, here we are not including the <p> tags as everything below the {% endblock %} and everything above the {% block body %} tag is copied. We are also using absolute URLs. The URLs are dynamic and quite easy to understand. We enclose them in “{{ }}” as part of the Jinja2 syntax. The url_for function reverses the entire URL for us, we just have to pass the name of the function as a string as a parameter to the function.

Now, we’ll create another template to reuse this created block “body”, let’s create the template “home.html” with the following contents:

templates/home.html

HTML




{% extends 'index.html' %}
 
{% block body %}
 
<p> This is a home page</p>
 
{% endblock %}


This looks like a two-liner but will also extend (not include) the index.html. This is by using the {% extends <file.html> %} tags, they parse the block into the mentioned template. After this, we can add the things we want. If you use the include tag it will not put the replacement paragraph in the correct place on the index.html page. It will create an invalid HTML file, but since the browser is very forgiving you will not notice unless you look at the source generated. The body text must be properly nested.

Finally, the piece left here is the route to home.html, so let’s create that as well. Let’s add another route to the “server.py file”

Python




@app.route("/home")
def home():
    return render_template("home.html")


So, this is a route bound to the “/home” URL with the home function that renders the template “home.html” that we created just right now.

Demonstrating block and URLs

As we can see the URL generated is dynamic, otherwise, we would have to hardcode both the template page paths. And also the block is working and inheriting the template as provided in the base templates. Open the page source in the browser to check it is properly formed HTML.

<!DOCTYPE html>
<html>
<head>
<title>FlaskTest</title>
</head>
<body>
   <h2>Welcome To GFG</h2>
   <h4>Flask: Rendering Templates</h4>
   <a href="/home">Home</a>
   <a href="/">Index</a>
   <a href="/about">About</a>
   <a href="/documentation">Documentation</a>
   
<p> This is a home page</p>
<p>must use extends not include</p>
</body>
</html>

Inducing Logic in Templates: We can use for loops if conditions in templates. this is such a great feature to leverage on. We can create some great dynamic templates without much of a hassle. Let us create a list in Python and try to render that on an HTML template. 

Using for loops in templates: For that, we will create another route, this time at “/about”, this route will bind to the function about that renders the template “about.html” but we will add some more things before returning from the function. We will create a list of some dummy strings and then parse them to the render_template function.

Python




@app.route("/about")
def about():
    sites = ['twitter', 'facebook', 'instagram', 'whatsapp']
    return render_template("about.html", sites=sites)


So, we have created the route at “/about” bound to the about function. Inside that function, we are first creating the list “Sites” with some dummy strings and finally while returning, we parse them to the render_template function as sites, you can call anything you like but remember to use that name in the templates. Now, to create the templates, we’ll create the template “about.html” with the following contents:

templates/about.html

HTML




{% extends 'index.html' %}
{% block body %}
<ul>
    {% for social in sites %}
    <li>{{ social }}</li>
    {% endfor %}
</ul>
{% endblock %}


We can use for loops in templates enclosed in “{% %}” we can call them in a regular Pythonic way. The sites are the variable(list) that we parsed in the route function. We can again use the iterator as a variable enclosed in “{{ }}”. This is like joining the puzzle pieces, the values of variables are accessed with “{{ }}”, and any other structures or blocks are enclosed in “{% %}. 

Now to make it more accessible you can add its URL to the index.html like so:

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>FlaskTest</title>
</head>
 
<body>
    <h2>Welcome To GFG</h2>
    <h4>Flask: Rendering Templates</h4>
    <a href="{{ url_for('home') }}">Home</a>
    <a href="{{ url_for('index') }}">Index</a>
    <a href="{{ url_for('about') }}">About</a>
    {% block body %}
     
     
<p>This is a Flask application.</p>
 
 
    {% endblock %}
</body>
 
</html>


 
This is not mandatory but it creates an accessible link for ease.  

Demonstrating for loop-in templates

As we can see it has dynamically created all the lists in the template. This can be used for fetching the data from the database if the app is production ready. Also, it can be used to create certain repetitive tasks or data which is very hard to do them manually. 

Corrected extends file

This correctly defined extends file removed the placeholder paragraph and replaces it in the body of the HTML.

If statement in HTML Template in Python Flask

We can even use if-else conditions in flask templates. Similar to the syntax for the for loops we can leverage that to create dynamic templates. Let’s see an example of a role for a website.

Let’s build the route for the section contact. This URL is “contact/<role>”, which is bound to the function contact which renders a template called “contacts.html”. this takes in the argument as role. Now we can see some changes here, this is just semantic changes nothing new, we can use the variable person as a different name in the template which was assigned as the values of the role.

Python




@app.route("/contact/<role>")
def contact(role):
    return render_template("contact.html", person=role)


So, this creates the route as desired and parses the variable role as a person to the template. Now let us create the template.

template/contact.html

HTML




{% extends 'index.html' %}
 
{% block body %}
  {% if person == "admin" %}
 
   
<p> Admin Section </p>
 
 
  {% elif person == "maintainer" %}
 
       
<p> App Source Page for Maintainer</p>
 
 
  {% elif person == "member" %}
 
       
<p> Hope you are enjoying our services</p>
 
 
  {% else %}
 
       
<p> Hello, {{ person }}</p>
 
 
  {% endif %}
{% endblock %}


So, in the template, we are checking the value of the variable person which is obtained from the URL and parsed from the render_template function. The if-else syntax is similar to Python with just “{% %}” enclosed. The code is quite self-explanatory as we create if-elif and else ladder, checking for a value and creating the HTML elements as per the requirement. 

 

So, we can see that the template is rendering the contents as per the role variable passed in the URL. Don’t try to create a URL link for this as it would not work since we need to enter the role variable manually. There needs to be some workaround done to use it.

So that was about using and rendering the templates in Flask. We have leveraged the Jinja templating syntax with Python to create some dynamic templates. 



Previous Article
Next Article

Similar Reads

Server Side Rendering vs Client Side Rendering vs Server Side Generation
In the world of web development, there are several approaches to rendering web pages: server-side rendering, client-side rendering, and server-side generation. Each approach has its own advantages and disadvantages, and choosing the right one for your project depends on your specific needs and goals. In this blog, we’ll explore the differences betw
4 min read
Explain lifecycle of component re-rendering due to re-rendering of parent component
React is a javascript library that renders components written in JSX. You can build and render any component as per your usage. States can be updated accordingly using the setState method. Props can be updated only by the parent component. Updating of state and props leads to the rendering of UI. Different lifecycle methods are called during these
2 min read
How does SSR(Server-Side Rendering) differ from CSR(client-side rendering) ?
Server-Side Rendering (SSR) and Client-Side Rendering (CSR) are two different approaches used in web development to render web pages to users. Each approach has its own set of advantages and disadvantages. In this article, we will learn about the difference between SSR and CSR with examples and features. Table of Content Server-Side Rendering (SSR)
4 min read
Free Website Templates - HTML and CSS Templates with Source Code
HTML (HyperText Markup Language) and CSS (Cascading Style Sheets) are the fundamental building blocks for designing and developing any web page or website. They create an interactive and visually appealing user interface through various CSS properties. This article curates a list of top-notch HTML & CSS template-based projects designed to enhan
5 min read
Flask - Templates
In this article, we are going to learn about the flask templates in Python. Python is a high-level, open-source, object-oriented language, consisting of libraries, used in many domains, such as Web Development, Machine Learning, and so on. In web development, Python is used for building REST APIs, server-side apps, etc. Popular libraries, are Djang
8 min read
Unit Testing Jinja Templates with Flask
Unit testing is an essential practice in software development to ensure the accuracy and functionality of code. When working with Jinja templates in Python, it's crucial to validate the correctness of the generated output. In this article, we will see how to perform unit testing Jinja Templates with Flask. Pre-requisite Unit TestingJinja TemplatePy
4 min read
Documenting Flask Endpoint using Flask-Autodoc
Documentation of endpoints is an essential task in web development and being able to apply it in different frameworks is always a utility. This article discusses how endpoints in Flask can be decorated to generate good documentation of them using the Flask-Autodoc module. This module provides the following features - Helps to document endpoints of
4 min read
How to use Flask-Session in Python Flask ?
Flask Session - Flask-Session is an extension for Flask that supports Server-side Session to your application.The Session is the time between the client logs in to the server and logs out of the server.The data that is required to be saved in the Session is stored in a temporary directory on the server.The data in the Session is stored on the top o
4 min read
How to Integrate Flask-Admin and Flask-Login
In order to merge the admin and login pages, we can utilize a short form or any other login method that only requires the username and password. This is known as integrating the admin page and Flask login since it simply redirects users to the admin page when they log in. Let's is how to implement this in this article. Integrate Flask Admin and Fla
8 min read
Minify HTML in Flask using Flask-Minify
Flask offers HTML rendering as output, it is usually desired that the output HTML should be concise and it serves the purpose as well. In this article, we would display minification of output routes of Flask responses using the library - Flask-Minify. Advantages of MinificationWebsites load faster as fewer lines are there to upload and download.Ban
12 min read
Flask URL Helper Function - Flask url_for()
In this article, we are going to learn about the flask url_for() function of the flask URL helper in Python. Flask is a straightforward, speedy, scalable library, used for building, compact web applications. It is a micro framework, that presents developers, useful tools, and, features, for coding REST APIs, and backend data processing, of web apps
11 min read
ReactJS Rendering Elements
In this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered. What are React Elements?React elements are different from DOM elements as React elements are simple JavaScript objects and are efficient to create. React elements are the building b
3 min read
CSS shape-rendering Property
The shape-rendering property is used to hint the renderer about the tradeoffs that have to be made while rendering shapes like circles, rectangles or paths. The renderer can be told to make the shape geometrically precise or optimize the shape to speed up rendering in certain situations. Syntax: shape-rendering: auto | optimizeSpeed | crispEdges |
4 min read
CSS image-rendering Property
The image-rendering property is used to set the type of algorithm used for image scaling. This property can be used to modify the scaling behavior when the user scales the image above or below the original dimensions. Syntax: shape-rendering: auto | crisp-edges | pixelated | initial | inherit Property Values: auto: It is used to indicate that the s
3 min read
Rendering Data-Frame to html template in table view using Django Framework
In Django, It is easy to render the HTML templates by setting URLs of respective HTML pages. Here we will let to know about how we can work with DataFrame to modify them for table view in the HTML template or web-pages, and for that, we have to use 'render' and 'HttpResponse' functions to manipulate the data inside the DataFrame. Sample DataFrame:
3 min read
Node.js Server Side Rendering (SSR) using EJS
Server-side rendering (SSR) is a popular technique for rendering a normally client-side-only single-page app (SPA) on the server and then sending a fully rendered page to the client. The client's JavaScript bundle can then take over and the SPA can operate as normal. SSR technique is helpful in situations where the client has a slow internet connec
3 min read
Template Rendering via Marko.js in Node.js
Marko: Marko makes it easy to represent your UI using a syntax that is like HTML. It is a friendly and super fast UI library that makes building web apps bearable and fun. It is so much like HTML, that you can use it as a replacement for a templating language like handlebars, mustache, or pug. However, Marko is much more than a templating language.
3 min read
PYGLET – Making Window Current OpenGL rendering context
In this article we will see how we can make the window current opengl rendering context in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or ca
2 min read
SVG text-rendering Attribute
The text-rendering attribute gives hint about what contracts should be made while rendering text. It has an effect only on the text element. Syntax: text-renderring = auto | optimizeLegibility | geometricPrecision | optimizeSpeed Attribute Values: The text-rendering attribute accepts the values mentioned above and described below: auto: It shows th
2 min read
SVG shape-rendering Attribute
The shape-rendering attribute hints the renderer about the tradeoff's to be made while rendering shapes like paths, circles, or rectangles. It has effect only on the following elements: <circle>, <ellipse>, <line>, <path>, <polygon>, <polyline>, and <rect>. Syntax: shape-rendering = auto | optimizeSpeed | c
2 min read
CSS text-rendering
Text-rendering is a property that allows you to choose what to optimize when rendering text. It sends the information to the rendering engine about what to optimize for while rendering text. It's actually not a CSS property and is not defined in any CSS standard. It's an SVG property but Gecko and Webkit browsers allow you to apply this property to
2 min read
How to set focus on an input field after rendering in ReactJS ?
Setting focus on an input field after rendering in ReactJS can be done in the following ways, but before doing that, set up your project structure with the following steps: Creating React Application: Step 1: Create a React application using the following command: npx create-react-app foldernameStep 2: After creating your project folder i.e. folder
2 min read
Vue.js Declarative Rendering
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. Declarative rendering in Vue enabl
2 min read
Vue.js Conditional Rendering
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. Conditional Rendering in Vue makes
2 min read
p5.js DOM and Rendering Complete Reference
DOM is a way to represent the webpage in a structured hierarchical way so that it will become easier for programmers and users to glide through the document. The functions used in p5.js are mentioned below. DOM Description select()It searchs an element in the page with the given id, class or tag name and return it as a p5.element. selectAll()It sea
2 min read
How to position absolute rendering the button in a new line ?
The position CSS property sets how an element is positioned in a document. Absolute positioning allows you to place your element where you want. The positioning is done relative to the first absolutely positioned element. The element we position absolute will be removed from the normal flow of the document and no space is created in the page for th
2 min read
Critical Rendering Path Flow
Have you ever wondered what occurs behind the scenes when webpage loads or how your browser processes these pages? Then you've come to the right place to discover more about it. The flow that a browser follows to transform a file into a webpage is known as the Critical Rending Path. The below diagram shows the flow of the Critical Rendering Path(CR
4 min read
jQWidgets jqxScheduler rendering Property
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxScheduler widget is used to show a set of appointments in a day, week, month, timeline day, timeline week as well as timeline month views. The rendering prope
2 min read
Vue.js List Rendering v-for with a Range
Vue.js is one of the best frameworks for JavaScript like React JS. The Vue JS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. The Vue JS is supported by all popular browsers like Chrome, Firefox, IE, Safari, etc. There are a lot of data that needs to
3 min read
Vue.js List Rendering v-for with v-if
Vue.js is one of the best frameworks for JavaScript like React JS. The Vue JS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. In order to repeat a task for a fixed amount of time, we make use of the for loop. There are a lot of data that needs to be r
3 min read