Prerequisite: HTML Basics, Python Basics, Flask
It is not possible to write front-end course every time user make changes in his/her profile. We use a template and it generates code according to the content.
Flask is one of the web development frameworks written in Python. Through flask, a loop can be run in the HTML code using jinja template and automatically HTML code can be generated using this.
The code will be stored in Directories in the format of Flask. So we will be making two directories,
- static – For static Files like images, css, js
- templates – For Html templates
app.py file which will contain all the Python file will be stored in the main directory and index.html file will be stored in templates.
app.py
The code of app.py is same for both examples. We will print a Python list with Some names of Pokemons first in the format of a list
and then a table.
from flask import Flask, render_template
app = Flask(__name__)
Pokemons =["Pikachu", "Charizard", "Squirtle", "Jigglypuff",
"Bulbasaur", "Gengar", "Charmander", "Mew", "Lugia", "Gyarados"]
@app.route('/')
def homepage():
return render_template("index.html", len = len(Pokemons), Pokemons = Pokemons)
app.run(use_reloader = True, debug = True)
|
Example #1: Making a List
We will use the argument Pokemons passed from python file here to automatically print a list instead of Writing it everytime.
index.html
<!DOCTYPE html>
<html>
<head>
<title>For loop in Flask</title>
</head>
<body>
<ol>
{%for i in range(0, len)%}
<li>{{Pokemons[i]}}</li>
{%endfor%}
</ol>
</body>
</html>
|
Output:
Without writing any data of list, the list will be automatically generated. You can use the css and js to make these look beautiful.

Example #2: Making a Table
We will use the argument Pokemons passed from python file here to automatically print a table instead of Writing it our self. Code for app.py for this example is same as the above one.
index.html
<!DOCTYPE html>
<html>
<head>
<title>For loop in Flask</title>
</head>
<style type="text/css">
th:tr{
color: blue;
}
tr:nth-of-type(2n){
border: 1px solid black;
background-color: rgba(150, 150, 150, 0.5);
}
td{
padding: 8px 8px;
border: 1px solid black;
}
</style>
<body>
<table style="margin-left: 20px;">
<th>
<tr style="color: green; ">
<td>Serial Number</td>
<td>Pokemon Name</td>
</tr>
</th>
{%for i in range(0, len)%}
<tr>
<td>{{i}}</td>
<td>{{Pokemons[i]}}</td>
{%endfor%}
</tr>
</table>
</body>
</html>
|
Output:
Without writing any data of list, the table will be automatically generated.

Instructions to Run code:
- Download the files from link provided above or make and store the code in the same format
- Run the app.py file in root directory
- Go to the local host ( http://127.0.0.1:5000/ in my case) and there you have the website
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
11 Oct, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...