How to use multiple buttons in st.columns

While using st.columns, i want to kept the result from one button if the other one got clicked, but it kept resetting

Code snippet:

with st.container():
        col1, col2 = st.columns(2)
        with col1:
            if st.button('Predict sentiment'): ...
        with col2:
            if st.button('Predict Price'): ...

I wanted both of this results to be displayed at the same time, please guide me

Hello @Huesca_Rashad, welcome to the forum!

Buttons by default don’t record state, so pressing one only counts it as pressed once, and when something else happens, it counts as not pressed. If you want to do something like what you’ve described, you can use session state, similar to what’s described here 3+ Nested Buttons - #2 by blackary. Please let me know if that doesn’t answer your question.

1 Like

i’m sorry but i’m new in using streamlit, i’m afraid i couldn’t follow the instructions when applying it to two different columns

@Huesca_Rashad Here is a little app to show one way to do this, using a custom function called stateful_button (you can see the code and copy it if you press the “show code” button) https://playground.streamlit.app/?q=stateful_button

1 Like

okay so i have to define the state function before the with command for each columns, got it, thank you very much for your efforts! really appreciated it!

1 Like