Skip to content

Accessing the app in Depends #3201

@nicolaerosia

Description

@nicolaerosia

I would like to access the FastAPI app in a Depends function, is there a way to do that?
The rationale here is that I would like to avoid global variables and use the state in app.

Example usecase:

def get_session(app: fastapi.FastAPI) -> Session:
    [...]
    access the app.state here
    [...]

@router.post("/init")
def create(session: Session = Depends(get_session)):
[...]
use the SQLAlchemy session
[...]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions