Streamlit session attributes reassigned somewhere

  1. Are you running your app locally or is it deployed? Locally
  2. Share the Streamlit and Python versions.
    Python 3.11.5
    streamlit==1.37.0

No errors just not understanding behaviour.

Issue:
Hello,
I’m new to streamlit.
I’m building a streamlit app and just laying the basic functionality (buttons, text boxes, etc…).
I want the user to only be able to give feedback once per submission (+1 or -1).
For this I’m using a st.session attribute: submitted to enable or disable feedback buttons.
I face a behavior that i do not understand when I submit text using ask button.
If I pressed +1, I’m allowed to repress +1 one more time (button not disabled).
If I pressed -1, I’m allowed to repress +1 or -1 one more time (buttons not disabled).
The buttons should be disabled if st.session.submitted is False.
The issue is mainly in st.session.submitted, it gets somehow reassigned to True again, despite one feedback button is pushed.
Can anyone help with this? I tried chatgpt and claude and both didn’t help.

import streamlit as st
import uuid

def print_log(*message):
    print(*message, flush=True)

def main():
    print_log("Starting the Course Assistant application")
    st.title("Course Assistant")

    # Session state initialization
    if 'conversation_id' not in st.session_state:
        st.session_state.conversation_id = str(uuid.uuid4())
        print_log(f"New conversation started with ID: {st.session_state.conversation_id}")
    if 'count' not in st.session_state:
        st.session_state.count = 0
        print_log("Feedback count initialized to 0")
    if 'submitted' not in st.session_state:
        st.session_state.submitted = False

    # User input
    user_input = st.text_input("Enter your question:")

    if st.button("Submit"):
        print_log(f"User submitted question: {user_input}")
        st.write(f"Question submitted: {user_input}")
        st.session_state.submitted = True

    col1, col2 = st.columns(2)
    with col1:
        if st.button("+1", disabled=not st.session_state.submitted):
            st.session_state.submitted = False
            st.session_state.count += 1
            print_log(f"Positive feedback received. New count: {st.session_state.count}")
    with col2:
        if st.button("-1", disabled=not st.session_state.submitted):
            st.session_state.submitted = False
            st.session_state.count -= 1
            print_log(f"Negative feedback received. New count: {st.session_state.count}")

    st.write(f"Current count: {st.session_state.count}")

if __name__ == "__main__":
    main()
1 Like

Hi,
The streamlit app is running from top to bottom (take a while to fully understand all the implication !)
So if you want the button above to be false, you need to use ‘st.rerun’. (it’s why, the second time, when you press the button, it rerun because there is a changement in the code, the session state as changed, and update the value)

            with col1:
                if st.button("+1", disabled=not st.session_state.submitted):
                    st.session_state.submitted = False
                    st.session_state.count += 1
                    print_log(f"Positive feedback received. New count: {st.session_state.count}")
                    st.rerun()
            with col2:
                if st.button("-1", disabled=not st.session_state.submitted):
                    st.session_state.submitted = False
                    st.session_state.count -= 1
                    print_log(f"Negative feedback received. New count: {st.session_state.count}")
                    st.rerun()

Hope, it helps !

1 Like

Thank you so much!!

Yes, totally helps.

But just to make sure I understand 100% correctly, so the app only restarts in either 2 cases: a change of code (in subsequent blocks like above case) or a manual restart (st.rerun()).

And no restart happens automatically with buttons being pressed?

Every widget trigger (slider, button…) will rerun the app :slight_smile:
(this is why we need to store the st.session_state (One exercice very usefull is the nested button )

1 Like
1 Like

Thank you!

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