Skip to main content

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

  1. 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.

  2. 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.

  3. 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

flask_dependency-0.1.1.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distribution

flask_dependency-0.1.1-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

Supported by

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