Hi there,
I am struggling to understand how session_state works with respect to the submit form. I have a variable that is updated when the a button is pressed. However, when later in the code I submit a form to use that value, it somehow gets back to its original value. The code is:
with st.container():
st.header("Manual configuration")
st.write(st.session_state)
with st.expander("Columns geometry:"):
col_1,col_2,col_3,col_4,col_5,col_6,col_7,col_8=st.columns(8); # divide the space into columns
col_2_value=col_2.selectbox("C1 type",options=["S","D","W"],index=1,key="col_2_key");
with st.container():
def update():
st.session_state["col_2_key"] = "S";
column_presets=st.columns(4); # Create the columnspan
SW_noLoad_button=column_presets[0].button('SW_noLoad',on_click=update);
st.write(st.session_state["col_2_key"])
The code works and if it returns “S” when the the button is clicked.
However, later on I have a “submit” button as:
with st.form("Run analysis"):
submitted = st.form_submit_button("Run analysis"); # create button analysis
if submitted:
When I press the “submit” button, the value “S” charges back into “D” again. That’s not really clear to me.
Apologies it might be very obvious but could someone please:
explain to me why it happens? It seems like the code re-run again from 0 instead of memorising the value
Help me carry the new value forward into the analysis section?
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.