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 Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 min read Like