Flask Tutorial: Learn Python Microframework from Scratch (2026)
Flask is a lightweight Python web framework designed for simplicity and flexibility. Created by Armin Ronacher in 2010 as an April Fools' joke that became a serious project, Flask has become the go-to choice for developers who want minimal overhead and maximum control. Unlike Django, Flask provides the essentials — routing, request handling, and templating — while leaving other decisions to the developer.
This philosophy makes Flask ideal for microservices, small-to-medium applications, APIs, and prototypes. Its extensive ecosystem of extensions means you can add exactly what you need.
Flask Routes and Views
In Flask, routes are defined with the @app.route() decorator, which binds a URL pattern to a view function. Route patterns can include variable segments like <username> or <int:post_id>.
HTTP methods are controlled via the methods parameter. Flask 2.0+ supports dedicated decorators like @app.get() and @app.post(). The url_for() function generates URLs from endpoint names, keeping templates decoupled from hard-coded paths.
from flask import Flask, url_for
app = Flask(__name__)
@app.route('/')
def home():
return 'Welcome to Flask
'
@app.route('/user/')
def profile(username):
return f'Profile: {username}
'
@app.route('/post/')
def show_post(post_id):
return f'Post #{post_id}
'
with app.test_request_context():
print(url_for('profile', username='alice'))
Jinja2 Templating Engine
Flask bundles the Jinja2 template engine, which renders dynamic HTML with a Django-inspired syntax. Templates live in a templates/ directory and support variables, filters, and control structures. Template inheritance via extends and block lets you define a reusable layout.
Flask automatically injects the request, session, and g objects into the template context. You can register context processors to make data available globally.
{% block title %}My Site{% endblock %}
{% block content %}{% endblock %}