Flask is a lightweight WSGI web framework for Python, designed to make getting started quick and easy while providing the flexibility to scale to complex applications. Unlike full-stack frameworks, Flask is intentionally minimalist — it provides core functionality for routing, requests, and templating, then lets you choose extensions for databases, authentication, and other features. As of Flask 3.x, the framework supports async views natively, class-based views via MethodView, secret key rotation, and improved configuration patterns including environment prefix loading. This modular approach makes Flask particularly well-suited for microservices, REST APIs, and projects where you want full control over the architecture without unnecessary abstractions.
What This Cheat Sheet Covers
This topic spans 24 focused tables and 193 indexed concepts, 133 flashcards, 8 practice tests with 232 questions. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Application Setup and Configuration
Everything starts with the Flask object, the central application instance that ties routing, requests, and config together. These entries cover how you create that instance, run the development server, and load settings from classes, files, or the environment, plus the security sensitive knobs like SECRET_KEY and DEBUG. The init_app() factory pattern near the end is what lets larger apps stay testable and spin up multiple instances cleanly.
| Method | Example | Description | |
|---|---|---|---|
app = Flask(__name__) | • Creates the core application object • __name__ helps Flask locate resources and templates relative to the module. | ||
app.run(debug=True, port=5000) | • Starts the development server with optional debug mode and custom port • the flask run CLI command is the recommended way to launch it instead. | ||
app.config.from_object('config.DevelopmentConfig') | Loads only the uppercase attributes from a class or module, never a plain dict. | ||
app.config.from_envvar('APP_SETTINGS') | Loads a config file whose path is named by an environment variable, a shortcut for from_pyfile(os.environ[...]). | ||
app.config.from_prefixed_env(prefix='FLASK_') | Reads environment variables directly, loading every variable that starts with the prefix (default FLASK_). | ||
app.config.from_file('config.json', load=json.load) | Loads config from any file format using a callable parser you pass in ( json.load, tomllib.load, etc). | ||
app.secret_key = 'your-secret-key'# or app.config['SECRET_KEY'] | • Sets the secret key used to sign session cookies and CSRF tokens • signs, does not encrypt, so must be random and kept secure. | ||
app.debug = True | • Enables debug mode: interactive debugger, automatic reload, and detailed error pages • unreliable if set in code, prefer --debug or FLASK_DEBUG. | ||
app.config['TESTING'] = True | Enables testing mode, which propagates exceptions to your test runner instead of the app's error handlers. | ||
db = SQLAlchemy()def create_app(): db.init_app(app) | • Defers extension initialization until an app instance is available • enables the application factory pattern and reusing one extension across multiple apps. | ||
app.config['PROPAGATE_EXCEPTIONS'] = True | • Forces exceptions to be re-raised instead of handled by error handlers • implicitly True when TESTING or DEBUG is enabled. | ||
app.config['SERVER_NAME'] = 'example.com:5000' | Sets the hostname and port used by url_for() outside a request context, it does not restrict which hosts Flask accepts requests from. | ||
app.config['APPLICATION_ROOT'] = '/app' | Sets the URL prefix for generating URLs outside a request context, inside a real request the dispatcher sets SCRIPT_NAME instead. | ||
app.config['PREFERRED_URL_SCHEME'] = 'https' | Sets the URL scheme ( http or https) used by url_for() when building external URLs outside a request context. | ||
app.config['SECRET_KEY_FALLBACKS'] = ['old-key'] | • List of old secret keys still accepted for unsigning (Flask 3.1+) • enables zero-downtime key rotation without an instant mass logout. |