Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,118 +8,90 @@ def load_menu():
|
|
| 8 |
except Exception as e:
|
| 9 |
raise ValueError(f"Error loading menu file: {e}")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
def filter_menu(preference):
|
| 13 |
menu_data = load_menu()
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
html_content = ""
|
| 17 |
-
for _, item in
|
| 18 |
html_content += f"""
|
| 19 |
-
<div class="menu-
|
| 20 |
-
<div class="menu-
|
| 21 |
<h3>{item['Dish Name']}</h3>
|
| 22 |
<p>{item['Description']}</p>
|
| 23 |
<p><strong>${item['Price ($)']}</strong></p>
|
| 24 |
</div>
|
| 25 |
-
<div class="menu-actions">
|
| 26 |
-
<img src="{item['Image URL']}" alt="{item['Dish Name']}" class="menu-
|
| 27 |
-
<button class="add-button" onclick="
|
| 28 |
</div>
|
| 29 |
</div>
|
| 30 |
"""
|
| 31 |
return html_content
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
def get_dish_details(dish_name):
|
| 35 |
-
menu_data = load_menu()
|
| 36 |
-
try:
|
| 37 |
-
dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
|
| 38 |
-
return (
|
| 39 |
-
dish["Image URL"],
|
| 40 |
-
dish["Dish Name"],
|
| 41 |
-
dish["Description"],
|
| 42 |
-
f"${dish['Price ($)']}"
|
| 43 |
-
)
|
| 44 |
-
except IndexError:
|
| 45 |
-
raise ValueError(f"Dish '{dish_name}' not found!")
|
| 46 |
-
|
| 47 |
-
# Gradio App
|
| 48 |
def app():
|
| 49 |
with gr.Blocks(css="style.css") as demo:
|
| 50 |
-
# Menu
|
|
|
|
| 51 |
preference_selector = gr.Radio(
|
| 52 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
| 53 |
value="All",
|
| 54 |
-
label="
|
| 55 |
)
|
| 56 |
menu_output = gr.HTML(value=filter_menu("All"))
|
| 57 |
|
| 58 |
-
# Modal
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
"spice_level": spice,
|
| 93 |
-
"extras": extras,
|
| 94 |
-
"quantity": qty,
|
| 95 |
-
"instructions": instructions
|
| 96 |
-
})
|
| 97 |
-
cart_html = "<br>".join(
|
| 98 |
-
[f"{item['quantity']}x {item['name']} ({item['spice_level']})" for item in cart]
|
| 99 |
-
)
|
| 100 |
-
return cart, cart_html
|
| 101 |
-
|
| 102 |
-
# Events
|
| 103 |
-
preference_selector.change(filter_menu, inputs=[preference_selector], outputs=[menu_output])
|
| 104 |
-
close_modal_button.click(close_modal, outputs=[modal])
|
| 105 |
-
add_to_cart_button.click(add_to_cart, inputs=[modal_name, spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_output])
|
| 106 |
|
| 107 |
-
# Layout
|
|
|
|
|
|
|
| 108 |
with gr.Row():
|
| 109 |
menu_output
|
| 110 |
-
with modal:
|
| 111 |
-
modal_image
|
| 112 |
-
modal_name
|
| 113 |
-
modal_description
|
| 114 |
-
modal_price
|
| 115 |
-
spice_level
|
| 116 |
-
extras
|
| 117 |
-
quantity
|
| 118 |
-
special_instructions
|
| 119 |
-
add_to_cart_button
|
| 120 |
-
close_modal_button
|
| 121 |
with gr.Row():
|
| 122 |
-
|
| 123 |
|
| 124 |
return demo
|
| 125 |
|
|
|
|
| 8 |
except Exception as e:
|
| 9 |
raise ValueError(f"Error loading menu file: {e}")
|
| 10 |
|
| 11 |
+
# Function to filter and display menu items
|
| 12 |
def filter_menu(preference):
|
| 13 |
menu_data = load_menu()
|
| 14 |
+
if preference == "Halal/Non-Veg":
|
| 15 |
+
filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
|
| 16 |
+
elif preference == "Vegetarian":
|
| 17 |
+
filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
|
| 18 |
+
elif preference == "Guilt-Free":
|
| 19 |
+
filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
|
| 20 |
+
else: # Default to "All"
|
| 21 |
+
filtered_data = menu_data
|
| 22 |
+
|
| 23 |
+
# Generate HTML for the menu
|
| 24 |
html_content = ""
|
| 25 |
+
for _, item in filtered_data.iterrows():
|
| 26 |
html_content += f"""
|
| 27 |
+
<div class="menu-card">
|
| 28 |
+
<div class="menu-card-content">
|
| 29 |
<h3>{item['Dish Name']}</h3>
|
| 30 |
<p>{item['Description']}</p>
|
| 31 |
<p><strong>${item['Price ($)']}</strong></p>
|
| 32 |
</div>
|
| 33 |
+
<div class="menu-card-actions">
|
| 34 |
+
<img src="{item['Image URL']}" alt="{item['Dish Name']}" class="menu-card-image">
|
| 35 |
+
<button class="add-button" onclick="openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}')">Add</button>
|
| 36 |
</div>
|
| 37 |
</div>
|
| 38 |
"""
|
| 39 |
return html_content
|
| 40 |
|
| 41 |
+
# Main Gradio app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def app():
|
| 43 |
with gr.Blocks(css="style.css") as demo:
|
| 44 |
+
# Menu Filters
|
| 45 |
+
gr.Markdown("## Dynamic Menu with Preferences")
|
| 46 |
preference_selector = gr.Radio(
|
| 47 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
| 48 |
value="All",
|
| 49 |
+
label="Choose a Preference"
|
| 50 |
)
|
| 51 |
menu_output = gr.HTML(value=filter_menu("All"))
|
| 52 |
|
| 53 |
+
# Modal Logic via HTML
|
| 54 |
+
modal_logic = gr.HTML("""
|
| 55 |
+
<div id="modal" class="modal">
|
| 56 |
+
<div class="modal-content">
|
| 57 |
+
<span class="close-button" onclick="closeModal()">×</span>
|
| 58 |
+
<img id="modal-image" class="modal-image" />
|
| 59 |
+
<h2 id="modal-name"></h2>
|
| 60 |
+
<p id="modal-description"></p>
|
| 61 |
+
<p id="modal-price"></p>
|
| 62 |
+
<label for="spice-level">Choose Spice Level:</label>
|
| 63 |
+
<select id="spice-level">
|
| 64 |
+
<option value="Mild">Mild</option>
|
| 65 |
+
<option value="Medium">Medium</option>
|
| 66 |
+
<option value="Spicy">Spicy</option>
|
| 67 |
+
</select>
|
| 68 |
+
<label for="quantity">Quantity:</label>
|
| 69 |
+
<input type="number" id="quantity" value="1" min="1" />
|
| 70 |
+
<textarea id="special-instructions" placeholder="Add special instructions here..."></textarea>
|
| 71 |
+
<button class="modal-add-button">Add to Cart</button>
|
| 72 |
+
</div>
|
| 73 |
+
</div>
|
| 74 |
+
<script>
|
| 75 |
+
function openModal(name, image, description, price) {
|
| 76 |
+
document.getElementById("modal").style.display = "block";
|
| 77 |
+
document.getElementById("modal-image").src = image;
|
| 78 |
+
document.getElementById("modal-name").innerText = name;
|
| 79 |
+
document.getElementById("modal-description").innerText = description;
|
| 80 |
+
document.getElementById("modal-price").innerText = price;
|
| 81 |
+
}
|
| 82 |
+
function closeModal() {
|
| 83 |
+
document.getElementById("modal").style.display = "none";
|
| 84 |
+
}
|
| 85 |
+
</script>
|
| 86 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# App Layout
|
| 89 |
+
with gr.Row():
|
| 90 |
+
preference_selector.change(filter_menu, inputs=[preference_selector], outputs=[menu_output])
|
| 91 |
with gr.Row():
|
| 92 |
menu_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
with gr.Row():
|
| 94 |
+
modal_logic
|
| 95 |
|
| 96 |
return demo
|
| 97 |
|