Form button not working

Buttons won’t remember they were clicked with the next user action. If you need to nest buttons, then you’ll need to use session state. You can set buttons to record information into session state with a callback function, then set your conditionals to check that information in session state.

I pasted some sample code in a similar thread linked below. (The conditionals in this snippet could easily be nested or not as written; whatever your case requires.)

if 'stage' not in st.session_state:
    st.session_state.stage = 0

def set_stage(stage):
    st.session_state.stage = stage

# Some code
st.button('First Button', on_click=set_stage, args=(1,))

if st.session_state.stage > 0:
    # Some code
    st.button('Second Button', on_click=set_stage, args=(2,))
if st.session_state.stage > 1:
    # More code, etc
    st.button('Third Button', on_click=set_stage, args=(3,))
if st.session_state.stage > 2:
    st.write('The end')
st.button('Reset', on_click=set_stage, args=(0,))
3 Likes