Python Flask
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. The reference material consulted while preparing these pages is listed in the section bibliography. |
This section is written against Flask 3.1.3 (released 2026-02-18), Werkzeug 3.1.9 and Python 3.13, checked on
2026-10-07. Flask 3.1 supports Python 3.9 and newer, while the examples use Python 3.10+ syntax. Flask 3.2 had not
been released on that date, so the changes planned for it (the merge of the application and request contexts, the
removal of __version__, a 303 status for redirect, and the end of Python 3.9 and 3.10 support) are described as
upcoming wherever they matter, and are collected on
What’s Changed & Migration.
What Flask is
Flask is a Python web framework for the WSGI protocol. It is deliberately small: it gives you routing, request and response objects, a template engine, signed cookie sessions, a command-line interface and a test client, and it leaves the choice of database layer, form library, authentication and every other building block to you or to extensions. "Micro" means that the core does not decide those things for you, not that the framework is only for small applications.
Flask is not written from scratch. It is a thin layer over the Pallets libraries, and most of what you use daily is implemented one level down:
-
Werkzeug provides the WSGI toolkit: the
RequestandResponseclasses, the URL routing system, the development server and the interactive debugger. -
Jinja renders the templates.
-
Click implements the
flaskcommand and your own CLI commands. -
ItsDangerous signs the session cookie and other tokens.
-
MarkupSafe provides the escaping behind Jinja’s autoescape.
-
Blinker provides the signals.
This section is for developers who:
-
know Python and want to build server-rendered sites, JSON APIs or both with Flask, from a first route to a deployed, tested and monitored application
-
are coming from Django, FastAPI, Spring Boot, Express or ASP.NET Core and want a precise map of what Flask does itself, what an extension does and what is left to you
-
maintain a Flask application written for Flask 1.x or 2.x, or learned from older tutorials and books, and need the current idioms (
DeclarativeBasemodels,db.session.execute(db.select(...)),flask --app,from_prefixed_env,SECRET_KEY_FALLBACKS) together with a table of what to change
The Bookshelf scenario
Every example builds one small original application, the Bookshelf, so the pages fit together instead of being isolated snippets:
-
books,authors,reviewsanduserstables, modelled with SQLAlchemy 2.0 and versioned with Flask-Migrate -
a
catalogblueprint (Jinja pages, WTForms forms, message flashing and uploads of cover images), anauthblueprint (Flask-Login), anadminblueprint (Flask-Admin) and anapiblueprint (flask-smorest and JWT) -
English and Spanish pages with Flask-Babel, caching with Flask-Caching, a Celery job that sends a review digest and a live "new reviews" stream over Server-Sent Events
-
tests with pytest, and deployment with Gunicorn behind nginx and in Docker
The final project layout (pyproject.toml, src/bookshelf/__init__.py with create_app, config.py,
extensions.py, models/, catalog/, auth/, admin/, api/, templates/, static/, migrations/, tests/
and instance/) is assembled on
Application Factory & Project Layout.
Version baseline
The versions below were read from PyPI on 2026-10-07. Every page states the versions it was written against, and the extension pages state the extension’s own release.
| Library | Version | Requires Python | Role in this section |
|---|---|---|---|
Flask |
3.1.3 |
3.9+ |
the framework; 3.2 is unreleased |
Werkzeug |
3.1.9 |
3.9+ |
WSGI toolkit, routing, test client, debugger |
Jinja |
3.1.6 |
3.7+ |
templates |
Click |
8.5.0 |
3.10+ |
command line |
ItsDangerous |
2.2.0 |
3.8+ |
signed cookies and tokens |
MarkupSafe |
3.0.4 |
3.9+ |
escaping |
Blinker |
1.9.0 |
3.9+ |
signals |
Quart |
0.23.1 |
3.13+ |
the ASGI re-implementation of the Flask API |
The extension releases current on the same date were Flask-SQLAlchemy 3.1.1, Flask-Migrate 4.1.0, Flask-WTF 1.3.0, Flask-Login 0.6.3, Flask-JWT-Extended 4.7.4, flask-smorest 0.47.0, APIFlask 3.1.2, Flask-Caching 2.5.1, Flask-Babel 4.0.0, Flask-Admin 2.2.1, Celery 5.6.3, Gunicorn 26.2.0 and Waitress 3.0.2. Several of them have not had a release for a long time (Flask-Login, Flask-Babel and Flask-SQLAlchemy among them), which is why the Extensions & Ecosystem page rates the maintenance status of each.
The four books
Four published books were consulted for the concepts a Flask developer meets in practice, and to find the idioms that are out of date today. They are cited, never copied: no text, code listing, figure or data set from any of them appears in this section, every example is written against the current official documentation, and where a book and the documentation disagree the documentation wins.
| Book | Edition | ISBN | Publisher page | Companion code |
|---|---|---|---|---|
Shalabh Aggarwal, Flask Framework Cookbook: Enhance your Flask skills with advanced techniques and build dynamic, responsive web applications |
Packt, 3rd edition, June 2023 |
978-1-80461-110-4 |
||
Shalabh Aggarwal, Flask Framework Cookbook: Over 80 proven recipes and techniques for Python web development with Flask |
Packt, 2nd edition, July 2019 |
978-1-78995-129-5 |
||
José Haro Peralta, Microservice APIs: Using Python, Flask, FastAPI, OpenAPI and more |
Manning, 1st edition, December 2022 |
978-1-61729-841-7 |
||
Olatunde Adedeji, Full-Stack Flask and React: Learn, code, and deploy powerful web applications with Flask 2 and React 18 |
Packt, 1st edition, October 2023 |
978-1-80324-844-8 |
How each book is used:
-
The two editions of the Flask Framework Cookbook are recipe books that cover the whole ecosystem: configuration, Jinja, SQLAlchemy, WTForms, authentication, REST, administration, internationalization, testing, deployment, containers and miscellaneous tips. The 3rd edition targets Flask 2.2, Werkzeug 2.2, SQLAlchemy 2.0 and Flask-SQLAlchemy 3.0 on Python 3.11, and the 2nd edition targets Flask 1.0, Werkzeug 0.15, SQLAlchemy 1.3 and Flask-SQLAlchemy 2.4 on Python 3.7, so the 2nd edition matters mostly for the Flask 1.x idioms you will meet in old code.
-
Microservice APIs is a framework-agnostic API design book. Only its chapter on Flask uses flask-smorest and marshmallow, and its other chapters on API design, documentation, security and testing are applied to Flask on the API and testing pages of this section.
-
Full-Stack Flask and React is half a React book. Only its Flask chapters are relevant here, on Flask 2.3 with Flask-SQLAlchemy and Flask-JWT-Extended, and the React side is covered by the site’s React section.
The Pallets libraries
Flask is one of the projects of the Pallets organization, together with the libraries it is built on. These are the sources behind the architecture page, and the reason many questions about Flask are really questions about one of them.
| Library | What it contributes to Flask | Documentation |
|---|---|---|
Werkzeug |
|
|
Jinja |
the template language, autoescaping, inheritance, macros, filters and tests |
|
Click |
the |
|
ItsDangerous |
signing and timestamped serializers for the session cookie and for reset or confirmation tokens |
|
MarkupSafe |
the |
|
Blinker |
the signal implementation behind |
Extensions and libraries used
Flask extensions are separate packages that follow the init_app pattern. The table lists every extension and
library the pages use, by the job it does, and the page that shows it in the Bookshelf.
| Area | Extensions and libraries | Page |
|---|---|---|
Database access |
Flask-SQLAlchemy, Flask-SQLAlchemy-Lite, SQLAlchemy 2.0, Python’s |
|
Migrations |
Flask-Migrate, Alembic, Flask-Alembic |
|
Other data stores |
redis-py, PyMongo, Flask-MongoEngine, the Elasticsearch Python client |
|
Forms and CSRF |
Flask-WTF, WTForms, wtforms-sqlalchemy |
|
Sessions |
Flask-Session, ItsDangerous |
|
Authentication |
Flask-Login, Authlib, Flask-Dance, python-ldap, Argon2 and bcrypt |
|
Authorization |
Flask-Principal, Flask-Security |
|
Security hardening |
Flask-Talisman, Flask-Limiter, pip-audit |
|
API tokens |
Flask-JWT-Extended, PyJWT |
|
API frameworks and schemas |
flask-smorest, marshmallow, apispec, APIFlask, Pydantic, Flask-RESTX, flask-openapi3 and Flask-RESTful (the last three for the code you will meet) |
|
Browser integration |
Flask-CORS, Vite |
|
Administration and rich text |
Flask-Admin, CKEditor, nh3 |
|
Internationalization |
Flask-Babel, Babel |
|
Caching |
Flask-Caching, cachelib |
|
Background jobs and e-mail |
Celery, RQ, Flask-Mail |
|
Async and real-time |
gevent, asgiref, Quart, Hypercorn, Uvicorn, flask-sock, Flask-SocketIO |
|
Testing |
pytest, pytest-mock, factory_boy, coverage.py, Schemathesis, Hypothesis |
|
Debugging and observability |
Flask-DebugToolbar, Sentry, OpenTelemetry, Prometheus, py-spy |
Debugging, Logging & Profiling and Cloud Platforms & Monitoring |
Servers and proxies |
Gunicorn, Waitress, mod_wsgi, uWSGI, nginx, Apache httpd |
|
Tooling |
uv, python-dotenv, Docker |
|
LLM APIs |
the OpenAI Python SDK |
Specifications used
The pages rely on these specifications and standards, each linked in the bibliography.
-
PEP 3333 (WSGI) and the ASGI specification, for the two server interfaces
-
RFC 9110 (HTTP semantics) and RFC 9111 (HTTP caching), and RFC 6265 (cookies), for status codes, headers, conditional requests and the session cookie
-
RFC 9457 (Problem Details for HTTP APIs), for error payloads
-
RFC 7519 (JSON Web Token), RFC 8725 and RFC 6750, for token authentication
-
OpenAPI 3.1 and JSON Schema 2020-12, for API descriptions
-
the WHATWG HTML Standard (Server-Sent Events) and the WHATWG Fetch Standard (CORS), for streaming and cross-origin requests
-
the OWASP Cheat Sheet Series, for the security guidance the official security page leaves open
Reading path
The pages are ordered so that each one builds on the previous ones. Read them in this order the first time, then use them as a reference:
-
Foundations: what Flask is made of, a first running app, the application factory, configuration and the two contexts.
-
Routing, views and requests: URLs, request data, responses, errors, hooks and blueprints.
-
Templates, sessions, forms and files: Jinja, static files, signed sessions, Flask-WTF and uploads.
-
Data: sqlite3 and SQLAlchemy, migrations and the other data stores.
-
Authentication, authorization and security: Flask-Login, social login, roles and the security checklist.
-
Building APIs: REST design, OpenAPI with flask-smorest, JWT and single-page applications.
-
Extensions and ecosystem: the extension model, the CLI, signals, the admin, translations, caching and Celery.
-
Async, streaming and real-time:
asyncviews, gevent, Quart, streaming and Server-Sent Events. -
Testing and debugging: pytest, contract tests, logging and profiling.
-
Production: Gunicorn, nginx, containers and cloud platforms.
-
Beyond the basics: calling LLM APIs, and what changed between releases.
If you already know an older Flask, start with What’s Changed & Migration and read the topic pages afterwards. The cheat sheet at Flask Cheat Sheet condenses the whole section onto one page for quick lookup.
What’s covered
Where related material lives elsewhere on this site
The Flask pages link to existing material instead of repeating it, and cover only what Flask adds.
| Topic | Where it lives | How the Flask section relates |
|---|---|---|
The Python language, typing, asyncio, virtual environments, pytest |
Python Reference, including Concurrency & Async, Virtual Environments & Packaging and Testing |
Flask pages link here for general Python and cover only what Flask adds. |
Other Python web frameworks |
The introduction compares Flask with each of them, and the migration page covers Flask to FastAPI and Quart. |
|
Mounting Flask inside FastAPI |
The migration page links there for |
|
OAuth 2.0, OpenID Connect and JWT theory |
The authentication pages cover only the Flask libraries (Authlib, Flask-Dance, Flask-JWT-Extended). |
|
GraphQL |
The REST API page compares GraphQL briefly and links here. |
|
Docker and containers |
The Docker page shows only the Flask-specific Dockerfile and Compose files. |
|
Kubernetes |
The Docker & Kubernetes page shows only the Flask-specific Deployment, probes and migration Job. |
|
Cloud deployment |
The cloud page covers the Flask-specific parts of each hosting platform. |
|
Database engines |
SQL Reference, MongoDB Reference, Redis Reference and Elasticsearch Reference |
The data pages cover only how to use each from Flask (clients created in the factory, sessions per request). |
Architecture |
Architecture, Architectural Patterns and Starting a New Project: Which Path and Which Technologies |
The project-layout page links the patterns (hexagonal, repository and service layer). |
Messaging and scheduling |
The Celery page points here for brokers and for schedulers beyond Celery beat. |
|
Image and media processing |
The file-upload page hands the processing of uploaded images over to it. |
|
CORS and front ends |
The single-page-application page covers only the Flask side. |
|
AI frameworks and models |
The LLM page stays introductory and points here for depth. |
Bibliography
Every source used to prepare this section is listed here, each linked to its official site. Individual pages list the
subset they derive from under their own == References heading.
Books
-
Aggarwal, S. Flask Framework Cookbook: Enhance your Flask skills with advanced techniques and build dynamic, responsive web applications. 3rd edition, Packt Publishing, June 2023. ISBN 978-1-80461-110-4. Packt publisher page; companion code: Flask-Framework-Cookbook-Third-Edition on GitHub
-
Aggarwal, S. Flask Framework Cookbook: Over 80 proven recipes and techniques for Python web development with Flask. 2nd edition, Packt Publishing, July 2019. ISBN 978-1-78995-129-5. Packt publisher page; companion code: Flask-Framework-Cookbook-Second-Edition on GitHub
-
Haro Peralta, J. Microservice APIs: Using Python, Flask, FastAPI, OpenAPI and more. 1st edition, Manning Publications, December 2022. ISBN 978-1-61729-841-7. Manning publisher page; companion code: abunuwas/microservice-apis on GitHub
-
Adedeji, O. Full-Stack Flask and React: Learn, code, and deploy powerful web applications with Flask 2 and React 18. 1st edition, Packt Publishing, October 2023. ISBN 978-1-80324-844-8. Packt publisher page; companion code: Full-Stack-Flask-and-React on GitHub
Official Flask documentation
Published at flask.palletsprojects.com, grouped as the documentation is (the development version is at flask.palletsprojects.com/en/latest, and the release notes for the unreleased 3.2 are on its changes page):
-
User’s Guide
-
Tutorial
-
Patterns for Flask
-
Deploying to Production
-
API Reference
-
Additional Notes
-
Releases and package
Pallets libraries and Quart
-
Blinker: Blinker documentation
-
Click
-
ItsDangerous
-
Jinja
-
MarkupSafe: MarkupSafe documentation
-
Quart
-
Werkzeug
Flask extensions and companion projects
-
APIFlask
-
corydolphin/flask-cors (GitHub): Flask-CORS: Releases
-
Flask-Admin
-
Flask-Alembic: Flask-Alembic: documentation
-
Flask-Babel (PyPI): Flask-Babel on PyPI
-
Flask-Bcrypt: Flask-Bcrypt documentation
-
Flask-Caching: Flask-Caching documentation
-
Flask-CORS
-
Flask-Dance
-
Flask-Dance (PyPI): Flask-Dance on PyPI
-
Flask-DebugToolbar: Flask-DebugToolbar documentation
-
Flask-HTTPAuth: Flask-HTTPAuth documentation
-
Flask-JWT-Extended
-
Flask-Limiter: Flask-Limiter: Documentation
-
Flask-Login: Flask-Login documentation
-
Flask-Mail: Flask-Mail: Documentation
-
Flask-Marshmallow: Flask-Marshmallow documentation
-
Flask-Migrate: Flask-Migrate: documentation
-
flask-openapi3: flask-openapi3 documentation
-
flask-orjson: flask-orjson documentation
-
Flask-Principal: Flask-Principal documentation
-
Flask-PyMongo: Flask-PyMongo documentation
-
Flask-RESTful
-
Flask-RESTX
-
Flask-Security
-
Flask-Session: Flask-Session documentation
-
flask-smorest
-
flask-sock: flask-sock documentation
-
Flask-SocketIO: Flask-SocketIO documentation
-
Flask-SQLAlchemy
-
Flask-SQLAlchemy-Lite: Flask-SQLAlchemy-Lite documentation
-
Flask-WTF
-
GoogleCloudPlatform/flask-talisman (GitHub): Flask-Talisman
-
miguelgrinberg/Flask-Migrate (GitHub): Flask-Migrate: Change Log
-
pallets-eco/flask-mail (GitHub): Flask-Mail: Source repository
-
python-babel: Flask-Babel documentation
-
python-babel/flask-babel (GitHub): Flask-Babel on GitHub
Libraries and tools used by the pages
-
aiosmtpd: aiosmtpd: Documentation
-
Alembic
-
apiaryio/dredd (GitHub): Dredd (archived)
-
apispec
-
argon2-cffi: argon2-cffi: API Reference
-
Authlib
-
Authlib (PyPI): Authlib on PyPI
-
Authlib JOSE: joserfc documentation
-
aws-actions/amazon-ecr-login (GitHub): aws-actions/amazon-ecr-login
-
aws-actions/configure-aws-credentials (GitHub): aws-actions/configure-aws-credentials
-
awslabs/aws-lambda-web-adapter (GitHub): AWS Lambda Web Adapter
-
Azure/login (GitHub): Azure/login
-
Babel
-
benfred/py-spy (GitHub): py-spy on GitHub
-
cachelib: cachelib documentation
-
Celery
-
Connexion: Connexion documentation
-
coverage.py: coverage.py: Configuration reference
-
django/asgiref (GitHub): asgiref: WSGI to ASGI adapter
-
factory_boy
-
fakeredis
-
google-github-actions/auth (GitHub): google-github-actions/auth
-
google-github-actions/deploy-cloudrun (GitHub): google-github-actions/deploy-cloudrun
-
Hypothesis
-
ijl/orjson (GitHub): orjson
-
jsonschema: jsonschema: Schema Validation
-
ldap3
-
ldap3 (PyPI): ldap3 on PyPI
-
marshmallow
-
MongoEngine: MongoEngine: Connecting to MongoDB
-
mongomock/mongomock (GitHub): mongomock
-
nh3: nh3 documentation
-
nose2: nose2 documentation
-
OAuthLib: oauthlib: OAuth 2 Security
-
oauthlib/oauthlib (GitHub): oauthlib: token parameter validation (source)
-
openai (PyPI): PyPI: openai
-
openai/openai-python (GitHub): OpenAI Python SDK: README
-
openapi-generators/openapi-python-client (GitHub): openapi-python-client
-
Pillow: Pillow: Image Module
-
pip-audit (PyPI): pip-audit
-
pyca/bcrypt (GitHub): pyca/bcrypt
-
Pydantic
-
PyJWT
-
PyMongo: PyMongo: Tutorial
-
pytest
-
pytest-cov: pytest-cov: Configuration
-
pytest-flask: pytest-flask: Features
-
pytest-mock: pytest-mock: Usage
-
python-ldap
-
python-ldap (PyPI): python-ldap on PyPI
-
python-openapi/openapi-spec-validator (GitHub): openapi-spec-validator
-
redis-py
-
requests-oauthlib: requests-oauthlib: OAuth 2 Workflow
-
RQ
-
Schemathesis
-
SQLAlchemy
-
theskumar/python-dotenv (GitHub): python-dotenv
-
webargs: webargs documentation
-
Whoosh Reloaded: Whoosh-Reloaded: Quick start
-
WTForms
-
wtforms-sqlalchemy.readthedocs.io: wtforms-sqlalchemy documentation
-
zappa/Zappa (GitHub): Zappa
Servers, infrastructure and platforms
-
Amazon Web Services
-
Apache HTTP Server: Apache httpd: mod_proxy
-
Boto3 (AWS SDK for Python)
-
Certbot: Certbot
-
CKEditor
-
Datadog: Datadog: Tracing Python applications
-
Docker
-
Elasticsearch
-
gevent: gevent documentation
-
GitHub Actions and APIs
-
GNU gettext
-
Google Cloud
-
Google Identity: Google: OpenID Connect
-
greenlet: greenlet documentation
-
Gunicorn
-
Hypercorn: Hypercorn documentation
-
Kubernetes
-
Let’s Encrypt: Let’s Encrypt: Documentation
-
Microsoft Azure
-
minikube: minikube: Pushing images
-
mod_wsgi: mod_wsgi: Quick Configuration Guide
-
MongoDB: PyMongo FAQ: Is PyMongo fork-safe?
-
New Relic: New Relic: Install the Python agent
-
nginx
-
OpenAI platform
-
OpenAPI Generator: OpenAPI Generator: Installation
-
OpenTelemetry
-
OpenTelemetry Python: OpenTelemetry Python: Fork process model
-
OpenTelemetry Python Contrib: OpenTelemetry Flask instrumentation
-
Pact: Pact documentation
-
Prometheus: Prometheus: Metric and label naming
-
Prometheus Python client
-
PythonAnywhere: PythonAnywhere: Setting up Flask applications
-
Redis
-
Sentry: Sentry: Flask integration
-
SQLite
-
Supervisor: Supervisor: Configuration File
-
systemd: systemd: systemd.service
-
Tornado: Tornado: tornado.wsgi
-
uv
-
Uvicorn: Uvicorn: Deployment
-
uWSGI: uWSGI: Options
-
Visual Studio Code: Visual Studio Code: Flask Tutorial
-
Vite
-
Waitress
Python language and standard library
-
Python documentation
-
Python Packaging User Guide: Python Packaging User Guide: src layout vs flat layout
Specifications and standards
-
ASGI
-
IETF RFCs
-
JSON Schema: JSON Schema 2020-12: Core
-
Martin Fowler: Martin Fowler: Richardson Maturity Model
-
MDN Web Docs
-
OpenAPI Specification
-
Python Enhancement Proposals: PEP 3333: Python Web Server Gateway Interface v1.0.1
-
Roy T. Fielding: Roy T. Fielding: REST APIs must be hypertext-driven
-
The Twelve-Factor App: The Twelve-Factor App: Config
-
WHATWG Fetch Standard: WHATWG Fetch: CORS protocol
-
WHATWG HTML Standard: WHATWG HTML Standard: Server-sent events
Security guidance
-
CSP Evaluator: CSP Evaluator
-
OWASP
-
OWASP Cheat Sheet Series
-
OWASP GenAI Security Project: OWASP: Top 10 for LLM Applications
The books are consulted references for the narrative and structure of some topics and for the list of outdated idioms; they are not reproduced here. The official documentation linked above is authoritative, and where the two differ, the documentation of the version you run prevails.