Duplicate WidgetID Error

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?

1 Like

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.

2 Likes

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>")

3 Likes

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.

2 Likes