Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,25 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
cart = []
|
| 15 |
|
|
@@ -25,18 +35,36 @@ def display_menu():
|
|
| 25 |
<div style="flex-grow: 1;">
|
| 26 |
<h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
|
| 27 |
<p style="margin: 5px 0; font-size: 12px;">{item['description']}</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
</div>
|
| 29 |
-
<button style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer;" onclick="add_to_cart('{item['name']}',
|
| 30 |
</div>
|
| 31 |
"""
|
| 32 |
return menu_html
|
| 33 |
|
| 34 |
-
def add_to_cart(
|
| 35 |
"""
|
| 36 |
-
Add an item to the cart with its price.
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def view_cart():
|
| 42 |
"""
|
|
@@ -48,7 +76,7 @@ def view_cart():
|
|
| 48 |
cart_summary = "<h3>Cart Summary:</h3><ul>"
|
| 49 |
total_price = 0
|
| 50 |
for item in cart:
|
| 51 |
-
cart_summary += f"<li>{item['item']} - ${item['price']}</li>"
|
| 52 |
total_price += item["price"]
|
| 53 |
cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>"
|
| 54 |
return cart_summary
|
|
@@ -61,9 +89,13 @@ with gr.Blocks() as demo:
|
|
| 61 |
|
| 62 |
# Cart interactions
|
| 63 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
| 64 |
cart_button = gr.Button("View Cart")
|
| 65 |
cart_output = gr.HTML()
|
| 66 |
|
|
|
|
| 67 |
cart_button.click(view_cart, outputs=cart_output)
|
| 68 |
|
| 69 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
# Load menu from Excel file
|
| 5 |
+
def load_menu(file_path="menu.xlsx"):
|
| 6 |
+
"""
|
| 7 |
+
Load menu data from an Excel file.
|
| 8 |
+
The Excel file should have columns: Name, Price, Description, Image
|
| 9 |
+
"""
|
| 10 |
+
menu_df = pd.read_excel(file_path)
|
| 11 |
+
menu = []
|
| 12 |
+
for _, row in menu_df.iterrows():
|
| 13 |
+
menu.append({
|
| 14 |
+
"name": row["Name"],
|
| 15 |
+
"price": row["Price"],
|
| 16 |
+
"description": row["Description"],
|
| 17 |
+
"image": row["Image"]
|
| 18 |
+
})
|
| 19 |
+
return menu
|
| 20 |
+
|
| 21 |
+
# Initialize the menu
|
| 22 |
+
menu_data = load_menu()
|
| 23 |
|
| 24 |
cart = []
|
| 25 |
|
|
|
|
| 35 |
<div style="flex-grow: 1;">
|
| 36 |
<h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
|
| 37 |
<p style="margin: 5px 0; font-size: 12px;">{item['description']}</p>
|
| 38 |
+
<div style="display: flex; align-items: center;">
|
| 39 |
+
<button style="background-color: #f0f0f0; padding: 5px 10px; border-radius: 5px; margin-right: 5px;" onclick="decreaseQuantity('{item['name']}')">-</button>
|
| 40 |
+
<span id="quantity-{item['name']}" style="padding: 5px;">1</span>
|
| 41 |
+
<button style="background-color: #f0f0f0; padding: 5px 10px; border-radius: 5px; margin-left: 5px;" onclick="increaseQuantity('{item['name']}')">+</button>
|
| 42 |
+
</div>
|
| 43 |
</div>
|
| 44 |
+
<button id="add-button-{item['name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer;" onclick="add_to_cart('{item['name']}', 1)">Add</button>
|
| 45 |
</div>
|
| 46 |
"""
|
| 47 |
return menu_html
|
| 48 |
|
| 49 |
+
def add_to_cart(food_item, quantity):
|
| 50 |
"""
|
| 51 |
+
Add an item to the cart with its price and quantity.
|
| 52 |
"""
|
| 53 |
+
for item in menu_data:
|
| 54 |
+
if item["name"] == food_item:
|
| 55 |
+
cart.append({
|
| 56 |
+
"item": food_item,
|
| 57 |
+
"quantity": quantity,
|
| 58 |
+
"price": item["price"] * quantity
|
| 59 |
+
})
|
| 60 |
+
break
|
| 61 |
+
cart_summary = "<ul>"
|
| 62 |
+
total_price = 0
|
| 63 |
+
for item in cart:
|
| 64 |
+
cart_summary += f"<li>{item['item']} (x{item['quantity']}) - ${item['price']}</li>"
|
| 65 |
+
total_price += item["price"]
|
| 66 |
+
cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>"
|
| 67 |
+
return f"{food_item} added to cart!\n\n{cart_summary}"
|
| 68 |
|
| 69 |
def view_cart():
|
| 70 |
"""
|
|
|
|
| 76 |
cart_summary = "<h3>Cart Summary:</h3><ul>"
|
| 77 |
total_price = 0
|
| 78 |
for item in cart:
|
| 79 |
+
cart_summary += f"<li>{item['item']} (x{item['quantity']}) - ${item['price']}</li>"
|
| 80 |
total_price += item["price"]
|
| 81 |
cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>"
|
| 82 |
return cart_summary
|
|
|
|
| 89 |
|
| 90 |
# Cart interactions
|
| 91 |
with gr.Row():
|
| 92 |
+
food_item_input = gr.Textbox(label="Food Item", placeholder="Enter item name")
|
| 93 |
+
quantity_input = gr.Number(label="Quantity", value=1, precision=0)
|
| 94 |
+
add_button = gr.Button("Add to Cart")
|
| 95 |
cart_button = gr.Button("View Cart")
|
| 96 |
cart_output = gr.HTML()
|
| 97 |
|
| 98 |
+
add_button.click(add_to_cart, inputs=[food_item_input, quantity_input], outputs=cart_output)
|
| 99 |
cart_button.click(view_cart, outputs=cart_output)
|
| 100 |
|
| 101 |
demo.launch()
|