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 — 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}"
# call setup_di AFTER registering routes (required when using auto_inject) —
# it registers flask_request_provider, so validate after it rather than before
container = Container(groups=[Dependencies])
setup_di(app, container)
container.validate() # optional fail-fast; flask_request_provider is registered by now
Pass auto_inject=True to setup_di to wire every registered view (app and blueprint routes alike) without a per-view @inject; because it walks app.view_functions at call time, setup_di must run after all routes are registered. flask.Request is resolvable within DI via the pre-built flask_request_provider context provider. Flask has no application-startup/shutdown hook, so the root container's shutdown is yours to own. As of modern-di 3.1 a container is open from construction, so no .open() call is needed before serving. Validation is explicit in 3.1: if you want boot-time fail-fast, call .validate() after setup_di (which registers flask_request_provider) — validating first would fail any provider with a non-optional flask.Request dependency, because that provider does not exist yet. Call fetch_di_container(app).close_sync() at your process-shutdown point; that half is still yours, since Flask has no shutdown hook to attach to.
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 IoC container and scopes.
Browse the full list of templates and libraries in
modern-python — see the org profile for the categorized index.
Metadata
Release files for modern-di-flask 3.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modern_di_flask-3.1.1.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_flask-3.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.3 kB
Release files / modern_di_flask-3.1.1.tar.gz
| Download URL | modern_di_flask-3.1.1.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
faabc7fb6d8b421d2a35e41b6fd36e5ef2bc02b48ac482d1a6a71750d6bf2793
|
|
BLAKE2b-256 checksum How to use checksums |
5085f89b4c4aee173ee73166682d2e1393e89247ed6188a7f5ce4abf890c93ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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-3.1.1-py3-none-any.whl
| Download URL | modern_di_flask-3.1.1-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1207ac082b868749ab0ba0ea685f7bb3ed15c786ae4f54e142eac848caf1ca2d
|
|
BLAKE2b-256 checksum How to use checksums |
4d5c39bfc7cbdd390b7542816cbfd3400cbb39a31e7d069304a002d7da765e89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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}
|