FastAPI-style dependency injection for Flask using Annotated and Python 3.12 features.
Project description
Flask-DI
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 flask-di
Usage
[!NOTE] For automatic injection of dependecies we need to wrap Flask class
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
[!TIP] Flask-DI also supports
Annotatedtype alias for declaring dependecies just like FastAPI does.from flask_di import DIFlask, Depends from typing import Annotated app = DIFlask(__name__) def get_db(): return {"session": "db-session"} SessionDep = Annotated(dict, Depends(get_db)) def get_user(session: SessionDep): return {"username": "alice", "session": session} UserDep = Annotated(dict, Depends(get_user)) @app.route("/info") def info(user: UserDep): 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
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file di_flask-0.1.4.tar.gz.
File metadata
- Download URL: di_flask-0.1.4.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee80382588c884cb8efe211fcc6dd9940ee8823b9b1f877a310ceccf7b5e5760
|
|
| MD5 |
e00a9f4cca368d1142e942cb4f479d3b
|
|
| BLAKE2b-256 |
e795f07f97910ea978332abed2ee7c62d3404998d2a0ed2661eb74dc87489dfa
|
Provenance
The following attestation bundles were made for di_flask-0.1.4.tar.gz:
Publisher:
build_and_publish.yml on razorblade23/di-flask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
di_flask-0.1.4.tar.gz -
Subject digest:
ee80382588c884cb8efe211fcc6dd9940ee8823b9b1f877a310ceccf7b5e5760 - Sigstore transparency entry: 732682799
- Sigstore integration time:
-
Permalink:
razorblade23/di-flask@630ea0cf75ae0e0e5ef375f5126ecd68407c18d1 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/razorblade23
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build_and_publish.yml@630ea0cf75ae0e0e5ef375f5126ecd68407c18d1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file di_flask-0.1.4-py3-none-any.whl.
File metadata
- Download URL: di_flask-0.1.4-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d2f7375ca1c1b418ebef13f21be4d472a1b6de2a0b39d19ed8f84cce13396ec
|
|
| MD5 |
5345381d579617ea249ca59faaa18ba4
|
|
| BLAKE2b-256 |
3fa4c0a76854ce988c35e8df8555b46f63d796d2fd89da3f126775c7748554e5
|
Provenance
The following attestation bundles were made for di_flask-0.1.4-py3-none-any.whl:
Publisher:
build_and_publish.yml on razorblade23/di-flask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
di_flask-0.1.4-py3-none-any.whl -
Subject digest:
2d2f7375ca1c1b418ebef13f21be4d472a1b6de2a0b39d19ed8f84cce13396ec - Sigstore transparency entry: 732682808
- Sigstore integration time:
-
Permalink:
razorblade23/di-flask@630ea0cf75ae0e0e5ef375f5126ecd68407c18d1 -
Branch / Tag:
refs/tags/v0.1.4 - Owner: https://github.com/razorblade23
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build_and_publish.yml@630ea0cf75ae0e0e5ef375f5126ecd68407c18d1 -
Trigger Event:
push
-
Statement type: