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
Depends
that 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
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
File details
Details for the file flask_dependency-0.1.1.tar.gz
.
File metadata
- Download URL: flask_dependency-0.1.1.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.12.3 Linux/6.8.11-200.fc39.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27c7934d5add6eb0279f42d9b2a4a49b502e9ad78195eb4ad58b7e0128a4696b |
|
MD5 | 72f6d9ed74c25a8045cfd5c7172135e7 |
|
BLAKE2b-256 | ce4e6ce97f8ec04fff3b704e25712b2b6b60b52fb5667fa6dd41f9184cc03543 |
File details
Details for the file flask_dependency-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: flask_dependency-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.12.3 Linux/6.8.11-200.fc39.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03c93f2ab9d91825afcc9ec35e0a0aaa15e4572c62cf4ed7564a7bef638667af |
|
MD5 | 8dc96022dea954619ff6bf121e69b559 |
|
BLAKE2b-256 | 6d0ad694ca2c49e0787f779e9445532a5ccfcc513aa1f1df3f80c4eb08bb4bc5 |