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

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.7
File Size Uploaded
di_flask-0.1.7.tar.gz 42.1 kB Details

Built distribution (wheel)

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

Total release size: 50.7 kB

Release files / di_flask-0.1.7.tar.gz

Download URL di_flask-0.1.7.tar.gz
Size 42.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ac8d96d2ead04aff4f311f937b0db8445dd41b7d03e8c56204f0d2ec39f46a49
BLAKE2b-256 checksum
How to use checksums
09655fb101d6b7e1ccc4a632933d7c89a31819eba034f6b365adc22a0c3ceddb
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 30, 2026.

Transparency log

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

Download URL di_flask-0.1.7-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d8e92db3ec9222988e9b03c43e0ef0de44a9d9d011baa4747ed540b42da5e6e
BLAKE2b-256 checksum
How to use checksums
9fc648cd63fb6f0eb2e827d992f3bf9cf92ddc3cb2b588d9b14dfb7571fba5a9
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 30, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

This release

0.1.7 This release

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