Flask Dependency
Dependency injection is a powerful concept that allows you to manage and inject dependencies into your application
components. In Flask, we can create a simple dependency injection system inspired by FastAPI's Depends.
Approach
-
Create a Dependency Class: We'll define a class called
Dependsthat will handle our dependencies. This class will allow us to declare dependencies for specific route handlers. -
Decorator for Dependency Resolution: We'll create a decorator that inspects the function and resolves any dependencies when called. This decorator will be applied to our route handlers.
-
Dependency Functions: We'll define individual functions (similar to FastAPI's dependencies) that represent our dependencies. These functions will be called automatically when needed.
Sample Code
# app.py
from flask import Flask, Blueprint
from pydantic import BaseModel
from flask_dependency import BackgroundTask, Depends, route, FormModel
app = Flask(__name__)
blueprint = Blueprint("example_blueprint", __name__)
app.debug = True
def sample_task(duration):
import time
time.sleep(duration)
return f"Slept for {duration} seconds"
# Example Backgraund Task
@route(blueprint, "/backgraund_task", methods=["POST"], endpoint="route_test")
def route_test(background_task: BackgroundTask = Depends()):
background_task.run(sample_task, 1)
return {"message": "OK"}
class InputModelForm(FormModel):
id: int
name: str
class ResponseInputModelForm(BaseModel):
id: int
name: str
class Config:
orm_mode = True
# Example Form
@route(blueprint, "/example_form", methods=["POST"], endpoint="route_test", response_schema=ResponseInputModelForm)
def route_test(input_data: InputModelForm = Depends()):
return {"message": "success"}
app.register_blueprint(blueprint)
Release files for flask_dependency 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flask_dependency-0.3.0.tar.gz | 3.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flask_dependency-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.5 kB
Release files / flask_dependency-0.3.0.tar.gz
| Download URL | flask_dependency-0.3.0.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
329cae9dcb1c5dae6a80f8dce0449a19c6efe093c2a5657144a6a079e1312d28
|
|
BLAKE2b-256 checksum How to use checksums |
9c888bfc96262c9fc03eec41caec57d7ea57c8f6fb8665d568855ed5fcf2e18d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.2.1 CPython/3.14.2 Linux/6.17.12-300.fc43.x86_64
|
Release files / flask_dependency-0.3.0-py3-none-any.whl
| Download URL | flask_dependency-0.3.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7a7c9a25979f82edbdf77940ca5e3407a978f9c959e3b2a2de9e88c1907830eb
|
|
BLAKE2b-256 checksum How to use checksums |
f9409c0c24be72733047a7b7a317bc1e525393277d252d1c38479ea9a3b54c4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.2.1 CPython/3.14.2 Linux/6.17.12-300.fc43.x86_64
|