Checkbox doesn't seem to change to True upon clicking

I have a problem that I conceptually know how to solve, but can’t really make it work in practice.
I have an app that interacts with the user a lot before the final result is rendered. At the end I’d just want to ask whether the user is happy with the result or not.
I am using checkbox and if the user is happy, I want to display something.
This is the general idea that I’ve set up:

if 'happy' not in st.session_state:
      st.session_state.happy = False
if st.session_state.happy is False:
    {do the whole calculation}
    st.session_state.happy = st.checkbox("are you happy?")
else:
    st.text("happy to hear!")

The problem is that even when I get to the checkbox and click on it, the “happy” session state never changes and the second part of if/else never executes. Can someone point out what I am doing wrong?

Hi @urska , check if the following code suffices:

Cheers

import streamlit as st

if ‘happy’ not in st.session_state:
    st.session_state.happy = False

st.session_state.happy = st.checkbox(“are you happy?”)

if st.session_state.happy is False:
    st.text(“sorry to hear!”)
    # do any calculations you want here
else:
    st.text(“happy to hear!”)
    # do any calculations you want here

Hi @Shawn_Pereira, I’ll help with the formatting, just a little reminder to use the preformatted text option if you want to paste your code, Cheers!

I am not sure I understand - it seems that you’ve moved the location of the checkbox?
I specifically need this checkbox to be at the end of the whole calculation, because it will ask questions about the experience with the app.
I am confused why the “happy” session state doesn’t update when the checkbox is checked.
thank you!

Hi @urska, the session state updates, it’s just that you dont know it as you have not displayed it on screen. Which is why I changed the code.

If you want to stick to your earlier code, you can replace your checkbox line with the following 2 lines.

st.session_state.happy = st.checkbox("are you happy?", value=st.session_state.happy)
st.button("click me")

The button will cause a screen refresh. After the refresh, since the happy variable values is already changed, the code will branch to the ’ happy to hear` message.

Cheers

Thanks @ditw11mhs for the formatting help - I always bungle with that. :slight_smile:

hey, @Shawn_Pereira,
I tried it and it doesn’t work. Also, i don’t want to display another button to click (it makes the layout very confusing this way). What currently happens is the following:

  • state “happy” is initialised as False
  • the main calculation is performed
  • at the end of the main calculation, the checkbox appears. The expected behaviour is for the state “happy” to change to True upon clicking and when the code is reloaded (upon clicking) to perform the “else” part of the code
  • however, what actually happens is that state “happy” remains False upon clicking and after reload, the main calculation starts again.
    (the same happens with your last solution)

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