Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -273,24 +273,15 @@ if "food_items" not in st.session_state:
|
|
273 |
# Initialize form input state variables
|
274 |
if "custom_food_name" not in st.session_state:
|
275 |
st.session_state["custom_food_name"] = ""
|
276 |
-
if "
|
277 |
-
st.session_state["
|
278 |
-
|
279 |
-
st.session_state["form_portion_unit"] = UNIT_OPTIONS[0]
|
280 |
-
if "form_cooking_method" not in st.session_state:
|
281 |
-
st.session_state["form_cooking_method"] = COOKING_METHODS[0]
|
282 |
-
|
283 |
def reset_form_fields():
|
284 |
-
"""Reset all form fields after adding an item"""
|
285 |
# Reset custom food name
|
286 |
st.session_state["custom_food_name"] = ""
|
287 |
-
#
|
288 |
-
st.session_state["
|
289 |
-
# Reset portion unit to default
|
290 |
-
st.session_state["form_portion_unit"] = UNIT_OPTIONS[0]
|
291 |
-
# Reset cooking method to Unknown
|
292 |
-
st.session_state["form_cooking_method"] = COOKING_METHODS[0]
|
293 |
-
# We don't reset the dropdown food selection as users might want to add multiple similar items
|
294 |
|
295 |
def add_food_item(food_name, portion_size, portion_unit, cooking_method, ingredients):
|
296 |
"""Add a food item to the session state"""
|
@@ -308,7 +299,7 @@ def add_food_item(food_name, portion_size, portion_unit, cooking_method, ingredi
|
|
308 |
"ingredients": ingredients
|
309 |
})
|
310 |
st.success(f"✅ Added {food_name} to your submission")
|
311 |
-
reset_form_fields()
|
312 |
return True
|
313 |
else:
|
314 |
st.error("❌ Please fill in all required fields")
|
@@ -453,15 +444,16 @@ if "original_image" in st.session_state:
|
|
453 |
st.subheader("🍲 Add Food Details")
|
454 |
|
455 |
# Use Streamlit form to capture Enter key and provide a better UX
|
456 |
-
with
|
|
|
|
|
457 |
food_selection = st.selectbox("Food Name", options=[""] + FOOD_SUGGESTIONS, index=0)
|
458 |
|
459 |
# Only show custom food name if the dropdown is empty
|
460 |
custom_food_name = ""
|
461 |
if food_selection == "":
|
462 |
custom_food_name = st.text_input("Or enter a custom food name",
|
463 |
-
value=st.session_state["custom_food_name"]
|
464 |
-
key="food_name_input")
|
465 |
|
466 |
# Determine the actual food name to use
|
467 |
food_name = food_selection if food_selection else custom_food_name
|
@@ -472,27 +464,23 @@ if "original_image" in st.session_state:
|
|
472 |
min_value=0.1,
|
473 |
step=0.1,
|
474 |
format="%.2f",
|
475 |
-
value=
|
476 |
-
key="portion_size_input")
|
477 |
with col2:
|
478 |
portion_unit = st.selectbox("Unit",
|
479 |
options=UNIT_OPTIONS,
|
480 |
-
index=
|
481 |
-
key="portion_unit_input")
|
482 |
|
483 |
# Set Cooking Method with "Unknown" as the default (index 0)
|
484 |
cooking_method = st.selectbox("Cooking Method (optional)",
|
485 |
options=COOKING_METHODS,
|
486 |
-
index=
|
487 |
-
key="cooking_method_input")
|
488 |
|
489 |
ingredients = st_tags.st_tags(
|
490 |
label="Main Ingredients (Add up to 5)",
|
491 |
text="Press enter to add",
|
492 |
value=[],
|
493 |
suggestions=["Salt", "Pepper", "Olive Oil", "Butter", "Garlic", "Onion", "Tomato"],
|
494 |
-
maxtags=5
|
495 |
-
key="ingredients_input"
|
496 |
)
|
497 |
|
498 |
# Submit button inside the form
|
@@ -502,11 +490,7 @@ if "original_image" in st.session_state:
|
|
502 |
# Store the custom food name if needed for future use
|
503 |
if custom_food_name:
|
504 |
st.session_state["custom_food_name"] = custom_food_name
|
505 |
-
|
506 |
-
# Reset all form fields to their defaults
|
507 |
-
reset_form_fields()
|
508 |
-
|
509 |
-
# Rerun to refresh with cleared fields
|
510 |
st.rerun()
|
511 |
|
512 |
# Make submit button more prominent
|
|
|
273 |
# Initialize form input state variables
|
274 |
if "custom_food_name" not in st.session_state:
|
275 |
st.session_state["custom_food_name"] = ""
|
276 |
+
if "form_key" not in st.session_state:
|
277 |
+
st.session_state["form_key"] = 0 # Add a form key to force re-rendering
|
278 |
+
|
|
|
|
|
|
|
|
|
279 |
def reset_form_fields():
|
280 |
+
"""Reset all form fields after adding an item by incrementing the form key"""
|
281 |
# Reset custom food name
|
282 |
st.session_state["custom_food_name"] = ""
|
283 |
+
# Increment the form key to force re-rendering with default values
|
284 |
+
st.session_state["form_key"] = st.session_state.get("form_key", 0) + 1
|
|
|
|
|
|
|
|
|
|
|
285 |
|
286 |
def add_food_item(food_name, portion_size, portion_unit, cooking_method, ingredients):
|
287 |
"""Add a food item to the session state"""
|
|
|
299 |
"ingredients": ingredients
|
300 |
})
|
301 |
st.success(f"✅ Added {food_name} to your submission")
|
302 |
+
reset_form_fields() # Reset form by incrementing key
|
303 |
return True
|
304 |
else:
|
305 |
st.error("❌ Please fill in all required fields")
|
|
|
444 |
st.subheader("🍲 Add Food Details")
|
445 |
|
446 |
# Use Streamlit form to capture Enter key and provide a better UX
|
447 |
+
# Use a dynamic key based on form_key to force re-rendering with default values
|
448 |
+
form_key = st.session_state.get("form_key", 0)
|
449 |
+
with st.form(key=f"food_item_form_{form_key}"):
|
450 |
food_selection = st.selectbox("Food Name", options=[""] + FOOD_SUGGESTIONS, index=0)
|
451 |
|
452 |
# Only show custom food name if the dropdown is empty
|
453 |
custom_food_name = ""
|
454 |
if food_selection == "":
|
455 |
custom_food_name = st.text_input("Or enter a custom food name",
|
456 |
+
value=st.session_state["custom_food_name"])
|
|
|
457 |
|
458 |
# Determine the actual food name to use
|
459 |
food_name = food_selection if food_selection else custom_food_name
|
|
|
464 |
min_value=0.1,
|
465 |
step=0.1,
|
466 |
format="%.2f",
|
467 |
+
value=0.1) # Always use default values
|
|
|
468 |
with col2:
|
469 |
portion_unit = st.selectbox("Unit",
|
470 |
options=UNIT_OPTIONS,
|
471 |
+
index=0) # Always use default values
|
|
|
472 |
|
473 |
# Set Cooking Method with "Unknown" as the default (index 0)
|
474 |
cooking_method = st.selectbox("Cooking Method (optional)",
|
475 |
options=COOKING_METHODS,
|
476 |
+
index=0) # Always use default values
|
|
|
477 |
|
478 |
ingredients = st_tags.st_tags(
|
479 |
label="Main Ingredients (Add up to 5)",
|
480 |
text="Press enter to add",
|
481 |
value=[],
|
482 |
suggestions=["Salt", "Pepper", "Olive Oil", "Butter", "Garlic", "Onion", "Tomato"],
|
483 |
+
maxtags=5
|
|
|
484 |
)
|
485 |
|
486 |
# Submit button inside the form
|
|
|
490 |
# Store the custom food name if needed for future use
|
491 |
if custom_food_name:
|
492 |
st.session_state["custom_food_name"] = custom_food_name
|
493 |
+
# Don't call reset_form_fields() here, it's already called in add_food_item
|
|
|
|
|
|
|
|
|
494 |
st.rerun()
|
495 |
|
496 |
# Make submit button more prominent
|