Session_state check breaks button

If you’re creating a debugging post, please include the following info:

  1. Are you running your app locally or is it deployed? YES
  2. If your app is deployed:
    a. Is it deployed on Community Cloud or another hosting platform? NA
    b. Share the link to the public deployed app.
  3. Share the link to your app’s public GitHub repository (including a requirements file). NOT IN GITHUB
  4. Share the full text of the error message (not a screenshot).
    clicking st.button is not having desired affect when checking the state of a session_state variable
  5. Share the Streamlit and Python versions.
    Streamlit, version 1.35.0
    Python 3.12.3

I’ve updated this with a simpler code to follow that still produces same result

import streamlit as st

def change_check():
    st.write("in change_check") # this displays correctly
    st.session_state.change = True

#initialize variables
my_var=None
if 'change' not in st.session_state:
    st.session_state.change = False

# selectbox trigger change_check
pick = st.selectbox("Pick a Collection",["test"],index=None,key='colpick', on_change=change_check)

#confirm response
st.write(f"st.session_state.change value is (should be True): {st.session_state.change}")

# working_db =chromadb.PersistentClient(path=st.session_state.dbpath)
if st.session_state.change: 
    my_var = "check"
    st.session_state.change = False   # ----- THIS BREAKS BUTTON 

if my_var != None:  # If I remove this "if" it works 
    if st.button("Collection Stats"):
        st.write("Hi there")  # This will only print when I st.session_state.change 

# There are two ways to print "Hi there"  1) comment out st.session_state.change=false OR  2) remove the my_var != None


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