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

Hi, I’ve this stateful button here:

if button("Change dataset", key="changedataset"):
   st.warning("If you don't have downloaded the modified dataset you'll lose all the changes applied.")

How it’s possible to reset its state to “not clicked” every time the page is load? (I’m able to modify the st.session_state variable also from another page but I don’t know how to that).

And also I’d like to raise a little issue related to this functionality. Not clicked my button looks like this:

image

(I’ve applied an html customization for styling them)

image

But when I click it the button become completely blank even if the functionality is working properly.

Any idea on this? Thanks for helping.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.