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!
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?
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)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.