Text_input inside while loop DuplicateWidgetID

I’m trying to create a chatbot with messaging style UI. The chatbot works currently by writing to the console. I’m trying to adapt it to Streamlit. My current code in question is as follows:

    current_line_number = 1
    user_submission = st.text_input('Response', key=str(current_line_number))
    if user_submission = 'exit':
        response = st.write('You said ' + user_submission)
        current_line_number = current_line_number + 1

However, even though I’m changing the keys with each loop, I still get the DuplicateWidgetID error.

Does anyone know what the problem is?

Hi @acarv468. Welcome to the community!

The problem seems to be that you need to move the line number initialization

current_line_number = 1

out of the while loop.

Happy app creating!

Thank you for responding so quickly. That did work solve the DuplicateWidgetID issue. However, now it displays infinite text input boxes. I’ll have to play around with it to try to get it working.

Yes. That’s because the first time your script runs, the text boxes will never have “exit” in them. What are you trying to do?

Also FWIW in Python, that first line would typically read:

while flag:

Happy app creating! :slight_smile:

1 Like

I’m trying to deploy a chatbot. So trying to print a new message each time the user enters text