Streamlit Autorefresh

Hi,

Actually the component loading is fine. Currently my issue is dependency on the browser. I have to keep it open and hence my laptop can’t shut down as well. Is there a way to open the tab from a terminal background.

I want to update my app on Streamlit every 30 minutes. This update includes calling an API to update the dataset on server side.

For now I should check something like below when user enters the page:

 if (last_update - now > 30*60): do_update()

In that case you would do something like the following

st_autorefresh(interval=30*60*1000, key="api_update")

That way the Streamlit app does not need a loop or time.sleep to keep updating.

1 Like

But I think with this, user should stay on that page. I need the update should be run server side every 30 minutes. Is this possible?

@MAmrollahi

So there’s two solutions I can think of: an easy one, and a harder one.

The easiest one is using st.experimental_memo with a ttl. This will allow every new rerun past a certain time to extract the contents. Depending on how “up to date” the frontend has to be, you can use autorefresh every minute and have the ttl every 30 minutes, so it’s at most a minute delayed…or you can do 15 minutes and it’s at most 15 minutes delayed etc. A well-cached (or memo’ed) information makes a rerun look seamless, so this would be my preferred approach.

The would not work if the effort is extremely time consuming or if you are really looking for very up-to-date information. There’s possible solutions to modify streamlit-autorefresh to have it call on the time marker (every half hour for example) and the ttl and get close and at least sync more clearly. The other option is to run some background job on a thread, but Streamlit is not super well-designed for that.

Hope that helps!

1 Like

I posted something related to this topic here: Best (current) practice for spawing a process to run independent of Streamlit? - #2 by asehmi

Hi @kmcgrady,

I saw this and wanted to use it for my app, which requires a live update every few minutes. However, I get this message when I try to run your trivial example.

Any ideas on how to bypass this? Thanks!

Hi @kmcgrady

I am getting this error when using autorefresh. My frequency is 1 sec.

The autorefresh works well for a while, then it starts giving the following error about “too many open files”, with the file name being the url that I have open. If it matters any, I am using the “pages” feature of Streamlit, and the url is one of the pages in question.

The other strange thing is that, this works fine on my local session of Streamlit, but when I push it to our AWS EC2 instance, the error starts appearing.

Hope you will have time to respond. Thank you very much.

streamlit version 1.17.0

2023-01-24 19:23:05.090 Exception in callback BaseAsyncIOLoop._handle_events(10, 1)
handle: <Handle BaseAsyncIOLoop._handle_events(10, 1)>
Traceback (most recent call last):
File “/usr/lib/python3.7/asyncio/events.py”, line 88, in _run
File “/opt/myapp/lib/python3.7/site-packages/tornado/platform/asyncio.py”, line 206, in _handle_events
File “/opt/myapp/lib/python3.7/site-packages/tornado/netutil.py”, line 267, in accept_handler
File “/usr/lib/python3.7/socket.py”, line 212, in accept
OSError: [Errno 24] Too many open files

Hey @HHest

Hmmm, based on the error you are providing, it seems like it’s a web socket problem. Let me see if my colleague knows more what’s going on.

Thank you very much @kmcgrady. I’ll try to replicate with a small example as well.

Hi @kmcgrady, will this solve the auto refresh issue of app on streamlit cloud? If I add st_autorefresh(interval=8*60*60*1000, key="no_update_for_8h"), will this avoid auto refresh issue for 8h for the deployed app on cloud?

Hey @SqTao .

Assuming you do not use the component, this would not fix the auto refresh issue. This might be a Streamlit cloud challenge. I believe the issue got a comment that explains this.

Sorry this does not solve your challenge! :frowning:

1 Like

Hi @kmcgrady
I know this was design for streamlit, which I was using with great happiness, but now I started using it with plotly-dash, and it makes the color of the bars black, when I remove it, the colors are back, I am working on Python 3.9.

Any idea why that would be?
Thanks

That’s really awesome! Thanks for sharing will look into this idea

Hey @arafeek . I have not played with plotly dash, so I do not know. Is there a way for Plotly Dash to support Streamlit custom components? I wasn’t aware of any way. If you can point me to a resource, I am happy to look into it.

Hi again,
Thanks for the prompt response, I actually just discovered that this will happen simply by importing the module, all you need is to add

from streamlit_autorefresh import st_autorefresh

After that, you will be stuck, restarting the kernel does not reset the colors, nor deleting the variables. I am using Spyder (the latest) with Python 3.9, and the only way to have the colors back is by starting a new file and copy the code (without this import)

That is so weird, but honestly, I don’t think it will properly work in plotly dash. This component sends a streamlit specific message to the frontend, and the autorefreshing is triggered from the frontend. It even requires Streamlit under the hood. I cannot see (from my searching) how it would work properly (impressed it won’t even error).

I wish I could help. I’d probably look at the Plotly Dash’s Live Updating mechanism

Amazing!!!!!

hi @kmcgrady
I am developing a streamlit application that displays data that changes for every n minutes i used st_autorefresh to rerun my code after that n minute interval , but if there is any user_interaction between that n minutes (suppose take the user-interaction was at n-2 minutes) the code is being runned at n minutes after the user interaction and not at n before the user interaction , can anyone please provide a solution for this,

appreciate any replies
Thank you

1 Like

Hi all!

I’ve been using this component for some months now for a real-time dashboard i’ve been working on and it has been really helpful.

One thing I noticed, is how memory consumption increases as the interval parameter decreases. When the refresh rate is set to 1 min, memory consumption is around 150 mb on average no matter how long the app is running. When the dashboard refreshes every second, the memory consumption increases very fast. In about 4-5 hours it has reached close to 1.5 gb of memory consumption.

I’m trying to debug the issue, but I’m not sure where to start. Any ideas? The app is collecting data. It would be really helpful if the dashboard refreshed every second and let it run for days.

Maybe the python garbage collector doesn’t release memory when the dashboard refreshes too often (?)

Last, whenever I close the chrome tab and open a new one, the memory consumption goes down to 100-120 mb and the cycle repeats itself.