Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| # Load menu from Excel file | |
| def load_menu(file_path="menu.xlsx"): | |
| """ | |
| Load menu data from an Excel file dynamically based on column headers. | |
| """ | |
| menu_df = pd.read_excel(file_path) | |
| # Automatically detect column names | |
| columns = menu_df.columns.str.strip() # Trim column names to avoid issues with spaces | |
| name_column = [col for col in columns if "name" in col.lower()][0] | |
| price_column = [col for col in columns if "price" in col.lower()][0] | |
| description_column = [col for col in columns if "description" in col.lower()][0] | |
| image_column = [col for col in columns if "image" in col.lower()][0] | |
| menu = [] | |
| for _, row in menu_df.iterrows(): | |
| menu.append({ | |
| "name": row[name_column], | |
| "price": row[price_column], | |
| "description": row[description_column], | |
| "image": row[image_column] | |
| }) | |
| return menu | |
| # Initialize the menu | |
| menu_data = load_menu() | |
| cart = [] | |
| def display_menu(): | |
| """ | |
| Function to display the menu in a formatted style with an 'Add' button for each item. | |
| """ | |
| menu_html = "" | |
| for item in menu_data: | |
| menu_html += f""" | |
| <div style="display: flex; margin-bottom: 15px; align-items: center; border: 1px solid #ddd; padding: 10px; border-radius: 5px;"> | |
| <img src="{item['image']}" alt="{item['name']}" style="width: 100px; height: 100px; margin-right: 15px; border-radius: 5px;"> | |
| <div style="flex-grow: 1;"> | |
| <h4 style="margin: 0;">{item['name']} - ${item['price']}</h4> | |
| <p style="margin: 5px 0; font-size: 12px;">{item['description']}</p> | |
| </div> | |
| <button 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> | |
| </div> | |
| """ | |
| return menu_html | |
| def add_to_cart(food_item, quantity): | |
| """ | |
| Add an item to the cart with its price and quantity. | |
| """ | |
| for item in menu_data: | |
| if item["name"] == food_item: | |
| cart.append({ | |
| "item": food_item, | |
| "quantity": quantity, | |
| "price": item["price"] * quantity | |
| }) | |
| break | |
| cart_summary = "<ul>" | |
| total_price = 0 | |
| for item in cart: | |
| cart_summary += f"<li>{item['item']} (x{item['quantity']}) - ${item['price']}</li>" | |
| total_price += item["price"] | |
| cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>" | |
| return f"{food_item} added to cart!\n\n{cart_summary}" | |
| def view_cart(): | |
| """ | |
| View the cart items and calculate the total price. | |
| """ | |
| if not cart: | |
| return "Your cart is empty." | |
| cart_summary = "<h3>Cart Summary:</h3><ul>" | |
| total_price = 0 | |
| for item in cart: | |
| cart_summary += f"<li>{item['item']} (x{item['quantity']}) - ${item['price']}</li>" | |
| total_price += item["price"] | |
| cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>" | |
| return cart_summary | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Dynamic Food Menu") | |
| # Display menu | |
| menu_display = gr.HTML(value=display_menu()) | |
| # Cart interactions | |
| with gr.Row(): | |
| food_item_input = gr.Textbox(label="Food Item", placeholder="Enter item name") | |
| quantity_input = gr.Number(label="Quantity", value=1, precision=0) | |
| add_button = gr.Button("Add to Cart") | |
| cart_button = gr.Button("View Cart") | |
| cart_output = gr.HTML() | |
| add_button.click(add_to_cart, inputs=[food_item_input, quantity_input], outputs=cart_output) | |
| cart_button.click(view_cart, outputs=cart_output) | |
| demo.launch() | |