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

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.8
File Size Uploaded
di_flask-0.1.8.tar.gz 41.7 kB Details

Built distribution (wheel)

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

Total release size: 49.2 kB

Release files / di_flask-0.1.8.tar.gz

Download URL di_flask-0.1.8.tar.gz
Size 41.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2f2f6d2a0efcd68027ad41e63b363b5050993b8de7b41dc8b5de24926e298831
BLAKE2b-256 checksum
How to use checksums
b1490251136ec613ce67e1396f6e9cd44525153c9117c74db10c5f329a530188
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.8-py3-none-any.whl

Download URL di_flask-0.1.8-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
25942a38ee64b80833768daff3140fc1ffd4b681761ec3a89a9aa4a38ed9a37e
BLAKE2b-256 checksum
How to use checksums
facb0e1d0a3001ecd6806bb3d9cc89f670b397bbe4c5a17cb22627d37e1a1fc1
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

This release

0.1.8 This release

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