Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,7 +33,7 @@ cart = {}
|
|
| 33 |
|
| 34 |
def display_menu():
|
| 35 |
"""
|
| 36 |
-
Render the menu dynamically.
|
| 37 |
"""
|
| 38 |
menu_html = ""
|
| 39 |
for item in menu_data:
|
|
@@ -47,9 +47,9 @@ def display_menu():
|
|
| 47 |
<p style="font-size: 12px; color: gray;">{item['description']}</p>
|
| 48 |
</div>
|
| 49 |
<div style="margin-left: auto; display: flex; align-items: center;">
|
| 50 |
-
<button onclick="
|
| 51 |
<span style="margin: 0 10px;">{cart[item['name']]['quantity']}</span>
|
| 52 |
-
<button onclick="
|
| 53 |
</div>
|
| 54 |
</div>
|
| 55 |
"""
|
|
@@ -62,7 +62,7 @@ def display_menu():
|
|
| 62 |
<h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
|
| 63 |
<p style="font-size: 12px; color: gray;">{item['description']}</p>
|
| 64 |
</div>
|
| 65 |
-
<button
|
| 66 |
</div>
|
| 67 |
"""
|
| 68 |
return menu_html
|
|
@@ -84,49 +84,37 @@ def add_to_cart(item_name):
|
|
| 84 |
break
|
| 85 |
return display_menu()
|
| 86 |
|
| 87 |
-
def update_quantity(item_name,
|
| 88 |
"""
|
| 89 |
Update the quantity of an item in the cart.
|
| 90 |
"""
|
| 91 |
if item_name in cart:
|
| 92 |
-
if
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
cart[item_name]["quantity"]
|
|
|
|
|
|
|
| 96 |
return display_menu()
|
| 97 |
|
| 98 |
-
def view_cart():
|
| 99 |
-
"""
|
| 100 |
-
Render the cart dynamically.
|
| 101 |
-
"""
|
| 102 |
-
if not cart:
|
| 103 |
-
return "Your cart is empty."
|
| 104 |
-
|
| 105 |
-
cart_html = "<h3>Your Cart:</h3>"
|
| 106 |
-
total_price = 0
|
| 107 |
-
for item in cart.values():
|
| 108 |
-
item_total = item["price"] * item["quantity"]
|
| 109 |
-
total_price += item_total
|
| 110 |
-
cart_html += f"""
|
| 111 |
-
<div style="display: flex; align-items: center; margin-bottom: 15px; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
|
| 112 |
-
<h4 style="margin: 0;">{item['name']}</h4>
|
| 113 |
-
<div style="margin-left: auto; display: flex; align-items: center;">
|
| 114 |
-
<button onclick="update_quantity('{item['name']}', {item['quantity'] - 1})" style="background-color: #dc3545; color: white; border: none; padding: 5px; border-radius: 5px;">-</button>
|
| 115 |
-
<span style="margin: 0 10px;">{item['quantity']}</span>
|
| 116 |
-
<button onclick="update_quantity('{item['name']}', {item['quantity'] + 1})" style="background-color: #28a745; color: white; border: none; padding: 5px; border-radius: 5px;">+</button>
|
| 117 |
-
</div>
|
| 118 |
-
<p style="margin: 0 0 0 15px;">${item_total:.2f}</p>
|
| 119 |
-
</div>
|
| 120 |
-
"""
|
| 121 |
-
cart_html += f"<h4>Total: ${total_price:.2f}</h4>"
|
| 122 |
-
return cart_html
|
| 123 |
-
|
| 124 |
# Gradio Interface
|
| 125 |
with gr.Blocks() as demo:
|
| 126 |
gr.Markdown("# Dynamic Food Menu")
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def display_menu():
|
| 35 |
"""
|
| 36 |
+
Render the menu dynamically as HTML.
|
| 37 |
"""
|
| 38 |
menu_html = ""
|
| 39 |
for item in menu_data:
|
|
|
|
| 47 |
<p style="font-size: 12px; color: gray;">{item['description']}</p>
|
| 48 |
</div>
|
| 49 |
<div style="margin-left: auto; display: flex; align-items: center;">
|
| 50 |
+
<button onclick="decrease_quantity('{item['name']}')" style="background-color: #dc3545; color: white; border: none; padding: 5px; border-radius: 5px;">-</button>
|
| 51 |
<span style="margin: 0 10px;">{cart[item['name']]['quantity']}</span>
|
| 52 |
+
<button onclick="increase_quantity('{item['name']}')" style="background-color: #28a745; color: white; border: none; padding: 5px; border-radius: 5px;">+</button>
|
| 53 |
</div>
|
| 54 |
</div>
|
| 55 |
"""
|
|
|
|
| 62 |
<h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
|
| 63 |
<p style="font-size: 12px; color: gray;">{item['description']}</p>
|
| 64 |
</div>
|
| 65 |
+
<button id="add-{item['name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer; margin-left: auto;">Add</button>
|
| 66 |
</div>
|
| 67 |
"""
|
| 68 |
return menu_html
|
|
|
|
| 84 |
break
|
| 85 |
return display_menu()
|
| 86 |
|
| 87 |
+
def update_quantity(item_name, operation):
|
| 88 |
"""
|
| 89 |
Update the quantity of an item in the cart.
|
| 90 |
"""
|
| 91 |
if item_name in cart:
|
| 92 |
+
if operation == "increase":
|
| 93 |
+
cart[item_name]["quantity"] += 1
|
| 94 |
+
elif operation == "decrease":
|
| 95 |
+
cart[item_name]["quantity"] -= 1
|
| 96 |
+
if cart[item_name]["quantity"] <= 0:
|
| 97 |
+
del cart[item_name]
|
| 98 |
return display_menu()
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Gradio Interface
|
| 101 |
with gr.Blocks() as demo:
|
| 102 |
gr.Markdown("# Dynamic Food Menu")
|
| 103 |
|
| 104 |
+
menu_html = gr.HTML(value=display_menu())
|
| 105 |
+
|
| 106 |
+
# Hidden input fields for Gradio's event handling
|
| 107 |
+
item_name_input = gr.Textbox(visible=False)
|
| 108 |
+
operation_input = gr.Textbox(visible=False)
|
| 109 |
+
|
| 110 |
+
# Simulate "Add to Cart" and "Update Quantity"
|
| 111 |
+
add_button = gr.Button("Add to Cart", visible=False)
|
| 112 |
+
update_button = gr.Button("Update Quantity", visible=False)
|
| 113 |
+
|
| 114 |
+
# Update menu after adding or updating quantity
|
| 115 |
+
add_button.click(fn=add_to_cart, inputs=item_name_input, outputs=menu_html)
|
| 116 |
+
update_button.click(fn=update_quantity, inputs=[item_name_input, operation_input], outputs=menu_html)
|
| 117 |
+
|
| 118 |
+
demo.append(menu_html)
|
| 119 |
+
|
| 120 |
+
demo.launch()
|