In my streamlit project, I have two buttons Start and Stop . Start button performs certain operation containing try-finally block which gives a result in variable final_res
& Stop button should display this final_res but isn’t detecting the variable since its in another if statement. How do I tackle this?
example pseudo code
if start:
try:
performs operation
final_res generated
finally:
final_res
if stop:
st.write(final_res)
Please refer to these questions which I answered. They explain how buttons behave and how you can use session state to hold values from widget interactions.
I’m still finding it difficult, Could you please specify where and how do I initialise the session state and set it to True even after pressing the other button to return the result?
if 'result' not in st.session_state:
st.session_state.result = None
if start:
try:
# performs operation
# final_res generated
finally:
st.session_state.result = final_res
if stop:
st.write(st.session_state.result)