Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,69 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
menu_data =
|
| 6 |
-
"
|
| 7 |
-
"Onion Pakoda"
|
| 8 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Cart to store selected items
|
| 11 |
cart = []
|
| 12 |
|
| 13 |
def display_menu():
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
for item
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
def add_to_cart(
|
| 23 |
"""
|
| 24 |
-
Add an item to the cart with
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
quantity = int(quantity)
|
| 30 |
-
cart.append({
|
| 31 |
-
"foodItem": food_item,
|
| 32 |
-
"quantity": quantity,
|
| 33 |
-
"price": menu_data[food_item]['price'] * quantity,
|
| 34 |
-
"suggestions": menu_data[food_item]['suggestions']
|
| 35 |
-
})
|
| 36 |
-
|
| 37 |
-
cart_summary = "\n".join([f"{item['foodItem']} (x{item['quantity']}) - ${item['price']}" for item in cart])
|
| 38 |
-
return f"{food_item} added to cart!\n\nCart Summary:\n{cart_summary}"
|
| 39 |
|
| 40 |
def view_cart():
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
"""
|
| 44 |
if not cart:
|
| 45 |
return "Your cart is empty."
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
# Gradio Interface
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
gr.Markdown("# Dynamic Food Menu")
|
| 52 |
|
| 53 |
# Display menu
|
| 54 |
-
|
| 55 |
|
| 56 |
-
#
|
| 57 |
with gr.Row():
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
add_button = gr.Button("Add to Cart")
|
| 61 |
-
|
| 62 |
-
# Output field for cart actions
|
| 63 |
-
cart_output = gr.Textbox(label="Cart", interactive=False)
|
| 64 |
-
|
| 65 |
-
# View cart button
|
| 66 |
-
view_cart_button = gr.Button("View Cart")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
add_button.click(add_to_cart, inputs=[food_item_input, quantity_input], outputs=cart_output)
|
| 70 |
-
view_cart_button.click(view_cart, outputs=cart_output)
|
| 71 |
|
| 72 |
-
# Launch Gradio App
|
| 73 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
# Simulated menu data
|
| 5 |
+
menu_data = [
|
| 6 |
+
{"name": "Samosa", "price": 10, "description": "Crispy fried pastry filled with spiced potatoes and peas. Serves 1. [Energy: 200 kcal, Protein: 5g, Carbohydrates: 25g, Fiber: 3g, Fat: 10g, Sugar: 1g]", "image": "images/samosa.jpg"},
|
| 7 |
+
{"name": "Onion Pakoda", "price": 14, "description": "Deep-fried onion fritters seasoned with herbs and spices. Serves 2. [Energy: 250 kcal, Protein: 6g, Carbohydrates: 30g, Fiber: 4g, Fat: 12g, Sugar: 2g]", "image": "images/onion-pakoda.jpg"},
|
| 8 |
+
{"name": "Paneer Tikka", "price": 15, "description": "Grilled paneer cubes marinated in spices. Serves 1. [Energy: 300 kcal, Protein: 15g, Carbohydrates: 10g, Fiber: 2g, Fat: 20g, Sugar: 1g]", "image": "images/paneer-tikka.jpg"},
|
| 9 |
+
{"name": "Veg Biryani", "price": 12, "description": "Aromatic rice cooked with vegetables and spices. Serves 1. [Energy: 350 kcal, Protein: 8g, Carbohydrates: 50g, Fiber: 5g, Fat: 12g, Sugar: 2g]", "image": "images/veg-biryani.jpg"},
|
| 10 |
+
{"name": "Chicken Curry", "price": 16, "description": "Tender chicken pieces cooked in a spicy curry. Serves 1. [Energy: 400 kcal, Protein: 25g, Carbohydrates: 10g, Fiber: 2g, Fat: 25g, Sugar: 3g]", "image": "images/chicken-curry.jpg"},
|
| 11 |
+
{"name": "Masala Dosa", "price": 10, "description": "Thin rice crepe stuffed with spiced potato filling. Serves 1. [Energy: 300 kcal, Protein: 6g, Carbohydrates: 45g, Fiber: 4g, Fat: 8g, Sugar: 1g]", "image": "images/masala-dosa.jpg"}
|
| 12 |
+
]
|
| 13 |
|
|
|
|
| 14 |
cart = []
|
| 15 |
|
| 16 |
def display_menu():
|
| 17 |
"""
|
| 18 |
+
Function to display the menu in a formatted style with an 'Add' button for each item.
|
| 19 |
"""
|
| 20 |
+
menu_html = ""
|
| 21 |
+
for item in menu_data:
|
| 22 |
+
menu_html += f"""
|
| 23 |
+
<div style="display: flex; margin-bottom: 15px; align-items: center; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
|
| 24 |
+
<img src="{item['image']}" alt="{item['name']}" style="width: 100px; height: 100px; margin-right: 15px; border-radius: 5px;">
|
| 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']}', {item['price']})">Add</button>
|
| 30 |
+
</div>
|
| 31 |
+
"""
|
| 32 |
+
return menu_html
|
| 33 |
|
| 34 |
+
def add_to_cart(item_name, item_price):
|
| 35 |
"""
|
| 36 |
+
Add an item to the cart with its price.
|
| 37 |
"""
|
| 38 |
+
cart.append({"item": item_name, "price": item_price})
|
| 39 |
+
return f"Added {item_name} to the cart!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def view_cart():
|
| 42 |
"""
|
| 43 |
+
View the cart items and calculate the total price.
|
| 44 |
"""
|
| 45 |
if not cart:
|
| 46 |
return "Your cart is empty."
|
| 47 |
+
|
| 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
|
| 55 |
|
|
|
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
gr.Markdown("# Dynamic Food Menu")
|
| 58 |
|
| 59 |
# Display menu
|
| 60 |
+
menu_display = gr.HTML(value=display_menu())
|
| 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()
|