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.
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.