My streamlit app keep running endless while loop
I tried using a condition for while loop to interact with my app, and the streamlit keep running on infinite. Please i need your help.
Steps to reproduce
Code snippet:
if 'button' not in st.session_state:
st.session_state['button'] = 0
user_name = st.text_input('What is your name?')
if user_name:
user_name = user_name.title()
st.write(f'Hi {user_name}! :wave: Nice to meet you.')
conversation_active = 0
while conversation_active > 0:
time.sleep(1)
ingredient_input_counter += 1
st.write("Let's get the ingredients! :smile:")
option = st.radio("How would you like to input your ingredients?", ["Write Ingredients", "Upload Image"], key =f"option_{ingredient_input_counter}")
if option == "Write Ingredients":
ingredient_text = st.text_input(f"Enter ingredients seperated by ONLY a comma(,):", key =f"ingredient_text_input_{ingredient_input_counter}")
if ingredient_text is not None:
try:
each_ingredient = ingredient_text.split(',')
for ingredd in each_ingredient:
ingredients_list.append(ingredd)
except Exception as e:
st.error(f"An error occurred: {str(e)}")
conversation_active = False
restart = st.button('Restart', key=f'text_except_button_{st.session_state.button}')
if restart:
st.session_state.button +=1
st.write('Welcome back!')
ingredients_list = []
conversation_active = True
elif option == "Upload Image":
uploaded_image = st.file_uploader(f"Upload an Image for Ingredient {ingredient_input_counter}:", type=["jpg", "png", "jpeg"], key =f"image_uploader_{ingredient_input_counter}")
# Perform image recognition here and append the result to ingredients_list
if uploaded_image is not None:
# Perform image recognition on the uploaded image
try:
img = Image.open(uploaded_image)
ingredient_label = recognize_ingredient(img)
st.image(img, caption=f"Recognized Ingredient: {ingredient_label}", use_column_width=True)
# Ask the user to confirm the recognized ingredient
confirmation = st.radio(f"Is the recognized ingredient '{ingredient_label.upper()}' correct?", ["Yes", "No"], key =f"image_confirmation_{ingredient_input_counter}")
if confirmation == "Yes":
ingredients_list.append(ingredient_label)
else:
image_continue = st.radio("You selected 'No!' would you like to reload the image, write by text or cancel application?", ["Reload image", "Text input", "Restart"], key =f"image_correction_{ingredient_input_counter}")
if image_continue == "Reload image":
uploaded_image2 = st.file_uploader(f"Upload an Image for Ingredient {ingredient_input_counter}:", type=["jpg", "png", "jpeg"], key =f"image_reuploader_{ingredient_input_counter}")
if uploaded_image is not None:
try:
img = Image.open(uploaded_image)
ingredient_label = recognize_ingredient(img)
st.image(img, caption=f"Recognized Ingredient: {ingredient_label}", use_column_width=True)
second_confirmation = st.radio(f"Is the recognized ingredient '{ingredient_label.upper()}' correct?", ["Yes", "No"], key =f"second_image_confirmation_{ingredient_input_counter}")
if second_confirmation == "Yes":
ingredients_list.append(ingredient_label)
else:
st.write("You selected 'No!' :sad: Please restart the application")
conversation_active = False
secod_restart = st.button('Restart', key=f'second_restart_button_{st.session_state.button}')
if restart:
st.session_state.button +=1
st.write('Welcome back! :smile:')
ingredients_list = []
conversation_active = True
except:
st.write("Error recognizing the image. Please try uploading the image again.")
conversation_active = False
restart = st.button('Restart', key=f'third_except_button_{st.session_state.button}')
if restart:
st.session_state.button +=1
st.write('Welcome back! :smile:')
ingredients_list = []
conversation_active = True
elif image_continue == "Text input":
ingredient_text = st.text_input(f"Enter ingredients seperated by ONLY a comma(,):", key =f"second_ingredient_text_input_{ingredient_input_counter}")
if ingredient_text is not None:
try:
each_ingredient = ingredient_text.split(',')
for ingredd in each_ingredient:
ingredients_list.append(ingredd)
except Exception as e:
st.error(f"An error occurred: {str(e)}")
conversation_active = False
restart = st.button('Restart', key=f'second_text_except_button_{st.session_state.button}')
if restart:
st.session_state.button +=1
st.write('Welcome back! :smile:')
ingredients_list = []
conversation_active = True
else:
st.write("You have chosen to restart! :smile:")
st.session_state.button +=1
st.write('Welcome back! :smile:')
ingredients_list = []
conversation_active = True
except:
st.write("Error recognizing the image. Please try uploading the image again.")
conversation_active = False
restart = st.button('Restart', key=f'image_except_button_{st.session_state.button}')
if restart:
st.session_state.button +=1
st.write('Welcome back! :smile:')
conversation_active = True
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
after greeting the user and asking how they want to input their ingredients, it should pause to allow the user to choose, after which based on their choice, they can continue.
Actual behavior:
Instead afetr greeting, it contunies to endlessless print the otion to choose and the text_input line to enter the ingredients by text