Buttons do not invoke rerun of script

I am trying to have a sidebar that offers a choice between images on the main screen. Pressing one of the sidebar buttons should set a value in a dataframe and move on to the next set of images. This moving on doesn’t happen unless I press another key, which is confusing because the visuals are lagging then.

Luckily, I could reduce my script to a few lines that demonstrate the problem:

import streamlit as st
import st_state_patch # to avoid reinitialising values after every user action

Initialise the value of file_index, but only when it hasn’t done so earlier in this session:

s = st.State()
if not s:
s.file_index=0 # and s.file_index = the number initially set to zero and should increase every time I press the b1.button

print(str(s.file_index)) # just to show its behaviour (see console)

my_placeholder_img_nr = st.sidebar.empty()
my_placeholder_b1 = st.sidebar.empty()

my_placeholder_img_nr.text("file number: "+str(s.file_index))

Next, the action button to increase s.file_index with 1
This button DOES have an action, even one that is changing a variable.
It should therefore rerun the script, but it doesn’t.
However, if I press the Refresh button, the action of the b1 button become visible
It seams the b1 button performs its actions, but they’re “cached” or “withheld” in some way:

if my_placeholder_b1.button(’----------- Counter + 1 -----------’):
s.file_index+=1

Now: when inserting a refresh button, surprise!
Pressing this button IS rerunning the script,
Although there is no real action defined for a keypress of this button:

dummy2=st.sidebar.button(’---------- ⟳ Refresh ⟳ ----------’)

1 Like

Setting a session state based on button clicks should help.