Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,9 @@ cart = {}
|
|
| 33 |
|
| 34 |
# Function to render the menu
|
| 35 |
def display_menu():
|
|
|
|
|
|
|
|
|
|
| 36 |
menu_html = ""
|
| 37 |
for item in menu_data:
|
| 38 |
if item["name"] in cart:
|
|
@@ -60,13 +63,18 @@ def display_menu():
|
|
| 60 |
<h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
|
| 61 |
<p style="font-size: 12px; color: gray;">{item['description']}</p>
|
| 62 |
</div>
|
| 63 |
-
<button
|
| 64 |
</div>
|
| 65 |
"""
|
| 66 |
return menu_html
|
| 67 |
|
|
|
|
| 68 |
# Function to add items to the cart
|
|
|
|
| 69 |
def add_to_cart(item_name):
|
|
|
|
|
|
|
|
|
|
| 70 |
if item_name in cart:
|
| 71 |
cart[item_name]["quantity"] += 1
|
| 72 |
else:
|
|
@@ -78,7 +86,8 @@ def add_to_cart(item_name):
|
|
| 78 |
"quantity": 1
|
| 79 |
}
|
| 80 |
break
|
| 81 |
-
return update_cart_summary()
|
|
|
|
| 82 |
|
| 83 |
# Function to update the cart summary
|
| 84 |
def update_cart_summary():
|
|
@@ -115,16 +124,19 @@ def display_cart():
|
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
gr.Markdown("# Dynamic Food Menu")
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
cart_summary = gr.HTML(value="No items added to cart.")
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
add_button = gr.Button("Add to Cart", visible=False)
|
| 125 |
-
add_button.click(add_to_cart, inputs=item_name_input, outputs=[menu_html, cart_summary])
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
demo.launch()
|
|
|
|
|
|
| 33 |
|
| 34 |
# Function to render the menu
|
| 35 |
def display_menu():
|
| 36 |
+
"""
|
| 37 |
+
Generate the menu as HTML with functional Add buttons tied to Gradio's backend.
|
| 38 |
+
"""
|
| 39 |
menu_html = ""
|
| 40 |
for item in menu_data:
|
| 41 |
if item["name"] in cart:
|
|
|
|
| 63 |
<h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
|
| 64 |
<p style="font-size: 12px; color: gray;">{item['description']}</p>
|
| 65 |
</div>
|
| 66 |
+
<button name="add-{item['name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer; margin-left: auto;">Add</button>
|
| 67 |
</div>
|
| 68 |
"""
|
| 69 |
return menu_html
|
| 70 |
|
| 71 |
+
|
| 72 |
# Function to add items to the cart
|
| 73 |
+
# Add to Cart Logic
|
| 74 |
def add_to_cart(item_name):
|
| 75 |
+
"""
|
| 76 |
+
Add an item to the cart and return the updated menu and cart summary.
|
| 77 |
+
"""
|
| 78 |
if item_name in cart:
|
| 79 |
cart[item_name]["quantity"] += 1
|
| 80 |
else:
|
|
|
|
| 86 |
"quantity": 1
|
| 87 |
}
|
| 88 |
break
|
| 89 |
+
return display_menu(), update_cart_summary()
|
| 90 |
+
|
| 91 |
|
| 92 |
# Function to update the cart summary
|
| 93 |
def update_cart_summary():
|
|
|
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
gr.Markdown("# Dynamic Food Menu")
|
| 126 |
|
| 127 |
+
menu_html = gr.HTML(value=display_menu())
|
| 128 |
+
cart_summary = gr.HTML(value="No items added to cart.")
|
|
|
|
| 129 |
|
| 130 |
+
# Hidden input for item name
|
| 131 |
+
item_name_input = gr.Textbox(visible=False)
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
# Add Button Logic
|
| 134 |
+
add_button = gr.Button("Add to Cart", visible=False)
|
| 135 |
+
add_button.click(fn=add_to_cart, inputs=item_name_input, outputs=[menu_html, cart_summary])
|
| 136 |
+
|
| 137 |
+
# Display Menu and Cart Summary
|
| 138 |
+
menu_html.render()
|
| 139 |
+
cart_summary.render()
|
| 140 |
|
| 141 |
demo.launch()
|
| 142 |
+
|