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 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 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.4.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.4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

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

Hashes for di_flask-0.1.4.tar.gz
Algorithm Hash digest
SHA256 ee80382588c884cb8efe211fcc6dd9940ee8823b9b1f877a310ceccf7b5e5760
MD5 e00a9f4cca368d1142e942cb4f479d3b
BLAKE2b-256 e795f07f97910ea978332abed2ee7c62d3404998d2a0ed2661eb74dc87489dfa

See more details on using hashes here.

Provenance

The following attestation bundles were made for di_flask-0.1.4.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.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

Hashes for di_flask-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2d2f7375ca1c1b418ebef13f21be4d472a1b6de2a0b39d19ed8f84cce13396ec
MD5 5345381d579617ea249ca59faaa18ba4
BLAKE2b-256 3fa4c0a76854ce988c35e8df8555b46f63d796d2fd89da3f126775c7748554e5

See more details on using hashes here.

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

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