Flask
Lightweight Python WSGI microframework with routing, Jinja templates, blueprints, and a built-in test client for HTTP testing.
Official Documentation ↗3.1.3. Check the official docs for the latest API surface.Installation
pip install flaskMethods
No methods match this filter.
Flask is a Python microframework that maps URLs to functions and stays out of the way otherwise. It provides routing, request and response helpers, Jinja templates, and a test client, while extensions supply databases, authentication, and validation.
Overview
The application object is created from an import name and decorated with routes. Each view receives a request context through the request proxy and returns a string, dict, tuple, or Response. Blueprints group routes for larger applications, and application factories make configuration testable. Flask is WSGI-based, so it runs behind Gunicorn or uWSGI in production.
Installation
Install with pip install flask. Virtual environments keep the dependency isolated, and requirements.txt or a lock file pins versions. Production deployments add a WSGI server such as Gunicorn. The built-in development server is for local work only; run it with flask –app app run –debug to get reloading and debug tracebacks.
Core API walkthrough
Flask(__name__) creates the application. @app.route maps a rule with converters such as <int:user_id>. jsonify returns JSON with the correct content type, and plain dicts are serialized automatically in current versions. Blueprint groups routes under a prefix for modular code. app.test_client() calls views in-process for fast tests with status and JSON assertions.
Pitfalls
The development server is single-process and not hardened. View functions must return a response, and returning None raises an error. Request context proxies are only valid during a request, so background threads cannot use request. JSON parsing depends on the content type, and redirects should use url_for rather than hard-coded paths.
Verification
Run pytest with the Flask test client and assert status codes, JSON bodies, and validation errors. Start the development server and curl each route, then check a 404 and a 405 response. Before deploying, run the same suite against a production server command to catch import and configuration differences.
FAQ
- Flask or FastAPI for a new API?
- Flask is synchronous WSGI with a huge extension ecosystem and simple routing. FastAPI is ASGI-first with type-driven validation and automatic OpenAPI docs. Choose based on async needs and whether generated docs matter.
- How do I run Flask in production?
- Do not use the development server. Run the app under a WSGI server such as Gunicorn or uWSGI behind a reverse proxy, with multiple workers, timeouts, and access logging configured by the process manager.
- Why does request.get_json() return None?
- The request content type is not application/json, or the body is empty. Pass force=True to parse regardless of content type, or silent=True to return None instead of raising a 415 error.
Report a problem with this API docOpens a pre-filled issue on GitHub.