kmcgrady / streamlit-autorefresh

An autorefresh component for Streamlit apps
MIT License
176 stars 16 forks source link

Refresh Not Pulling New Data #5

Closed andrew-mitchell-721 closed 1 year ago

andrew-mitchell-721 commented 2 years ago

Thanks for this great component! I'm having a problem and not sure if it's user error or expected behavior. Trying to refresh every 5 minutes. I have a module within my app that defines a few constants like:

NOW = datetime.now().astimezone(tz=timezone('US/Mountain')).replace(tzinfo=None)

I'd like to refresh the value of NOW each time the script reruns, as another part of the module completes an API recall based on the value of NOW. However, when I instantiate st_autorefresh, the reload doesn't update the value of NOW upon refresh.

Am I missing something? Thanks!

andrew-mitchell-721 commented 2 years ago

Never mind! Solved it by storing now in st.session.state and refreshing it on each reload:

count = st_autorefresh(interval=3000, key='counter')

if 'now' not in st.session_state:
    st.session_state.now = datetime.now().astimezone(tz=timezone('US/Mountain')).replace(tzinfo=None)
else:
    del st.session_state.now
    st.session_state.now = datetime.now().astimezone(tz=timezone('US/Mountain')).replace(tzinfo=None)