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.
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.