Updating a widget label via st.session_state()

Initializing Session States

if ‘counter’ not in st.session_state:
st.session_state.counter = 0
if ‘start’ not in st.session_state:
st.session_state[‘start’] = False
if ‘stop’ not in st.session_state:
st.session_state[‘stop’] = False
if ‘refresh’ not in st.session_state:
st.session_state[‘refresh’] = False
if “button_label” not in st.session_state:
st.session_state[‘button_label’] = “START”

Function to update current session

def update_session_state():
if st.session_state.counter==1:
st.session_state[‘start’] = True
st.session_state[‘button_label’] = “SUBMIT”

elif st.session_state.counter == 2:
    # Set start to False
    st.session_state['start'] = False
    # Set stop to True
    st.session_state['stop'] = True
    # Rename button text
    st.session_state['button_label'] = "RELOAD"

elif st.session_state.counter == 3:
    # Deactivate start & stop by setting them to False
    st.session_state['start'] = st.session_state['stop'] = False
    # Activate refresh to True
    st.session_state['refresh'] = True
    st.session_state.clear()

def update_label():
if st.session_state.counter==0:
st.session_state[‘button_label’] = “START”
elif st.session_state.counter==1:
st.session_state[‘button_label’] = “SUBMIT”
elif st.session_state.counter==2:
st.session_state[‘button_label’] = “RELOAD”

Session States @ the start

st.write(“Before Button Press:”, st.session_state)

Initializing Button Text

button = st.button(label=st.session_state[‘button_label’],
key=‘button_press’,
on_click=update_label)

if button:
st.session_state.counter +=1
update_session_state()
# st.rerun()

btn_text = st.session_state.button_label
st.write("Current label should be ", btn_text)

nl(3)
st.write("current states on button press:", st.session_state)

I am having to click the button widget twice before it updats and the RELOAD logic returns a key error. I really need to figure out how to update that button accurately. Anyone who can help please?

Hi @fesomadealli, try this simple code and modify it to your use:

ms = st.session_state
if 'counter' not in ms: ms.counter = 0
if 'btn_txt' not in ms: ms.btn_txt = ['Start', 'Submit', 'Reload']

def btn_click():
    ms.counter += 1
    if ms.counter > 2: ms.counter = 0

st.button(ms.btn_txt[ms.counter], on_click=btn_click)

Cheers

Thank you very much @Shawn_Pereira

While hoping for a response, I had initially used:

# Function to update current session
            def update_session_state():
                if st.session_state.counter == 1:
                    st.session_state['start'] = True
                    st.session_state['button_label'] = "SUBMIT"
                    st.session_state.current_quiz = random.sample(quiz.sport_questions, 10)

                elif st.session_state.counter == 2:
                    # Set start to False
                    st.session_state['start'] = True 
                    # Set stop to True
                    st.session_state['stop'] = True
                    # Rename button text
                    st.session_state['button_label'] = "RELOAD"

                elif st.session_state.counter == 3:
                    # Deactivate start & stop by setting them to False
                    st.session_state['start'] = st.session_state['stop'] = False
                    # Activate refresh to True
                    st.session_state['refresh'] = True
                    st.session_state.clear()

if button:
            st.session_state.counter += 1
            update_session_state()
            with st.spinner("*this may take a while*"):
                time.sleep(2)
            st.experimental_rerun()

It worked but I wasn’t very confident on the experimental_rerun() approach.
Your solution works very well too and it’s quite simple.
I am trying to build a Quiz App and really thank you for your assistance.