Skip to main content

Flask-DI

PyPI - Downloads

A minimal and clean FastAPI-style dependency injection system for Flask.

Features

  • FastAPI-style Depends()
  • Automatic injection (no decorators required)
  • Nested dependencies
  • Override system for testing
  • Per-request caching
  • Pure Python and lightweight

Installation

pip install di-flask

Usage

from flask_di import DIFlask, Depends

app = DIFlask(__name__)

def get_db():
    return {"session": "db-session"}

def get_user(db=Depends(get_db)):
    return {"username": "alice", "db": db}

@app.route("/info")
def info(user=Depends(get_user)):
    return user

Overrides

Flask-DI supports overrides for easy mocking and testing of dependacies.

def override_db():
    return {"session": "test-db"}

app.dependency_overrides[get_db] = override_db

Generator dependencies (setup / teardown)

Just like FastAPI, a dependency can be a generator function that yields its value once instead of returning it. Whatever runs before the yield is setup, the yielded value is what gets injected, and whatever runs after the yield is teardown — run automatically once the request finishes (even if the view raised). This is the common pattern for handing out a database session that needs to be closed afterwards:

from typing import Generator
from sqlalchemy.orm import Session

def get_session() -> Generator[Session, None, None]:
    with Session(engine) as session:
        yield session

SessionDep = Annotated[Session, Depends(get_session)]

@app.route("/users")
def list_users(session: SessionDep):
    return [u.name for u in session.query(User).all()]

The default-value style (def view(session=Depends(get_session))) works the same way. Teardown also runs for dependencies resolved manually via app.resolve(...), once the surrounding request or app context ends.

Using flask-login or similar libraries along with di-flask

The exact same werkzeug.local.LocalProxy instance under the hood (same object identity, same thread/context-local behavior), we are just lying to the type checker about its declared type, which is safe here because we know it'll be a DIFlask in any app using this library.

from flask_di import current_app, Depends

@login_manager.user_loader
def load_user(user_id):
    db = current_app.resolve(get_db)
    return db.get(User, int(user_id))

Disclaimer

This is my snippet of code that I use for my Flask project.

Just wanted to share as its dead simple, lightweight and pure python.

Contribution

You are free to contribute to the project in any way you want.

The goal of the project is to be on-par with FastAPI DI style. Please keep it simple, concise and with clear code comments.

Metadata

Release files for di-flask 0.1.9

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

Source distribution (sdist)

Source distribution for di-flask 0.1.9
File Size Uploaded
di_flask-0.1.9.tar.gz 41.8 kB Details

Built distribution (wheel)

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

Total release size: 49.2 kB

Release files / di_flask-0.1.9.tar.gz

Download URL di_flask-0.1.9.tar.gz
Size 41.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9a709ff6fe08f314ed810f4027c553d84827d365ee5186347202c4818fb3113f
BLAKE2b-256 checksum
How to use checksums
38e3721a8413f0e99f4f60e7018be7ee2b48a645db3ad7e0fbb490ee9385c6b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 31, 2026.

Transparency log

Release files / di_flask-0.1.9-py3-none-any.whl

Download URL di_flask-0.1.9-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67b741e38b7494c49676052045f7238f47c3cf2ec417bb45a9be3d39d082cf7a
BLAKE2b-256 checksum
How to use checksums
9666b00742c14f2cc7272ffe25d01fb283df67dd614b09bddf379f07ea96dab8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 31, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.10

2 release files

This release

0.1.9 This release

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

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