hey assistent I have this if "toggle_state" not in st.session_state:
st.session_state.toggle_state = "AT"
code
col1, col2 = st.columns([2, 1])
with col1:
all_data = st.checkbox("See Merged Files")
with col2:
mt_selected = st.checkbox(
"MT", value=(st.session_state.toggle_state == "MT")
)
at_selected = st.checkbox(
"AT", value=(st.session_state.toggle_state == "AT")
)
if mt_selected:
st.session_state.toggle_state = "MT"
at_selected = False
elif at_selected:
st.session_state.toggle_state = "AT"
mt_selected = False this dosen;t work it should deslect AT checkbox when MT is on and vice versa. Also I want it on 3 collomns
Assistant
It looks like you're trying to create a toggle system for two checkboxes ("MT" and "AT") in a Streamlit app. When one checkbox is checked, the other should automatically get deselected. Additionally, you want to set up your layout to have three columns.
To achieve the desired functionality, you'd need to ensure that when one checkbox is selected, the other gets deselected in the user interface as well as in your session state. Below is a modified version of your code that implements this logic within a three-column layout:
python
import streamlit as st
# Initialize session state for toggle_state if it doesn't exist
if "toggle_state" not in st.session_state:
st.session_state.toggle_state = "AT"
# Create three columns
col1, col2, col3 = st.columns(3)
with col1:
all_data = st.checkbox("See Merged Files")
with col2:
# Create "MT" checkbox
mt_selected = st.checkbox(
"MT", value=(st.session_state.toggle_state == "MT")
)
if mt_selected:
st.session_state.toggle_state = "MT" # Set toggle_state to "MT"
elif st.session_state.toggle_state == "MT":
st.session_state.toggle_state = "AT" # If MT is not selected and was previously, set toggle_state to "AT"
with col3:
# Create "AT" checkbox
at_selected = st.checkbox(
"AT", value=(st.session_state.toggle_state == "AT")
)
if at_selected:
st.session_state.toggle_state = "AT" # Set toggle_state to "AT"
elif st.session_state.toggle_state == "AT":
st.session_state.toggle_state = "MT" # If AT is not selected and was previously, set toggle_state to "MT"
# Update the checkboxes if toggle_state changed
if st.session_state.toggle_state == "MT":
at_selected = False
elif st.session_state.toggle_state == "AT":
mt_selected = False
# Optionally show the selected state
st.write(f"Current selection: {st.session_state.toggle_state}")
Key Changes:
Three Columns: We create three columns and place the checkboxes in the first two columns, leaving the third column empty or you can add whatever you need.
State Management:
When a checkbox is selected, the corresponding state is set.
If "MT" is checked, "AT" should automatically be deselected by managing the toggle_state. We ensure that this is reflected in the session state of streaming, such that when you check one checkbox again it reflects the correct state.
User Feedback: There’s an optional message displayed at the end showing the current selection for debugging or user feedback.
This approach will ensure that the checkboxes behave as desired, allowing only one to be selected at any one time.