Hey everyone. I’m working on building a human-in-the-loop app. What I’ve got is several sound files that I’d like a human to be able to go in and click yes or no on to determine if the sound file contains the correctly predicted sound. I’ve been trying to put this into a loop. When I do this, I keep running into this error:
DuplicateWidgetID: There are multiple identical st.selectbox 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, please pass a unique key argument to st.selectbox.
Essentially what I’m doing is:
Def music(input_location):
Outfile = open(input_location, 'rb')
Data = oufile.read()
St.audio(data, format = 'audio/m4a')
Option = st.selectbox('Did you hear a voice?', ('Yes', 'No'))
import glob
file_location = os.path.join('my path', '*.m4a')
filenames = glob.glob(file_location)
For I in range(len(filenames)):
Music(filenames[i])
Any ideas on how to get around this error besides manually renaming the variable?
If its inside a loop why dont you just enumerate and name it
1.asdf, 2.asdf etc…
This should give it unique keys and overcome the duplicate widget error.
Note that this error message isn’t telling you that you need to have unique Python variable names. It’s telling you that you need to add a key argument to st.selectbox or any other Streamlit widget when you want to use multiple of the same widget in an app:
Option = st.selectbox('Did you hear a voice?', ('Yes', 'No'), key = "<uniquevalueofsomesort>")
Thank you both for replying. I ended up with solution similar to this:
import glob
file_location = os.path.join(‘my path’, ‘*.m4a’)
filenames = glob.glob(file_location)
Count = 0
for f in filenames:
outfile = open(f, ‘rb’)
data = outfile.read()
st.write('File:', filenames[count])
st.audio(data, format = 'audio/m4a')
option = st.selectbox('Did you hear a voice?', ('Yes', 'No'), key = count)
st.write('You select:', option)
count += 1
Randy, thanks for pointing out the key. I completely missed that in the docs. That solved my problem pretty quickly. Thank you both again for your help.
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.