Skip to main content

modern-di-flask

PyPI version Supported Python versions Downloads Coverage CI License GitHub stars uv Ruff ty

modern-di integration for Flask.

Full guide: Flask integration docs

Usage example: examples/

Installation

uv add modern-di-flask      # or: pip install modern-di-flask

Usage

Flask has no dependency-injection system of its own, so modern-di-flask pairs an @inject decorator with inert FromDI markers (there is no Depends). setup_di installs a before_request/teardown_appcontext pair that builds a per-request Scope.REQUEST child container and closes it once the request finishes. Resolution is sync-only, and the child container is closed with close_sync().

import typing

from flask import Flask
from modern_di import Container, Group, Scope, providers
from modern_di_flask import FromDI, inject, setup_di


class Settings:
    def __init__(self) -> None:
        self.greeting = "hello"


class Dependencies(Group):
    settings = providers.Factory(scope=Scope.APP, creator=Settings)


app = Flask(__name__)


@app.route("/hello/<name>")
@inject
def hello(name: str, settings: typing.Annotated[Settings, FromDI(Dependencies.settings)]) -> str:
    return f"{settings.greeting}, {name}"


container = Container(groups=[Dependencies])
setup_di(app, container)
container.validate()  # optional fail-fast; must come after setup_di registers its providers

Pass auto_inject=True to setup_di to wire every registered view (app and blueprint routes alike) without a per-view @inject. It walks app.view_functions when setup_di runs, so call setup_di after all routes are registered. flask.Request is resolvable within DI via the pre-built flask_request_provider context provider.

A container is open from construction. To fail fast at boot, call .validate() after setup_di, which registers flask_request_provider. Flask has no shutdown hook, so call fetch_di_container(app).close_sync() at process shutdown.

API

Symbol Description
setup_di(app, container, *, auto_inject=False) Registers the container on app.extensions, installs the before_request/teardown_appcontext pair that builds and closes a per-request Scope.REQUEST child, and, if auto_inject=True, wraps every currently registered view with inject; returns the container
FromDI(dependency) Inert marker (used with @inject) that resolves a provider or type from the per-request child container
inject(view) Decorator for a view function; resolves its FromDI-annotated parameters without rewriting the function's signature. Raises RuntimeError naming setup_di when a request reaches it without setup_di called
fetch_di_container(app) Returns the root Container stored on app.extensions
flask_request_provider ContextProvider for flask.Request (REQUEST scope), auto-registered by type

📦 PyPI

📝 License

Part of modern-python

Built on modern-di, a dependency-injection framework with an IoC container and scopes.

Browse the full list of templates and libraries in modern-python; the org profile has the categorized index.

Metadata

Release files for modern-di-flask 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modern-di-flask 4.0.0
File Size Uploaded
modern_di_flask-4.0.0.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for modern-di-flask 4.0.0
File Interpreter ABI Platform
modern_di_flask-4.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.8 kB

Release files / modern_di_flask-4.0.0.tar.gz

Download URL modern_di_flask-4.0.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2ff16b4a8a8e436f61b889822e51839bdaa0350c9bd2f11e0eec3a6f52ac32bc
BLAKE2b-256 checksum
How to use checksums
d15eb9f7e20eb3b184256bf9e32b233b04669415dce6c374ab0cb74449f72fb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / modern_di_flask-4.0.0-py3-none-any.whl

Download URL modern_di_flask-4.0.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f51bb5c1adecaf56b7cf040daf9343ef14bcc4b3bb0e8dde46cf393e42305fb1
BLAKE2b-256 checksum
How to use checksums
64bb06e8ad03c81808e4b58529c2b26a4cbdcbc10ff03e94349d911b83c21c64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.24 {"installer":{"name":"uv","version":"0.12.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

4.0.0 This release

2 release files

3.2.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page