Nested Button

Hello, I am having trouble figuring out how to only display the second button labeled “begin” if the initial “yes” button was pressed. Also when I re-run my app all three buttons (“yes”, “no”, and “begin” appear when I just want “yes” and “no”)
I have very limited coding experience so any tips would be great!

here’s my code:

  st.write("Would you like to begin data collection?")

#Initialize state
if "button_clicked" not in st.session_state:    
    st.session_state.button_clicked = False
def callback():
    #Button was clicked
    st.session_state.button_clicked = True

if (    
    st.button("Yes", on_click = callback)     
    or st.session_state.button_clicked   
    
):     
    if st.button("Begin"):    
        st.balloons()
                
if st.button("No"):
    
    alert = st.warning("Please go back to the main page") # Display the alert
    time.sleep(3) # Wait for 3 seconds
    alert.empty() # Clear the alert

Session_state and callbacks can be a bit tricky at the beginning. Session_state is very useful, but callbacks at the beginning might not be necessary. You can achieve different options without them, but it’s not recommended unless you enjoy having a headache

Here the different options :

# Initialize session state
        if "button_clicked" not in st.session_state:
            st.session_state.button_clicked = False

        def yes_callback():
            # "Yes" button was clicked
            st.session_state.button_clicked = True

        def no_callback():
            # "No" button was clicked
            alert = st.warning("Please go back to the main page")
            time.sleep(3)
            alert.empty()

        def begin_callback():
            st.balloons()
            st.session_state.button_clicked = False

        st.write("Would you like to begin data collection?")

        if not st.session_state.button_clicked:
            st.button("Yes", on_click=yes_callback)
            st.button("No", on_click=no_callback)

        else:
            st.button("Begin", on_click=begin_callback)

One function, one callback just to be sure to apply what we want to.

You can achieve the same :

if "button_clicked" not in st.session_state:
    st.session_state.button_clicked = False

st.write("Would you like to begin data collection?")

# Display "Yes" and "No" buttons if "Yes" has not been clicked
if not st.session_state.button_clicked:
    if st.button("Yes"):
        st.session_state.button_clicked = True
    if st.button("No"):
        alert = st.warning("Please go back to the main page")
        time.sleep(3)
        alert.empty()

        # Display "Begin" button if "Yes" has been clicked
        elif st.session_state.button_clicked:
            if st.button("Begin"):
                st.balloons()
                time.sleep(3)
                st.session_state.button_clicked = False
                st.rerun()  # Rerun the script to reset the button.

Thank you so much. How would I re-arrange the code if I wanted to just display the begin button, then have the yes and no buttons after the begin button is clicked with the ballons popping up if the user clicks the yes button?

1 Like

I figured it out, I guess I can finesse a little :joy:


  if "button_clicked" not in st.session_state:
              st.session_state.button_clicked = False
  
  def begin_callback():
              # "Yes" button was clicked
              st.session_state.button_clicked = True
  
  def no_callback():
              # "No" button was clicked
              alert = st.warning("Please go back to the main page")
              time.sleep(3)
              alert.empty()
  
  def yes_callback():
              st.balloons()
              st.session_state.button_clicked = False
  
  st.write("Would you like to begin data collection?")
  
  if not st.session_state.button_clicked:
              st.button("Click Here to Begin Data Collection", on_click=begin_callback)
            
           

else:
            st.button("yes", on_click=yes_callback)
            st.button("no", on_click=no_callback)
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.