Skip to main content

Quart integration for dishka

Project description

Quart-Dishka: Dishka integration for Quart

PyPI version Supported versions License

Integration of Dishka dependency injection framework with Quart web framework.

Features

  • Automatic Scope Management: Handles REQUEST and SESSION scopes for HTTP and WebSocket requests
  • Dependency Injection: Injects dependencies into route handlers via:
    • Auto-injection mode for all routes
    • @inject decorator for manual setup
  • WebSocket Support: Full support for WebSocket handlers with proper scoping
  • Blueprint Support: Works with Quart blueprints out of the box

Installation

Install using pip:

pip install quart-dishka

Or with uv:

uv add quart-dishka

Quick Start

from quart import Quart
from dishka import Provider, Scope, provide, make_async_container, FromDishka
from quart_dishka import QuartDishka, inject

# Define your providers
class StringProvider(Provider):
    @provide(scope=Scope.REQUEST)
    def greeting(self) -> str:
        return "Hello"

# Create Quart app and Dishka container
app = Quart(__name__)
container = make_async_container(StringProvider())

# Initialize extension
QuartDishka(app=app, container=container)

# Use dependency injection in routes
@app.route("/")
@inject
async def hello(greeting: FromDishka[str]) -> str:
    return f"{greeting}, World!"

if __name__ == "__main__":
    app.run()

Usage

Method 1: Auto-Injection Mode

Enable automatic dependency injection for all routes:

from quart import Quart
from dishka import FromDishka, make_async_container
from quart_dishka import QuartDishka

app = Quart(__name__)
container = make_async_container()
QuartDishka(app=app, container=container, auto_inject=True)

# No @inject decorator needed
@app.route("/")
async def hello(greeting: FromDishka[str]) -> str:
    return f"{greeting}, World!"

Method 2: Manual Injection

Use the @inject decorator for specific routes:

from dishka import FromDishka
from quart import Quart
from quart_dishka import inject


app = Quart(__name__)


@app.route("/")
@inject
async def hello(greeting: FromDishka[str]) -> str:
    return f"{greeting}, World!"

WebSocket Support

from dishka import FromDishka
from quart import Quart, Websocket
from quart_dishka import inject


app = Quart(__name__)


@app.websocket("/ws")
@inject
async def websocket(ws: FromDishka[Websocket], greeting: FromDishka[str]):
    await ws.accept()
    await ws.send(f"{greeting} from WebSocket!")

Factory Pattern

from dishka import make_async_container
from quart import Quart
from quart_dishka import QuartDishka

container = make_async_container()
dishka = QuartDishka(container=container)

def create_app():
    app = Quart(__name__)
    dishka.init_app(app)
    return app

Blueprint Support

from dishka import FromDishka
from quart import Blueprint, Quart
from quart_dishka import inject

app = Quart(__name__)
bp = Blueprint("example", __name__)


@bp.route("/hello")
@inject
async def hello(greeting: FromDishka[str]) -> str:
    return greeting


app.register_blueprint(bp)

Requirements

  • Python 3.10+
  • Quart >= 0.20.0
  • Dishka >= 1.4.0

More Examples

Check out the examples directory for more detailed examples:

  • Basic HTTP routes
  • WebSocket handlers

Supported by

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