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?
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.
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)
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.