Hi!
I have an app that fetches data from a SQL database. The data is collected and used to create dashboards. The thing is that the database is constantly updated and I need the data in my app to update every 10 minutes to keep up. I’ve been using this app for the past two months and the only way I found to solve this is to manually rerun the app in my VsCode terminal (streamlit run app.py). I have already tried infinite loops for rerun and the autorefresh tool. But as far as I understood, the data only changes when I restart the app in a new terminal, not on a rerun.
My best guess is that if you rerun the app and the data doesn’t refresh, you might need to tweak the ttl so that your data cache expires. For example, if you’re using st.connection st.connection - Streamlit Docs, you might pass ttl=timedelta(minutes=10) to make the cache expire after 10 minutes.
However, that won’t make your app automatically rerun, it will just mean that if it does rerun, it should fetch new data if 10 or more minutes have passed since the last run.
If you want to make your app automatically fetch new data every few minutes, you might use the new experimental_fragment⚡️ Launched in 1.33: st.experimental_fragment, which has a run_every argument that makes a function rerun on a schedule.
I’m not using st.connection. I’m just connecting with sqlalchemy using an engine.
For the data cache I’m using st.cache_data(ttl=600). For the app refresh, I’m using autofresh. This way, I thought that the data would be cached for 10 minutes and after those 10 minutes the app would refresh and collect new data. That should’ve worked, right?
Anyway, I’m going to read about st.connection and see how it works, I might implement it with the experimental feature.
Yeah, that should work in theory. I’m not 100% sure how the autorefresh component works with st.cache_data, but I would have expected it to work. I think you might get a more reliable experience with st.experimental_fragment.
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.