Eagerly execute specific functions on app launch

Summary

My dashboard downloads data files which are refreshed every day. This takes about a minute. Is there a way within streamlit to easily call specific functions when the app launches, rather than having the user wait for the initial download?

Similarly, are there any recommended methods to ensure cached data provider functions are called/refreshed on a regular interval.

Would apscheduler or similar work within streamlit?

If you are deploying on Streamlit Cloud you are more limited than if you have control over the infrastructure serving your app.

If you have control over the infrastructure, you could use whatever scheduling was available within that system.

As for Streamlit Cloud, the only recommendation I have is to schedule some computer somewhere to connect to your app at 1 AM, so that it can do any of your more expensive computations that you want to cache. (For example, if you have a get_data(date) function that is cached, then by passing in the date, it will use the cache until the date changes…modulo some cleanup depending on how much data is being cached and how long your app runs between reboots.)

I made an example of a script that connects to a Streamlit app. This could be packaged up in a bat file and scheduled for instance.

https://mathcatsand-examples.streamlit.app/run_by_script

Hey Debbie! I’m looking to host this as a service within our docker swarm. Using selenium to trigger streamlits internal refresh is smart.

Do you know of any way to do it without external scripts or similar?

I’m most worried about service bloat - having one service which uses streamlit run to serve the dashboard and something internal to refresh it would be more elegant than one service to serve, and another to trip the refresh.

If there isn’t something, I wonder if the team would be open for a FR or PR to implement it

I don’t have anything slicker than the Selenium script. That can live most anywhere, even the same service offering up the Streamlit app if it’s a service you control.

I know I have heard other mentions on the forum about people wanting to schedule their app to do things autonomously in various situations. If it’s just updating files outside of Streamlit, that can be scripted anywhere. So the biggest argument for a Streamlit feature specifically (that I know of) is wanting to get the cache updated on some regular basis so the first user of the day isn’t hit with a wait time.

I did a quick search of Issues on GitHub, but didn’t find it posted there. I have definitely read posts touching on it in the forum, though. Streamlit staff are very happy to get feature requests recorded in GitHub and I think options within the config file to specify some autonomous execution would be nice, specifically targeting optimal cache performance.

Great idea, I’ve raised this as FR at Provide methods to refresh caching · Issue #5871 · streamlit/streamlit · GitHub

1 Like

Gave it a thumbs up on GitHub. :+1:

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.