Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| # Menu data (simulate database) | |
| menu_data = { | |
| "Veg Samosa": {"price": 9, "suggestions": ["Green Chutney", "Masala Tea"]}, | |
| "Onion Pakoda": {"price": 10, "suggestions": ["Tamarind Sauce", "Hot Tea"]}, | |
| } | |
| # Cart to store selected items | |
| cart = [] | |
| def display_menu(): | |
| """ | |
| Display menu items in a user-friendly format. | |
| """ | |
| menu_list = [] | |
| for item, details in menu_data.items(): | |
| menu_list.append(f"{item} - ${details['price']}") | |
| return "\n".join(menu_list) | |
| def add_to_cart(food_item, quantity): | |
| """ | |
| Add an item to the cart with quantity and suggestions. | |
| """ | |
| if food_item not in menu_data: | |
| return "Error: Item not found in menu." | |
| quantity = int(quantity) | |
| cart.append({ | |
| "foodItem": food_item, | |
| "quantity": quantity, | |
| "price": menu_data[food_item]['price'] * quantity, | |
| "suggestions": menu_data[food_item]['suggestions'] | |
| }) | |
| cart_summary = "\n".join([f"{item['foodItem']} (x{item['quantity']}) - ${item['price']}" for item in cart]) | |
| return f"{food_item} added to cart!\n\nCart Summary:\n{cart_summary}" | |
| def view_cart(): | |
| """ | |
| Display the current cart contents. | |
| """ | |
| if not cart: | |
| return "Your cart is empty." | |
| cart_summary = "\n".join([f"{item['foodItem']} (x{item['quantity']}) - ${item['price']}" for item in cart]) | |
| return f"Cart Summary:\n{cart_summary}" | |
| # Gradio Interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Dynamic Food Menu") | |
| # Display menu | |
| menu_output = gr.Textbox(label="Menu", value=display_menu(), interactive=False) | |
| # Input fields for adding to cart | |
| 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") | |
| # Output field for cart actions | |
| cart_output = gr.Textbox(label="Cart", interactive=False) | |
| # View cart button | |
| view_cart_button = gr.Button("View Cart") | |
| # Actions | |
| add_button.click(add_to_cart, inputs=[food_item_input, quantity_input], outputs=cart_output) | |
| view_cart_button.click(view_cart, outputs=cart_output) | |
| # Launch Gradio App | |
| demo.launch() | |