I am testing a very simple streamlit app with a toggle and a button. Specifically, I want to use st.toggle
and have a st.button
that, when clicked, switches this toggle off. The toggle is on by default.
The problem I am facing is that the button works fine to disable the toggle on the first click. Now if I turn the toggle on manually and click the button, it does not switch the toggle off. Further, turning the toggle on/off manually is taking two mouse clicks. Is there something about session_state I am missing in my code? Thanks for any advice!
Code below with comments. Deployed locally. Python v3.12 and Streamlit v1.34.
import streamlit as st
st.title('Streamlit Toggle App')
# Initialize or retrieve the current state of the toggle
if 'toggle' not in st.session_state:
st.session_state.toggle = True # Default to on
# Function to switch off the toggle
def switch_off_toggle():
st.session_state.toggle = False
# Display the toggle with its current state
toggle = st.toggle("Toggle", value=st.session_state.toggle)
# Display the button, which when clicked, will call the function to switch off the toggle
btn = st.button("Switch Off Toggle", on_click=switch_off_toggle)
# Update the toggle state based on user interaction
st.session_state.toggle = toggle
# Optional: Display the current state of the toggle
st.write("Toggle is currently:", "On" if st.session_state.toggle else "Off")