Getting access to `app.state` from starlette mounted streamlit app.

Hi all. I am struggling to access app.state from a starlette streamlit mounted app. I set the app state in the lifespan function that I pass over to the streamlit app which I then mount from starlette. However, the data I set in the lifespan I can’t seem to find a way to access from streamlit. Anybody try this out yet? Am I missing something?

Welcome to the community and thanks for your question! :balloon: It seems likely that when you use the new experimental ASGI/Starlette integration (e.g., via streamlit.starlette.App), you can set app.state in your lifespan function, but Streamlit scripts themselves do not have direct access to the Starlette app.state object. The Streamlit script runs in its own context and does not expose the Starlette app instance or its state to your Streamlit code. This is not documented as a supported feature, and there’s no official API to access app.state from within your Streamlit script according to the available docs and PRs for the ASGI integration.

If you need to share data between the Starlette app and your Streamlit script, you may need to use another mechanism, such as environment variables, a shared database, or a custom solution. There is no documented way to access Starlette’s app.state from inside the Streamlit script itself—this limitation is noted in the experimental ASGI app PR and related discussions. If you have a minimum reproducible example, please share it so others can help debug or suggest workarounds!

Sources: