Custom init on 'streamlit run' before first page load?

I’d like to run some initialization code for my multi-page Streamlit application before the first page load. Is that possible?

The initialization logic only needs to run once, at service start (streamlit run app_init.py). I’m aware of @st.cache_resource, but the issue is that the caching can only be triggered by a page load. My app is headless, so I’d like to just set things up before the first page load. I guess I can open the page myself or send a request to warm up my app, but not great.

It would be amazing if the Streamlit server had a simple hook for us to define basic logic that runs once, on startup, such as loading environment variables, parsing configs, setup my database connections, load caches, wire up classes with DI etc.

To configure a Streamlit application to start with a specific page, you can use the pages/ folder or the st.page_link function. You can also try the st-pages module.

Thanks, though loading a specific page is not my issue.

I want to call @st.cache_resource when I start the server, before anyone opens any streamlit page.

I must have missed this when I asked, but it looks like the new st.navigationpattern lets us create an application entry file that acts as a router to all other pages. This is great! This lets me set the page config once, as my first streamlit call, then wire up the rest of my application with long-lived cached resources, then invoke whatever page we need to go to.