empty() does not remove cleared elements?
See original GitHub issueSummary
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:
- Created a year ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
See the app here to see issue for container entries in general, and how it can be fixed by adding a sleep after emptying
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