How to do nested button and print both button outputs?

How to do this:

button1 = st.button('click')
if button1:
    st.write('button 1 is working')
    button2 = st.button('click me')
    if button2:
        st.write('button2 is working')

When I press button 1, I want it to print “button 1 is working” and show button2. And if I click on button 2, I want the button1 output to remain on the page + I want button 2 output.

How to do this?

That is a popular question! :smiley: Buttons don’t remember their state, so they will only be True immediately after being clicked (and hence a nested button with go away when clicked because the parent will go back to False). You can use session state and callback functions to extend the functionality.

I did a little introduction on extending widget functionality with session state here

Here’s a few cases other users have had with nested buttons (first link goes to my reply with sample code to show a basic case)

1 Like

I will check your other posts. This helped me troubleshoot. Thanks! Side note: Do you know exactly how long does the button stay true after clicking?

Every time you interact with something, the page reloads. So when you click a button, the page will reload with that button being True. As soon as you do anything else, the page will reload with that button being False. It’s not so much a length of time, just that it won’t survive past any other activity.