No project description provided
Project description
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)
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
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 flask_dependency-0.3.0.tar.gz.
File metadata
- Download URL: flask_dependency-0.3.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.14.2 Linux/6.17.12-300.fc43.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
329cae9dcb1c5dae6a80f8dce0449a19c6efe093c2a5657144a6a079e1312d28
|
|
| MD5 |
fe9e98656f2e8d6df61ac0e15b93a6a2
|
|
| BLAKE2b-256 |
9c888bfc96262c9fc03eec41caec57d7ea57c8f6fb8665d568855ed5fcf2e18d
|
File details
Details for the file flask_dependency-0.3.0-py3-none-any.whl.
File metadata
- Download URL: flask_dependency-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.14.2 Linux/6.17.12-300.fc43.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a7c9a25979f82edbdf77940ca5e3407a978f9c959e3b2a2de9e88c1907830eb
|
|
| MD5 |
b25105ca5df98d733ee33f0fb42eaa34
|
|
| BLAKE2b-256 |
f9409c0c24be72733047a7b7a317bc1e525393277d252d1c38479ea9a3b54c4b
|