Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ def load_menu(file_path="menu.xlsx"):
|
|
| 7 |
Load menu data from an Excel file dynamically based on column headers.
|
| 8 |
"""
|
| 9 |
menu_df = pd.read_excel(file_path)
|
| 10 |
-
|
| 11 |
# Automatically detect column names
|
| 12 |
columns = menu_df.columns.str.strip() # Trim column names to avoid issues with spaces
|
| 13 |
name_column = [col for col in columns if "name" in col.lower()][0]
|
|
@@ -25,9 +24,8 @@ def load_menu(file_path="menu.xlsx"):
|
|
| 25 |
})
|
| 26 |
return menu
|
| 27 |
|
| 28 |
-
# Initialize the menu
|
| 29 |
menu_data = load_menu()
|
| 30 |
-
|
| 31 |
cart = []
|
| 32 |
|
| 33 |
def display_menu():
|
|
@@ -60,13 +58,7 @@ def add_to_cart(food_item, quantity):
|
|
| 60 |
"price": item["price"] * quantity
|
| 61 |
})
|
| 62 |
break
|
| 63 |
-
|
| 64 |
-
total_price = 0
|
| 65 |
-
for item in cart:
|
| 66 |
-
cart_summary += f"<li>{item['item']} (x{item['quantity']}) - ${item['price']}</li>"
|
| 67 |
-
total_price += item["price"]
|
| 68 |
-
cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>"
|
| 69 |
-
return f"{food_item} added to cart!\n\n{cart_summary}"
|
| 70 |
|
| 71 |
def view_cart():
|
| 72 |
"""
|
|
@@ -83,21 +75,27 @@ def view_cart():
|
|
| 83 |
cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>"
|
| 84 |
return cart_summary
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("# Dynamic Food Menu")
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
menu_display = gr.HTML(
|
|
|
|
| 91 |
|
| 92 |
-
#
|
| 93 |
with gr.Row():
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
add_button = gr.Button("Add to Cart")
|
| 97 |
-
cart_button = gr.Button("View Cart")
|
| 98 |
-
cart_output = gr.HTML()
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
|
| 103 |
demo.launch()
|
|
|
|
| 7 |
Load menu data from an Excel file dynamically based on column headers.
|
| 8 |
"""
|
| 9 |
menu_df = pd.read_excel(file_path)
|
|
|
|
| 10 |
# Automatically detect column names
|
| 11 |
columns = menu_df.columns.str.strip() # Trim column names to avoid issues with spaces
|
| 12 |
name_column = [col for col in columns if "name" in col.lower()][0]
|
|
|
|
| 24 |
})
|
| 25 |
return menu
|
| 26 |
|
| 27 |
+
# Initialize the menu and cart
|
| 28 |
menu_data = load_menu()
|
|
|
|
| 29 |
cart = []
|
| 30 |
|
| 31 |
def display_menu():
|
|
|
|
| 58 |
"price": item["price"] * quantity
|
| 59 |
})
|
| 60 |
break
|
| 61 |
+
return f"{food_item} added to cart!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
def view_cart():
|
| 64 |
"""
|
|
|
|
| 75 |
cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>"
|
| 76 |
return cart_summary
|
| 77 |
|
| 78 |
+
# Gradio Interface with Pages
|
| 79 |
+
def menu_page():
|
| 80 |
+
return gr.update(value=display_menu(), visible=True), gr.update(visible=False)
|
| 81 |
+
|
| 82 |
+
def cart_page():
|
| 83 |
+
return gr.update(visible=False), gr.update(value=view_cart(), visible=True)
|
| 84 |
+
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
gr.Markdown("# Dynamic Food Menu")
|
| 87 |
|
| 88 |
+
# Menu and Cart Views
|
| 89 |
+
menu_display = gr.HTML(visible=True)
|
| 90 |
+
cart_display = gr.HTML(visible=False)
|
| 91 |
|
| 92 |
+
# Navigation Buttons
|
| 93 |
with gr.Row():
|
| 94 |
+
menu_button = gr.Button("Menu")
|
| 95 |
+
cart_button = gr.Button("Cart")
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
# Event Handling for Navigation
|
| 98 |
+
menu_button.click(menu_page, outputs=[menu_display, cart_display])
|
| 99 |
+
cart_button.click(cart_page, outputs=[menu_display, cart_display])
|
| 100 |
|
| 101 |
demo.launch()
|