In this Python Flask article we want to talk about Introduction to Python Flask, Python Flask is popular micro web framework written in Python. it is used for building web applications and APIs quickly and easily. Flask is lightweight, flexible and easy to use, and this makes it popular choice for developers of all levels of experience. in this article, we want to provide an introduction to Python Flask and explore some of its key features and benefits.
First of all we need to install Python Flask
1 |
pip install flask |
After Flask is installed, we can create new Flask application. create new Python file and import the Flask class
1 |
from flask import Flask |
After that we creates new instance of the Flask class, this creates new Flask application with the name __name__, __name__ variable is special Python variable that represents the name of the current module. in this case it will be the name of the Python file that you are running.
1 |
app = Flask(__name__) |
Routing in Flask
One of the key features of Flask is its routing system. Routing is the process of matching URL to a specific function in your Flask application. for create a route in Flask, you need to use @app.route decorator. Decorators are a way to modify the behavior of a function without changing its code. in Flask decorators are used to associate URL with a function.
This is an example of a simple route that returns Welcome to geekscoders.com when you visit the root URL of your Flask application, in this example @app.route(‘/’) decorator associates the root URL (/) with the hello() function. when you visit the root URL of your Flask application, Flask will call hello() function and return the string Welcome to geekscoders.com.
1 2 3 |
@app.route('/') def hello(): return 'Welcome to geekscoders.com' |
Templates in Flask
Flask also supports templates, which are used to render dynamic HTML pages. Templates allow you to separate the presentation logic from the application logic. for using templates in Flask, you need to create templates directory in your Flask application. this directory will contain your HTML templates.
This is an example of simple template that uses Jinja2, popular template engine for Flask, in this example, te {{ title }}, {{ heading }} and {% for name in names %} are Jinja2 template tags. they allows you to insert dynamic content into your HTML templates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html> <head> <title>{{ title }}</title> </head> <body> <h1>{{ heading }}</h1> <ul> {% for name in names %} <li>{{ name }}</li> {% endfor %} </ul> </body> </html> |
For rendering this template in Flask, you need to use render_template function, in this example, names() function returns names.html template, with title, heading and names variables passed to the template.
1 2 3 4 5 6 |
from flask import render_template @app.route('/names') def names(): names = ['Geekscoders', 'Bob', 'John'] return render_template('names.html', title='Names', heading='List of Names', names=names) |
This is the complete code for app.py file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): return 'Welcome to geekscoders.com' @app.route('/about') def about(): return 'This is an about page' @app.route('/names') def names(): names = ['Geekscoders', 'Bob', 'John'] return render_template('names.html', title='Names', heading='List of Names', names=names) if __name__ == '__main__': app.run(debug=True) |
This is our names.html file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html> <head> <title>{{ title }}</title> </head> <body> <h1>{{ heading }}</h1> <ul> {% for name in names %} <li>{{ name }}</li> {% endfor %} </ul> </body> </html> |
This is our project structure
Run the code and go to http://127.0.0.1:5000/names route, this will the results