Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,51 @@
|
|
| 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 dynamically based on column headers.
|
| 8 |
"""
|
| 9 |
menu_df = pd.read_excel(file_path)
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
menu = []
|
| 19 |
for _, row in menu_df.iterrows():
|
| 20 |
menu.append({
|
| 21 |
-
"name": row[
|
| 22 |
-
"price": row[
|
| 23 |
-
"description": row[
|
| 24 |
-
"image": row[
|
| 25 |
})
|
| 26 |
return menu
|
| 27 |
|
| 28 |
# Initialize menu and cart
|
| 29 |
-
menu_data = load_menu()
|
| 30 |
cart = []
|
| 31 |
|
| 32 |
def display_menu():
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
"""
|
| 36 |
menu_html = ""
|
| 37 |
for item in menu_data:
|
| 38 |
menu_html += f"""
|
| 39 |
<div style="display: flex; margin-bottom: 15px; align-items: center; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
|
| 40 |
-
<img src="{item['
|
| 41 |
<div style="flex-grow: 1;">
|
| 42 |
-
<h4 style="margin: 0;">{item['
|
| 43 |
-
<p style="margin: 5px 0; font-size: 12px;">{item['
|
| 44 |
-
<label for="quantity-{item['
|
| 45 |
-
<input type="number" id="quantity-{item['
|
| 46 |
</div>
|
| 47 |
-
<button onclick="add_to_cart('{item['
|
| 48 |
</div>
|
| 49 |
"""
|
| 50 |
return menu_html
|
|
@@ -62,8 +63,8 @@ def add_to_cart(item_name, quantity):
|
|
| 62 |
else:
|
| 63 |
# Add new item to cart
|
| 64 |
for item in menu_data:
|
| 65 |
-
if item["
|
| 66 |
-
cart.append({"name": item_name, "price": item["
|
| 67 |
break
|
| 68 |
return f"{item_name} added to cart! Total items: {len(cart)}"
|
| 69 |
|
|
@@ -120,6 +121,3 @@ with gr.Blocks() as demo:
|
|
| 120 |
demo += notification
|
| 121 |
|
| 122 |
demo.launch()
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
# Load menu data from Excel file
|
| 5 |
def load_menu(file_path="menu.xlsx"):
|
| 6 |
"""
|
| 7 |
Load menu data from an Excel file dynamically based on column headers.
|
| 8 |
"""
|
| 9 |
menu_df = pd.read_excel(file_path)
|
| 10 |
+
# Normalize column names (strip spaces and make lowercase)
|
| 11 |
+
menu_df.columns = menu_df.columns.str.strip().str.lower()
|
| 12 |
|
| 13 |
+
# Check for required columns
|
| 14 |
+
required_columns = {"name", "price", "description", "image"}
|
| 15 |
+
if not required_columns.issubset(set(menu_df.columns)):
|
| 16 |
+
raise ValueError(f"Excel file is missing one or more required columns: {required_columns}")
|
| 17 |
+
|
| 18 |
+
# Map data to required structure
|
|
|
|
| 19 |
menu = []
|
| 20 |
for _, row in menu_df.iterrows():
|
| 21 |
menu.append({
|
| 22 |
+
"name": row["name"],
|
| 23 |
+
"price": row["price"],
|
| 24 |
+
"description": row["description"],
|
| 25 |
+
"image": row["image"]
|
| 26 |
})
|
| 27 |
return menu
|
| 28 |
|
| 29 |
# Initialize menu and cart
|
| 30 |
+
menu_data = load_menu("menu.xlsx")
|
| 31 |
cart = []
|
| 32 |
|
| 33 |
def display_menu():
|
| 34 |
"""
|
| 35 |
+
Render the menu dynamically as HTML.
|
| 36 |
"""
|
| 37 |
menu_html = ""
|
| 38 |
for item in menu_data:
|
| 39 |
menu_html += f"""
|
| 40 |
<div style="display: flex; margin-bottom: 15px; align-items: center; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
|
| 41 |
+
<img src="{item['image']}" alt="{item['name']}" style="width: 100px; height: 100px; margin-right: 15px; border-radius: 5px;">
|
| 42 |
<div style="flex-grow: 1;">
|
| 43 |
+
<h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
|
| 44 |
+
<p style="margin: 5px 0; font-size: 12px;">{item['description']}</p>
|
| 45 |
+
<label for="quantity-{item['name']}" style="margin-right: 10px;">Quantity:</label>
|
| 46 |
+
<input type="number" id="quantity-{item['name']}" value="1" min="1" style="width: 50px;">
|
| 47 |
</div>
|
| 48 |
+
<button onclick="add_to_cart('{item['name']}', document.getElementById('quantity-{item['name']}').value)" style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer;">Add</button>
|
| 49 |
</div>
|
| 50 |
"""
|
| 51 |
return menu_html
|
|
|
|
| 63 |
else:
|
| 64 |
# Add new item to cart
|
| 65 |
for item in menu_data:
|
| 66 |
+
if item["name"] == item_name:
|
| 67 |
+
cart.append({"name": item_name, "price": item["price"], "quantity": quantity})
|
| 68 |
break
|
| 69 |
return f"{item_name} added to cart! Total items: {len(cart)}"
|
| 70 |
|
|
|
|
| 121 |
demo += notification
|
| 122 |
|
| 123 |
demo.launch()
|
|
|
|
|
|
|
|
|