Need help with session-state. its not working properly

I tried to open and close buttons using session-state but there is a problem when i try to open another button the previous button is still open

# Toggle the display of "Add New Tool" section
    if add_tool_clicked:
        session_state.show_add_tool_section = not session_state.show_add_tool_section
        session_state.show_delete_tool_section = False
        session_state.show_modify_tool_section = False
        print("show_add_tool_section:", session_state.show_add_tool_section)

    # Display "Add New Tool" section if toggled
    if session_state.show_add_tool_section:
        st.subheader("Add New Tool") ........
  # Display "DELETE Tool" section if toggled
    with column[1]:
        delete_clicked = st.button("DELETE", key="delete_button")  # Change the key value to "delete_button"
        print("delete_clicked:", delete_clicked)

    if delete_clicked:
        session_state.show_delete_tool_section = not session_state.show_delete_tool_section
        session_state.show_add_tool_section = False
        session_state.show_modify_tool_section = False
        print("show_delete_tool_section:", session_state.show_delete_tool_section)

    if session_state.show_delete_tool_section:
        st.subheader("DELETE Tool") ...........
 with column[2]:
        modify_clicked = st.button("MODIFY", key="modify_button") 
        print("modify_clicked:", modify_clicked)

    if modify_clicked:
        session_state.show_modify_tool_section = not session_state.show_modify_tool_section
        session_state.show_add_tool_section = False
        session_state.show_delete_tool_section = False

    if session_state.show_modify_tool_section:
        st.subheader("MODIFY Tool")
        tools = [row[0] for row in tools_data[1:]]  # Extract tool names from the CSV data
        selected_tool = st.selectbox("Select Tool to Modify",tools,index= None, placeholder="Choose a tool") ...........  

when i click add button then delete button the add session is not closing but when i click delete first then add it works perfectly
is there any change i need to do here

The trick is that you want to update the session state variables at the top of the script, before you actually use them to determine which sections to show.

Here’s a working version of your script, which uses on_click=update_clicked_button to update the session state whenever any of the buttons are clicked.

import streamlit as st
import pandas as pd

column = st.columns(3)

tools_data = pd.DataFrame(
    {
        "tool_name": ["tool1", "tool2", "tool3"],
        "tool_description": ["description1", "description2", "description3"],
        "tool_price": [100, 200, 300],
    }
)

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

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

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


def update_clicked_button():
    if st.session_state["add_button"]:
        st.session_state.show_add_tool_section = (
            not st.session_state.show_add_tool_section
        )
        st.session_state.show_delete_tool_section = False
        st.session_state.show_modify_tool_section = False
    elif st.session_state["delete_button"]:
        st.session_state.show_delete_tool_section = (
            not st.session_state.show_delete_tool_section
        )
        st.session_state.show_add_tool_section = False
        st.session_state.show_modify_tool_section = False
    elif st.session_state["modify_button"]:
        st.session_state.show_modify_tool_section = (
            not st.session_state.show_modify_tool_section
        )
        st.session_state.show_add_tool_section = False
        st.session_state.show_delete_tool_section = False


with column[0]:
    # Toggle the display of "Add New Tool" section
    add_tool_clicked = st.button(
        "ADD", key="add_button", on_click=update_clicked_button
    )

    # Display "Add New Tool" section if toggled
if st.session_state.show_add_tool_section:
    st.subheader("Add New Tool")

# Display "DELETE Tool" section if toggled
with column[1]:
    delete_clicked = st.button(
        "DELETE", key="delete_button", on_click=update_clicked_button
    )


if st.session_state.show_delete_tool_section:
    st.subheader("DELETE Tool")

with column[2]:
    modify_clicked = st.button(
        "MODIFY", key="modify_button", on_click=update_clicked_button
    )
    st.write("modify_clicked:", modify_clicked)

if st.session_state.show_modify_tool_section:
    st.subheader("MODIFY Tool")
    tools = [row[0] for row in tools_data[1:]]  # Extract tool names from the CSV data
    selected_tool = st.selectbox(
        "Select Tool to Modify", tools, index=None, placeholder="Choose a tool"
    )

By the way, you might find a better experience using st.tabs to create Add/Delete/Modify sections of your app.

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