If you’re creating a debugging post, please include the following info:
running locally,
no public link
N/A
N/A
python 3.12.3 , streamlit 1.35.0
this code should call a function every time a button is clicked to toggle session_state.clicked between true and false
it runs once, but doesn’t run when called again. Any ideas why this is responding in this manner?
import streamlit as st
if 'clicked' not in st.session_state:
st.session_state['clicked'] = False # clicked initialized to False
def click(): # this should toggle clicked between True and False
if st.session_state.clicked == False:
st.session_state.clicked = True
else:
st.session_state.clicked = False
st.session_state # monitor session state
st.button("num1",key='num3', on_click=click)
if st.session_state.clicked:
st.write("in if 1")
click()
st.button("num2",key='num2', on_click=click)
if st.session_state.clicked:
st.write("in if 2")
click()
It can help if you add a little more debugging, specifically you can add st.write("IN CLICK") inside the click function – the basic answer is because “click” ends up being called twice (once because of the on_click, and then once again because you’re checking to see if it’s now True with your two if statements), and so it toggles it from false to true, then back to false, and it ends up back where it started.
I’m not sure exactly what you’re trying to accomplish, but here’s a simplified version of your script that just calls “click” when a button is clicked, so it only gets called once per run instead of twice
import streamlit as st
if "clicked" not in st.session_state:
st.session_state["clicked"] = False # clicked initialized to False
def click():
st.session_state["clicked"] = not st.session_state["clicked"]
st.write("Now st.session_state.clicked:", st.session_state.clicked)
st.button("num1", key="num3", on_click=click)
st.button("num2", key="num2", on_click=click)
st.write("Now st.session_state.clicked:", st.session_state.clicked)
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.