Button state

Hello,

I need that once the choice is made, everything does not disappear (I’ve many more widgets to show, that’s just a toy code). I figured it can be done using session_state, but I couldn’t implement it. Can someone help me? Thank you

if st.button('choose'):

            choice= st.radio("pick one",
                    ('hello', 'ciao', 'grüez'))
            st.write(choice)

Here’s one way to accomplish it, by essentially making the button into a toggle for an entry in session state, and then checking that session state to decide whether to show the other widgets.

import streamlit as st

if "button_pressed" not in st.session_state:
    st.session_state["button_pressed"] = False

# Make button function as a toggle.
if st.button("choose"):
    st.session_state["button_pressed"] = not st.session_state["button_pressed"]

if st.session_state["button_pressed"]:
    choice = st.radio("pick one", ("hello", "ciao", "grüez"))
    st.write(choice)
1 Like

thank you!

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