kmcgrady / streamlit-autorefresh

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

not causing a re-render event #14

Closed rdaigle007 closed 8 months ago

rdaigle007 commented 9 months ago

Hi! For background I am a total streamlit noob. But, I'm learning quick, it is rather simple.

I have a problem I thought following call would help:

    st_autorefresh(interval=1000, limit=1, key="autorefresh_cnt")

I have a pretty simple streamlit app. There occurs times when user takes some action like press a button that causes what I call a "render" event where the streamlit main app is invoked again from scratch. Great! However, there are times in the code where I change some state and I'd like the script to be called again (what I call a re-render). st.rerun() doesn't work (as it seems to messup some of my state in st.session_state.

I thought the above call to st_autorefresh() would cause script to be run again in 1 second. But script is never executed again.

My streamlit app is a simple 1 file, single page app. Just want to cause the script to be run again preserving all of st.session_state.

kmcgrady commented 8 months ago

Hey @rdaigle007 ! Sorry for the delay with the holidays! I think the problem has to do with the key. When you send the same arguments down, Streamlit associates it with the same component, and so it does not reset the counter (which in this case is 1). You will need to change the key somehow. The easiest way is to add some field in session state.

import streamlit as st

if 'refresh_key' not in st.session_state:
  st.session_state = False

if some_criteria_is_match:
  st.session_state.refresh_key = not st.session_state.refresh_key

 st_autorefresh(interval=1000, limit=1, key=str(st.session_state.refresh_key))

I think I should implement some sort of solution to #10 would solve your problem in the future, so I'll think of a solution there. Going to close this issue but feel free to respond if you come into more issues.