I want to build a widget with a specific key.
And the widget may be added or removed by other condition.
If it is removed, I would like to build a new one with same key. (so uuid way is not suitable for this case)
What I want is like below, but it is not work. DuplicateWidgetID : There are multiple widgets with the same key='aaa' .
import streamlit as st
st.selectbox('aaa',[1,2,3], key='aaa')
del st.session_state['aaa']
st.selectbox('aaa',[1,2,3], key='aaa')
Hi @gavin811201. According to my understanding you need to stop the rerunning of the application. Because streamlit follows every time rerun the whole application from top to bottom
Here is an approach to reusing a key. We monitor the usage of a key, if it is not yet used, we can create a widget using that key. However if it is already used, we need to decrement the usage counter so that we can use it again. This is triggered by a button in my example code.
import streamlit as st
from streamlit import session_state as ss
import random
# Maintain a variable to keep track of widget key usage.
# If the usage of the key is zero, we can use it.
if 'key_monitor' not in ss:
ss.key_monitor = {'aaa_key': 0, 'bbb_key': 0}
# Create a variable for label.
if 'label' not in ss:
ss.label = 'label 0'
def change():
ss.key_monitor['aaa_key'] = 0 # reset to recreate
def create():
if ss.key_monitor['aaa_key'] > 0:
ss.label = 'label ' + str(random.randint(10, 99))
ss.key_monitor['aaa_key'] -= 1 # decrement to create a new widget/label
# Create a widget with aaa_key if it not used yet.
if ss.key_monitor['aaa_key'] == 0:
ss.key_monitor['aaa_key'] += 1
st.selectbox(ss.label, [1,2,3], key='aaa_key', on_change=change)
# Create a new widget using the aaa_key.
st.button('Create a new widget using existing key', on_click=create)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.