Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button inside button is resetting the page #931

Closed
pavancs opened this issue Jan 7, 2020 · 4 comments
Closed

Button inside button is resetting the page #931

pavancs opened this issue Jan 7, 2020 · 4 comments
Labels
type:bug Something isn't working

Comments

@pavancs
Copy link

pavancs commented Jan 7, 2020

Summary

I have a scenario where, when a button is pressed, it should perform some action and then a new button should appear for further actions. But, when the new button is pressed, the current page is getting completely reset. Sample code as shown below,

    if st.button("Button 1"):
        st.text("Button 1 pressed")
        if st.button("Button 2"):
            st.text("Button 2 pressed")

Steps to reproduce

With the above-mentioned code snippet,

  1. Click on 'Button 1'
  2. Displays message "Button 1 pressed" and Button 2 appears
  3. Press 'Button 2' to reproduce the bug

Expected behavior:

When 'Button 2' is pressed, the page should maintain previous information and execute further actions.

Actual behavior:

The complete page is getting reset.

Is this a regression?

No

Debug info

  • Streamlit version: 0.52.2
  • Python version: 3.7.5
  • Using Conda
  • OS version: MacOS 10.14.6
@pavancs pavancs added the type:bug Something isn't working label Jan 7, 2020
@svombergen
Copy link

This is working as streamlit was designed, the code reruns from top to bottom after every action, in that new run it will only have access to the latest action. In your example after pushing button 2 it doesn remember pushing button 1 before that.

I guess you could do a ugly workaround like so:

if st.button("Button 1") | st.button("Button 2"):
  st.text("Button 1 pressed")
    if st.button("Button 2"):
      st.text("Button 2 pressed")

If you dont have too many steps in your button trail :)

@pavancs
Copy link
Author

pavancs commented Jan 7, 2020

Thanks for the clarification.
I guess, above workaround is not working. Getting DuplicateWidgetID: There are multiple identical st.button widgets with the same generated key. error.
Anyways, got your point :)

@pavancs pavancs closed this as completed Jan 8, 2020
@davidusb-geek
Copy link

davidusb-geek commented May 17, 2021

I know the topic is closed but here is a functional workaround to this issue:

import SessionState

button1 = st.empty()
text1 = st.empty()
button2 = st.empty()
text2 = st.empty()

ss = SessionState.get(button1 = False)

if button1.button('1') or ss.button2:
    ss.button1 = True

if ss.button1:
    text1.write('you clicked the first button')
    if button2.button('2'):
        text2.write('you clicked the second button')

@Mehdi2402
Copy link

Hello all, I know the topic is closed but I have tried all the workarounds in this issue and none of them works.

Here is a solution that worked for me :

FaniloAndrianasolo youtube

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants