question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

empty() does not remove cleared elements?

See original GitHub issue

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

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
blackarycommented, Aug 19, 2022

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

1reaction
LukasMasuchcommented, Jul 28, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

jquery - empty() everything except a particular element
Using jQuery's empty() method, can I prevent a particular element from being removed from the DOM, while all other elements are removed?
Read more >
.empty() | jQuery API Documentation
Description: Remove all child nodes of the set of matched elements from the DOM. version added: 1.0.empty(). This method does not accept any...
Read more >
Difference between remove(), empty() and detach() methods
empty() – Removes all content and child elements from the selected element. This method does not remove selected element. detach() – Removes all ......
Read more >
How to empty an ArrayList in Java? clear() vs removeAll ...
This method is not designed to remove all elements from a Collection. So, if your intention is to delete all elements from a...
Read more >
Collect, Clear, and ClearCollect functions in Power Apps
The items to be added can be: ... Note that Clear only operates on collections and not other data sources. You can use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found