Flask adapter¶
pip install pydepi-flask — import as depi_flask. Usage guide:
Flask integration.
FlaskInjector
¶
FlaskInjector(provider: ServiceProvider, param_name: str = 'provider', autowire: bool = False)
Bases: BaseInjector
Per-request dependency scopes for a Flask app.
Example
injector = FlaskInjector(provider) injector.setup(app)
@app.route('/data/
With autowire=True, registered types are resolved by annotation instead::
injector = FlaskInjector(provider, autowire=True)
@app.route('/data/<key>')
@injector.inject
def get_data(key, service: DataService):
return service.get(key)
key is left for Flask to fill from the URL rule, because DataService is
registered and str is not.
Parameters:
-
provider(ServiceProvider) –the built ServiceProvider to resolve from.
-
param_name(str, default:'provider') –the keyword argument the request scope is passed as in the default (non-autowire) mode. Name it to taste --
containeris a common alternative. -
autowire(bool, default:False) –when True,
injectresolves registered types by annotation instead of passing the scope. Not supported by every integration; FastAPI rejects it outright.
Source code in packages/depi-core/depi/integration.py
setup
¶
Install before/teardown request hooks on app.
Source code in packages/depi-flask/depi_flask/__init__.py
create_scope
¶
create_scope() -> ServiceScope
current_scope
¶
current_scope() -> ServiceScope
Return the scope bound to the current request context.
Raises:
-
NoActiveScopeError–if setup(app) never ran, or this is called outside a request.
Source code in packages/depi-core/depi/integration.py
inject
¶
Decorator handing the request scope to fn.
Default mode passes the scope as the param_name keyword argument.
With autowire=True, parameters annotated with registered types are
resolved and passed individually instead.
Uses functools.wraps, so it composes inside a decorator stack (route registration, auth, response handling) without losing the wrapped function's identity.