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.