Session_state needs two clicks to perform action

Hello everyone, greetings from Brazil, I have a problem with “st.session_state”, in which I need to execute the action twice for it to be executed, I’m using “on_change” and even so it only works after incrementing and running it for the second time turn. I apologize if it was a little confusing, my English isn’t that good.

    with col2:
            def cid_change():
                st.session_state.cid_change = True
                st.rerun()
            container = st.container()
            
            for key, value in st.session_state.df.items():
                if key == "CID":
                    st.session_state.inputs[key] = st.text_input(key, value.upper(), on_change=cid_change).upper()
                    if st.session_state.cid_change:
                        temp_motivo = cid_query(st.session_state.inputs["CID"])
                        st.session_state.inputs["Motivo do Afastamento"] = temp_motivo
                        st.session_state.df = st.session_state.inputs
                        st.session_state.cid_change = False
                else:
                    st.session_state.inputs[key] = st.text_input(key, value)

Hello,

Did you read this document Widget updating for every second input when using session state - Streamlit Docs?

The code is iterating session_state df and then it is assigning it in the loop… I don’t think it’s a good idea…

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