Python | Using for loop in Flask Last Updated : 11 Oct, 2018 Comments Improve Suggest changes Like Article Like Report 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. PYTHON3 1== # importing modules from flask import Flask, render_template # declaring app name app = Flask(__name__) # making list of pokemons Pokemons =["Pikachu", "Charizard", "Squirtle", "Jigglypuff", "Bulbasaur", "Gengar", "Charmander", "Mew", "Lugia", "Gyarados"] # defining home page @app.route('/') def homepage(): # returning index.html and list # and length of list to html page return render_template("index.html", len = len(Pokemons), Pokemons = Pokemons) # if __name__ == '__main__': # running app 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 HTML <!DOCTYPE html> <html> <head> <title>For loop in Flask</title> </head> <body> <ol> <!-- For loop logic of jinja template --> {%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 HTML <!DOCTYPE html> <html> <head> <title>For loop in Flask</title> </head> <!-- Adding some style to table (OPTIONAL) --> <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;"> <!-- Table headers --> <th> <tr style="color: green; "> <td>Serial Number</td> <td>Pokemon Name</td> </tr> </th> <!-- For loop logic of jinja template --> {%for i in range(0, len)%} <!-- table rows --> <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 Comment More infoAdvertise with us Next Article Python | Using for loop in Flask K Kartikaybhutani Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2018 Practice Tags : python Similar Reads Understanding for-loop in Python A Pythonic for-loop is very different from for-loops of other programming language. A for-loop in Python is used to loop over an iterator however in other languages, it is used to loop over a condition. In this article, we will take a deeper dive into Pythonic for-loop and witness the reason behind 6 min read How to Access Index using for Loop - Python When iterating through a list, string, or array in Python, it's often useful to access both the element and its index at the same time. Python offers several simple ways to achieve this within a for loop. In this article, we'll explore different methods to access indices while looping over a sequenc 2 min read Subdomain in Flask | Python Prerequisite: Introduction to Flask In this article, we will learn how to setup subdomains in Flask. But first, let's go through the basic like what is DNS and subdomains. Domain Name System (DNS): The Domain Name System (DNS) is a hierarchical and decentralized naming system for computers, services 3 min read How to Use CSS in Python Flask Flask is a popular web framework for Python that makes it easy to build web applications. One of the key elements of any web application is styling, which is where CSS (Cascading Style Sheets) comes in. CSS allows us to control the look and feel of our web pages, making them more attractive and user 3 min read How PyCharm supports Flask in Python? Flask is a reliable framework for Python web applications and APIs. It is a well-liked option among developers due to its simplicity and adaptability. But to fully realize its potential, effective instruments are needed. PyCharm shows up as a powerful ally with a ton of capabilities designed specifi 3 min read Unused variable in for loop in Python Prerequisite: Python For loops The for loop has a loop variable that controls the iteration. Not all the loops utilize the loop variable inside the process carried out in the loop. Example: Python3 # i,j - loop variable # loop-1 print("Using the loop variable inside :") # used loop variabl 3 min read Todo list app using Flask | Python There are many frameworks that allow building your webpage using Python, like Django, flask, etc. Flask is a web application framework written in Python. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine. Its modules and libraries that help the developer to writ 3 min read How to use Flask-Session in Python Flask Sessions in Flask store user-specific data across requests, like login status, using cookies. Data is stored on the client side but signed with a secret key to ensure security. They help maintain user sessions without requiring constant authentication.This article demonstrates how to implement serve 4 min read Loops in Python - For, While and Nested Loops Loops in Python are used to repeat actions efficiently. The main types are For loops (counting through items) and While loops (based on conditions). Additionally, Nested Loops allow looping within loops for more complex tasks. While all the ways provide similar basic functionality, they differ in th 9 min read Retrieving HTML Form data using Flask Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks. Read th 3 min read Like