Skip to main content

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 di-flask

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 Annotated type 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

di_flask-0.1.5.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

di_flask-0.1.5-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file di_flask-0.1.5.tar.gz.

File metadata

  • Download URL: di_flask-0.1.5.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

Hashes for di_flask-0.1.5.tar.gz
Algorithm Hash digest
SHA256 b1121a9e23d9adf3527e8ed7da418539d2d2cec34a5a8420744b331004d7bd73
MD5 9a67c4121c2784c67de261675149c79d
BLAKE2b-256 85c337d20b636087af1e8e503984617a5f1a8fa3d8bbb682214c85b6948a96e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for di_flask-0.1.5.tar.gz:

Publisher: build_and_publish.yml on razorblade23/di-flask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file di_flask-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: di_flask-0.1.5-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

Hashes for di_flask-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 fefc0f265aac731bb7177c3ce95a136c20c93e3d3b8865b8f252cf111786292f
MD5 57e1fdbc51ee400db80d2c23a5aa33ac
BLAKE2b-256 073209fba272a459e8e8255e109f54b084005730e4de7c55b9ac045e1c1d682e

See more details on using hashes here.

Provenance

The following attestation bundles were made for di_flask-0.1.5-py3-none-any.whl:

Publisher: build_and_publish.yml on razorblade23/di-flask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page