What Flask Does
- Routing — decorator-based URL rules
- Templates — Jinja2 template engine
- Request/response — Werkzeug WSGI utilities
- Blueprints — modular application components
- Extensions — Flask-SQLAlchemy, Flask-Login, Flask-CORS, Flask-Migrate, Flask-RESTful
- CLI —
flask run, custom commands - Sessions — cookie-based, configurable
- Testing — built-in test client
- Context — application and request context management
- Signals — Blinker-based event system
Architecture
WSGI application. Request enters Werkzeug server, Flask routes it by matching URL rules (decorated functions). Request context is pushed per-request, providing access to request, g, session globals. Jinja2 renders HTML. Blueprints organize large apps into reusable modules.
Self-Hosting
# Production (never use flask run in prod)
pip install gunicorn
gunicorn app:app --bind 0.0.0.0:8000 --workers 4
# Docker
FROM python:3.12-slim
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:8000"]Key Features
- Minimal core (~1500 lines)
- Decorator-based routing
- Jinja2 templates
- Werkzeug request/response
- Blueprints for modularity
- Extensive extension ecosystem
- Built-in debugger
- Test client
- CLI via Click
- Flask 3.0+ requires Python 3.8+
Comparison
| Framework | Type | Async | ORM |
|---|---|---|---|
| Flask | Micro | WSGI (sync) | Via extension |
| FastAPI | Async micro | ASGI | Via SQLAlchemy |
| Django | Full-stack | WSGI + ASGI | Built-in |
| Bottle | Micro | WSGI | None |
| Litestar | Async full | ASGI | Built-in |
FAQ
Q: Flask vs FastAPI? A: FastAPI has native async, built-in OpenAPI docs, and Pydantic validation — good for modern APIs; Flask is simpler with an older ecosystem and more tutorials. For new pure-API projects, FastAPI is recommended; Flask is great for full-stack or existing projects.
Q: Does Flask support async? A: Flask 2.0+ supports async views, but the underlying layer is still WSGI (async runs in a threadpool). For true async I/O, use FastAPI or Quart (Flask's async fork).
Q: How to organize large projects?
A: Use Blueprints to split modules by feature. The Application Factory pattern (a create_app() function) supports different environment configurations.
Sources
- Docs: https://flask.palletsprojects.com
- GitHub: https://github.com/pallets/flask
- License: BSD 3-Clause