Flask
Lightweight WSGI web framework. Current version is 3.1.3 (Feb 2026). Flask 3.0 (Sep 2023) removed a large set of APIs that had been deprecated since 2.x — before_first_request, FLASK_ENV, JSON config keys, and custom json_encoder/decoder. LLMs still generate these removed patterns.
Warnings
- breaking @app.before_first_request and @bp.before_app_first_request decorators removed in Flask 3.0. Extremely common in older tutorials and LLM-generated code.
- breaking FLASK_ENV environment variable, ENV config key, and app.env property removed in Flask 3.0. Using FLASK_ENV=development no longer enables debug mode.
- breaking JSON config keys removed in Flask 3.0: JSON_AS_ASCII, JSON_SORT_KEYS, JSONIFY_MIMETYPE, JSONIFY_PRETTYPRINT_REGULAR. LLMs still generate these in config blocks.
- breaking app.json_encoder and app.json_decoder class attributes removed in Flask 3.0. Custom JSON serialization using these was a common pattern.
- breaking send_file() parameter names changed in Flask 2.0 and finalised in 3.0: attachment_filename → download_name, cache_timeout → max_age, add_etags → etag. Old names raise TypeError.
- deprecated flask.Markup imported from flask is deprecated. Markup was moved to MarkupSafe.
- gotcha flask run requires the app to be discoverable. By default looks for app.py or wsgi.py with an app variable. Use FLASK_APP env var or --app flag for other module names.
- gotcha Async views require Flask[async] (installs asgiref). Defining async def routes without this raises RuntimeError or silently runs synchronously depending on version.
Install
-
pip install Flask -
pip install "Flask[async]" -
pip install "Flask[dotenv]"
Imports
- Flask
from flask import Flask app = Flask(__name__)
- before_first_request
# Use app context or lifespan pattern with app.app_context(): init_db() # Or with app factory + teardown: @app.before_request def check_initialized(): ...
Quickstart
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.get('/')
def index():
return jsonify({'status': 'ok'})
@app.post('/items')
def create_item():
data = request.get_json()
return jsonify(data), 201
@app.errorhandler(404)
def not_found(e):
return jsonify({'error': 'not found'}), 404
# Run: flask --app app run --debug