How to use output variables from one button in another button?

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)

Hi,

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.

  1. Text_input inside a button doesn't work - #2 by asehmi
  2. How to work date input with shortcuts - #2 by asehmi

HTH,
Arvindra

1 Like

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

if start:
    try:
        # performs operation
        # final_res generated
    finally:
        st.session_state.result = final_res

if stop:
   st.write(st.session_state.result)
1 Like

Oh yes, thank you so much!
This solved it