Skip to content

How to pass arbitrary data to context #1034

@cirezd

Description

@cirezd

Hello, I am looking for a way to inject or pass arbitrary data to the context object. Similar to what is possible in fastapi via app.state or via dependencies:

from fastapi import FastAPI

app = FastAPI()

app.state.custom_context = {"a": "b"}

# In separate file

router = Router()

@router.get("/items/{item_id}")
async def read_items(item_id: str, request: Request):
    example_context = request.app.state.custom_context
    return items[item_id]

My use case is that I have the handlers in separate files and I would like to pass general run information to the handlers, without the need for database requests. I am aware of user_data, but I am not sure this is appropriate here, since it is more tied to the Request and not the overall run. Furthermore, I don't see a straight-forward way to pass the data from the start_urls? Is it possible to abuse the pre_navigation_hook or something similar?

Any guidance would be helpful. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    t-toolingIssues with this label are in the ownership of the tooling team.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions