Polling API in background for status change

I am trying to fetch the status of a task from an API and want to show the current status automatically whenever it changes. So I would like to poll the API every 10secs without re-running the application.
I currently have a refresh button that checks the status, but the whole page loads whilst it carries out its query and you then can’t use the rest of the app in the 3 sec period, so re-querying this button on repeat doesn’t work for a user.

Button at end of the script, so it re-runs when this is queried.
if st.sidebar.button(‘check status’):
fetched = request.get(url, headers=headers)

This type of thing is best implemented as a custom component. It’ll run independently of Streamlit’s execution cycle.

You could also take a look at @andfanilo’s example for motivation.

1 Like