Skip to content

How to pass arbitrary data to context #1034

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions