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
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.
- Text_input inside a button doesn't work - #2 by asehmi
- How to work date input with shortcuts - #2 by asehmi
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?
Something like this should work:
if 'result' not in st.session_state:
st.session_state.result = None
# performs operation
# final_res generated
st.session_state.result = final_res
Oh yes, thank you so much!
This solved it