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

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 opens 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 the flask_request_provider that validate=True checks for, so
# open the root container yourself only after setup_di, before serving
container = Container(groups=[Dependencies], validate=True)
setup_di(app, container)
container.open()

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 lifecycle is yours to own end-to-end: call .open() after setup_di (which registers flask_request_provider) and before serving — required under modern-di 3.x's mandatory-open lifecycle, since validate=True runs at open() time and needs that provider already registered — and call fetch_di_container(app).close_sync() at your process-shutdown point.

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
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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modern_di_flask-3.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modern_di_flask-3.0.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file modern_di_flask-3.0.1.tar.gz.

File metadata

  • Download URL: modern_di_flask-3.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.30 {"installer":{"name":"uv","version":"0.11.30","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}

File hashes

Hashes for modern_di_flask-3.0.1.tar.gz
Algorithm Hash digest
SHA256 c998f6c2a6d1f79828d15631a278862f5bdd31d7aeaba21655b16800d1611acc
MD5 d7bf04b731b6f1c05771ade8f4806b24
BLAKE2b-256 7b787e06f81d2319530244b29038cbd3cd9eca93d6c1564db69a2bbfe67efc54

See more details on using hashes here.

File details

Details for the file modern_di_flask-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: modern_di_flask-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.30 {"installer":{"name":"uv","version":"0.11.30","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}

File hashes

Hashes for modern_di_flask-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37ec9cd7a80944aabae44da72656b1edf6c2c9300d6f34ea99aa2adf65a46b96
MD5 f8dcbc15340c114aba425e1813f046cd
BLAKE2b-256 056f2ea13ff6153b0a1b9fddd27a2431678689f549613b0c2c975df97e9c7638

See more details on using hashes here.

Release history Release notifications | RSS feed

3.1.0

2 files

This release

3.0.1 This release

2 files

3.0.0

2 files

2.1.0

2 files

2.0.0

2 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