Public Wiki71,141View on GitHub
Want pallets/flask knowledge in your AI?

Application

This is the source repository for Flask, the Python WSGI micro web framework. It contains the complete framework implementation (src/flask), its comprehensive test suite (tests), official example applications (Flaskr blog tutorial, JavaScript frontend integration, Celery task queue integration), and Sphinx documentation. This is the actual upstream Flask project that gets published to PyPI as the 'flask' package.

Key Features

  • WSGI Application Core
  • Sans-IO Base Architecture
  • Blueprint System
  • Context Management
  • Signed Cookie Sessions
  • Click CLI Infrastructure
  • Multi-Source Configuration
  • Async Route Support
  • Signal System
  • Flaskr Tutorial App

Coding Conventions

Standards and patterns used in this codebase

Imports

Use lazy imports via module-level __getattr__ for expensive or optional dependencies to improve startup time

In __init__.py, re-export all public API symbols explicitly, and use __getattr__ patterns for deferred loading of heavy modules
src/flask/__init__.pysrc/flask/globals.py
Architecture

Separate sans-IO (protocol-independent) base logic from WSGI-specific implementation using inheritance, placing base classes in sansio/ subpackage

flask/sansio/app.py contains App base class with routing and config logic; flask/app.py contains Flask(App) with WSGI-specific request handling
src/flask/sansio/app.pysrc/flask/app.pysrc/flask/sansio/blueprints.pysrc/flask/blueprints.py
Type Annotations

Use __future__ annotations import and provide full type hints on public API methods, with dedicated type checking tests to validate annotations

from __future__ import annotations at top of every module; tests/type_check/ contains static type validation files
src/flask/app.pysrc/flask/ctx.pysrc/flask/helpers.py
Testing

Use pytest fixtures in conftest.py for test setup; provide reusable app and client fixtures; use fixture apps in tests/test_apps/ for integration scenarios

conftest.py defines app, app_ctx, req_ctx, and client fixtures; test apps are standalone packages under tests/test_apps/
tests/conftest.pytests/test_apps
Context Management

Use context-local proxy objects (via werkzeug.local.LocalProxy) for global-like access to request, app, session, and g, backed by context stacks

current_app, request, session, g are all LocalProxy objects defined in globals.py that look up values from the current context
src/flask/globals.pysrc/flask/ctx.py
API Design

Use decorator-based registration patterns for routes, error handlers, before/after request hooks, and CLI commands

@app.route('/path'), @app.errorhandler(404), @app.before_request, @app.cli.command()
src/flask/app.pysrc/flask/sansio/app.pysrc/flask/cli.py
Configuration

Support multiple configuration sources (mapping, Python files, TOML files, environment variables, prefixed env vars) through a Config class extending dict

Config class provides from_mapping(), from_file(), from_object(), from_prefixed_env() methods for flexible config loading
src/flask/config.py
Documentation

Use Sphinx with intersphinx mappings, custom extensions (singlehtml builder, github link tab), and pallets-sphinx-themes for documentation

docs/conf.py configures intersphinx for Python, Werkzeug, Click, Jinja, MarkupSafe, SQLAlchemy and uses version info from importlib.metadata
docs/conf.py
Module Structure

Keep __main__.py minimal as a CLI entry point, delegating immediately to the CLI module's main function

from flask.cli import main; main()
src/flask/__main__.py
Error Handling

Use custom exception classes and convert HTTP exceptions from Werkzeug; provide both errorhandler decorator and register_error_handler method patterns

App handles HTTPException, unhandled exceptions via handle_exception/handle_http_exception methods with signal support for got_request_exception
src/flask/app.pysrc/flask/sansio/app.py
Naming

Use snake_case for functions, methods, and variables; PascalCase for classes; prefix internal/private attributes with underscore

class Flask, class Blueprint, def make_response, def _get_exc_class_and_code, _request_ctx_stack
src/flask/app.pysrc/flask/blueprints.pysrc/flask/ctx.py
Signals

Use blinker-based signals for extensibility hooks, with named signals created via Namespace and graceful degradation when blinker is unavailable

request_started, request_finished, template_rendered, got_request_exception signals defined in signals module
src/flask/signals.pysrc/flask/app.py