streamlit: empty() does not remove cleared elements?

Summary

Not sure if this is expected behaviour (at least from docs on empty it doesn’t seem so). Calling .empty() on st.empty keeps children elements in the Streamlit state, so their ids cannot be re-cycled even though the widgets dissappear.

Steps to reproduce

Open in Streamlit Cloud

import streamlit as st

placeholder = st.empty()

with placeholder.container():
  st.number_input("One", value=1)
  st.number_input("Two", value=2)

if st.button('Clear & Add'):
  placeholder.empty()
  with placeholder.container():
    st.number_input("One", value=1)
    st.number_input("Two", value=2)
    st.number_input("Three", value=3)

Run the app and click Clear & Add button.

Expected behavior:

2 number inputs are replaced with 3 number inputs as specified in code snippet.

Actual behavior:

streamlit.errors.DuplicateWidgetID: There are multiple identical `st.number_input` widgets with the
same generated key.

When a widget is created, it's assigned an internal key based on
its structure. Multiple widgets with an identical structure will
result in the same internal key, which causes this error.

To fix this error, please pass a unique `key` argument to
`st.number_input`.

Debug info

  • Streamlit version: 1.10.0
  • Python version: 3.7.13
  • OS version: macOS Big Sur 11.6.8
  • Browser version: 103

Additional information

I’m using modified example from https://docs.streamlit.io/library/api-reference/layout/st.empty with different set of widgets. It would be nice for empty to actually remove the widgets so that it’s possible to re-draw bits of UI from scratch.

Thanks.

Views

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 21
  • Comments: 20 (4 by maintainers)

Most upvoted comments

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Views

I’m also struggling with the same issue. Without empty there’s no real way to make a streamlit app properly dynamic without lots on ugly lapsed elements

See the app here to see issue for container entries in general, and how it can be fixed by adding a sleep after emptying

This issue is really bad for me, I’m trying to build chat in combination with buttons and st.spinner as “typing/actions indicator”, and I’m put in a very uncomfortable position to choose between empty() of a full chat + sleep on every user input (where the whole chat is rendered anew and user clearly sees this weird behavior of everything disappearing and fast automatic scroll to the bottom) OR I only clear buttons under message on user input, but that often shows repeat of last message from the bot under new user input (gray-ed out) until something else is rendered on top.

Is there a chance this “clearing” stuff will get fixed anytime soon?

Still dealing with the same thing, am having an impossible time clearing any UI elements on a button click.

In general, I find the behavior of st.empty() to be a bit unpredictable. I can empty an empty in slightly different ways, then run the code repeatedly to find that each method is not solidly consistent. Adding the time.sleep() after an attempt to clear does indeed improve the consistency, but not exactly 100%. Here’s a gist showing different ways I’ve tried interacting with empty.

If you comment out the sleep inside the wait function, you can see it unadorned with workaround @blackary mentioned.

Without the workaround, some clicks with have all the columns clearing, some will have none clearing, some will have only one clearing, and some will have all but one clearing. Just try clicking about every 10 lines or so. I’ve done this a few times, usually going 50-100 clicks in a run.

In particular, the first click on the page usually has none of the columns clearing (with or without workaround).

With the workaround: Columns (by index) 0,1, and 4 are the best behaving, followed by 3. Although columns 2 and 5 usually don’t empty, there are times when all the columns empty. (Column 3 is probably the quickest way to witness the workaround “mostly working” but not 100%.)

https://gist.github.com/MathCatsAnd/cd25d9eb8276b131e29665d983f76ace

It just seems a bit wasteful to keep all those objects when they are not visible.

I’m not so sure if there is even a lot of waste created by this since the check is mainly based on an str set of already added widget keys. But I also haven’t looked closely into this, and there could be some wasteful stuff happening on the frontend part. This code part might be a good point to start looking into this:

https://github.com/streamlit/streamlit/blob/756072522ee4a473158355f025f36e1dbd7b74a2/lib/streamlit/state/widgets.py#L195