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?
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)
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.
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.