Sidebar button doesn't work

Hi, I have a problem when creating sidebar button. When I click analysis, it will show “Data loading” and “Visualizations” button. But when I click “Data loading” or"Visualizations" button, it will return to “Analysis” button. I have try to use session_state, but it doesn’t work. Could anyone help me solve this problem? Thank you in advance!

if st.button('Analysis'):
    if sidebar.button("Data loading"):
        show_basic_statistics()

    elif sidebar.button("Visualizations"):
        selected_subcontent = sidebar.radio("Select:", ["OptionA", "OptionB"])
        if selected_subcontent == "OptionA" :
            st.header('OptionA')
            opt = ["Option1", "Option2", "Option3"]
            option = st.selectbox("Select an option", opt)
            if option == Option1":

Using Session_state:

if "Data loading" not in st.session_state:
    st.session_state["Data loading"] = False
if "Visualizations" not in st.session_state:
    st.session_state["Visualizations"] = False
if st.button('Analysis'):
    if sidebar.button("Data loading"):
        st.session_state["Data loading"] = True
        show_basic_statistics()
        show_data_preview()
    if st.session_state["Visualizations"]:
    if sidebar.button(" Visualizations"):
        st.session_state["Visualizations"] = True
        selected_subcontent = sidebar.radio("Select:", ["OptionA", "OptionB"])

Button don’t have state. They return True on the page load resulting from their click and then immediately go back to False. If you nest anything inside a button output, then it will go away with the next user interaction.

Check out this blog post for some info:

For more examples… :grin:
https://discuss.streamlit.io/search?q=%40mathcatsand%20%22buttons%20don%27t%22

You may have to use something else to be your navigator, see a simple trick you can expand on

import streamlit as st
from streamlit import sidebar
if "clicked" not in st.session_state:
    st.session_state["clicked"] = False

if st.session_state.clicked or st.button('Analysis'):
    st.session_state.clicked = True
    if sidebar.button("Data loading"):
        show_basic_statistics() 
         #st.session_state["clicked"] = False # If you want to go back, you have to set false then click 

    elif sidebar.button("Visualizations"):
        selected_subcontent = sidebar.radio("Select:", ["OptionA", "OptionB"])
        if selected_subcontent == "OptionA" :
            st.header('OptionA')
            opt = ["Option1", "Option2", "Option3"]
            option = st.selectbox("Select an option", opt)
            if option == Option1":

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