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

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.10
File Size Uploaded
di_flask-0.1.10.tar.gz 42.0 kB Details

Built distribution (wheel)

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

Total release size: 49.7 kB

Release files / di_flask-0.1.10.tar.gz

Download URL di_flask-0.1.10.tar.gz
Size 42.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d3b2a43a1261b027e92f512f8931b2c098c22a4311acd19d69ee6da58b8a72ce
BLAKE2b-256 checksum
How to use checksums
a2225949896f532e6490ac97f0efff61029c453f876c5c987e66189cb5107763
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.10-py3-none-any.whl

Download URL di_flask-0.1.10-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b16bf3e7f7a0a2fb94839233262375a8042abf52a0898e306c69868325bc696
BLAKE2b-256 checksum
How to use checksums
8042c812936863d3fb6175d1bde2bc3e49e95b2bfa494cb2288a0ea831251f6e
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

This release

0.1.10 This release

2 release files

0.1.9

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