Using with frameworks

There are some integrations in library, however you are not limited to them only. You can create custom integrations for your framework of choice. Some of the integrations are supported by a community, refer to their documentation as well.

Built-in frameworks integrations

Web frameworks

Telegram bots

Tasks and events

Other

aiohttp

aiogram

arq

Click

gRPC

aiogram-dialog

FastStream (com)

Flet (com)

FastAPI

pyTelegramBotAPI

taskiq

ag2 (com)

Flask

Celery

Litestar

RQ (com)

Pyramid (com)

Jobify (com)

Sanic

APScheduler (com)

Starlette (com)

Strawberry (com)

Quart (com)

If you have another framework refer Adding new integrations

See also the real integration examples here.

Common approach

For several frameworks library contains helper functions so you don’t need to control scopes yourself, but just annotate handler/view functions and change application startup code.

To use framework integration you mainly need to do 4 things:

  • call setup_dishka on your container and framework entity

  • add FromDishka[YourClass] on you framework handlers (or view-functions)

  • decorate your handlers with @inject before registering them in framework. Some integrations do not require it, see their details

  • add additional provider to the container to access framework specific objects from your provider.

Note

FromDishka[T] is basically a synonym for Annotated[T, FromComponent()] and is used to get an object from default component. To use other component you can use the same syntax with annotated Annotated[T, FromComponent("X")].

For more details on components see Components and providers isolation

For such integrations library enters scope for each generated event. So, if you have standard scope, than handler dependencies will be retrieved as for Scope.REQUEST. For streaming protocols and websockets you will be also to have SESSION-scoped objects with a lifespan of the whole stream.

Additionally, you may need to call container.close() in the end of your application lifecycle if you want to finalize APP-scoped dependencies.

Some frameworks have their own specific, check corresponding page.

For FastAPI it will look like:

from dishka.integrations.fastapi import FromDishka, inject, setup_dishka, FastapiProvider

@router.get("/")
@inject
async def index(interactor: FromDishka[Interactor]) -> str:
    result = interactor()
    return result

app = FastAPI()
container = make_async_container(your_provider, FastapiProvider())
setup_dishka(container, app)