Help needed with user input - Duplicate WidgetID

Hi all,

I am a new user of streamlit. I need to get user input. I am doing it in a while loop - so that I can validate user input. This works very well in python. However, in streamlit, it always shows me a duplicate widgetID error.
Here is the simplified piece of code -

import streamlit as st
word_list = [‘arise’,‘awake’,‘smart’]
def input_word():
while True:
word = st.text_input("Input the word you entered> ")
if len(word) == 5 and word.lower() in word_list:
break
return word.lower()
input_word()

And this is the error message I get -

DuplicateWidgetID : There are multiple identical st.text_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.text_input .

The program still runs and I can get the input. However, I would like to remove this error message seen by the user.
If I try to pass the key to the st.text_input function, it will execute infinitely while printing the input line infinite times.

Is there a way, I can avoid this error message getting displayed?

Thanks for your help in advance,
Vikram.

Hello @Vikram_Kamthe

You don’t need the while True loop in there, Streamlit takes care of rerunning the script from top to bottom with the user input stored in the word variable whenever the user inputs something in the text_input. The while True creates an unecessary infinite loop which is why you see an infinite input line.

import streamlit as st

word_list = ["arise","awake","smart"]

word = st.text_input("Input the word you entered> ")
if len(word) == 5 and word.lower() in word_list:
  st.write(word)

Happy Streamlitin’
Fanilo :balloon:

1 Like

Hi @andfanilo,

Thank you for taking a look and for your response. I agree that this would solve the problem in case of single input on a page. However I have multiple inputs in a loop and if I do not use the while loop, streamlit will print the next statements as well before getting user input for first text.

You can take a look at my streamlit app here -
https://share.streamlit.io/vkamthe/wordlehelper/main/WordleHelperPredict.py

And the code here -

Maybe I am thinking of traditional programming flow and not using the streamlit flow.

Or if there is any way to hide the streamlit DuplicateWidget warning, that may also work for me.

regards,
Vikram.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.