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

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

quart_dishka-1.0.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quart_dishka-1.0.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file quart_dishka-1.0.0.tar.gz.

File metadata

  • Download URL: quart_dishka-1.0.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for quart_dishka-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fe3c3d90990fd3d491098d568d54dad69a96f163cd410207956e58a2d69206e1
MD5 646661cf9381f8315f9b7f43c06edb80
BLAKE2b-256 dbff6c366bee430f0cd6cf3353be24ba93dfad4fad48e4eb560192d644eb2302

See more details on using hashes here.

File details

Details for the file quart_dishka-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: quart_dishka-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for quart_dishka-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9560602b24c569ec5d52a978a14d95ec9172ca2c596b48a0bc97e66294110fd1
MD5 20c1815d7a324019ea713bc858d75bae
BLAKE2b-256 1eb97dd1c59ea7245cd985938ec636b9f07fd6223528b2a9c5d8a1b45640debe

See more details on using hashes here.

Supported by

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