dschandra commited on
Commit
8edd0c3
·
verified ·
1 Parent(s): cd6ed7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -15
app.py CHANGED
@@ -1,15 +1,25 @@
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
 
@@ -25,18 +35,36 @@ def display_menu():
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
  """
@@ -48,7 +76,7 @@ def view_cart():
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
@@ -61,9 +89,13 @@ with gr.Blocks() as demo:
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()
 
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.
8
+ The Excel file should have columns: Name, Price, Description, Image
9
+ """
10
+ menu_df = pd.read_excel(file_path)
11
+ menu = []
12
+ for _, row in menu_df.iterrows():
13
+ menu.append({
14
+ "name": row["Name"],
15
+ "price": row["Price"],
16
+ "description": row["Description"],
17
+ "image": row["Image"]
18
+ })
19
+ return menu
20
+
21
+ # Initialize the menu
22
+ menu_data = load_menu()
23
 
24
  cart = []
25
 
 
35
  <div style="flex-grow: 1;">
36
  <h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
37
  <p style="margin: 5px 0; font-size: 12px;">{item['description']}</p>
38
+ <div style="display: flex; align-items: center;">
39
+ <button style="background-color: #f0f0f0; padding: 5px 10px; border-radius: 5px; margin-right: 5px;" onclick="decreaseQuantity('{item['name']}')">-</button>
40
+ <span id="quantity-{item['name']}" style="padding: 5px;">1</span>
41
+ <button style="background-color: #f0f0f0; padding: 5px 10px; border-radius: 5px; margin-left: 5px;" onclick="increaseQuantity('{item['name']}')">+</button>
42
+ </div>
43
  </div>
44
+ <button id="add-button-{item['name']}" 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>
45
  </div>
46
  """
47
  return menu_html
48
 
49
+ def add_to_cart(food_item, quantity):
50
  """
51
+ Add an item to the cart with its price and quantity.
52
  """
53
+ for item in menu_data:
54
+ if item["name"] == food_item:
55
+ cart.append({
56
+ "item": food_item,
57
+ "quantity": quantity,
58
+ "price": item["price"] * quantity
59
+ })
60
+ break
61
+ cart_summary = "<ul>"
62
+ total_price = 0
63
+ for item in cart:
64
+ cart_summary += f"<li>{item['item']} (x{item['quantity']}) - ${item['price']}</li>"
65
+ total_price += item["price"]
66
+ cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>"
67
+ return f"{food_item} added to cart!\n\n{cart_summary}"
68
 
69
  def view_cart():
70
  """
 
76
  cart_summary = "<h3>Cart Summary:</h3><ul>"
77
  total_price = 0
78
  for item in cart:
79
+ cart_summary += f"<li>{item['item']} (x{item['quantity']}) - ${item['price']}</li>"
80
  total_price += item["price"]
81
  cart_summary += f"</ul><h4>Total Price: ${total_price}</h4>"
82
  return cart_summary
 
89
 
90
  # Cart interactions
91
  with gr.Row():
92
+ food_item_input = gr.Textbox(label="Food Item", placeholder="Enter item name")
93
+ quantity_input = gr.Number(label="Quantity", value=1, precision=0)
94
+ add_button = gr.Button("Add to Cart")
95
  cart_button = gr.Button("View Cart")
96
  cart_output = gr.HTML()
97
 
98
+ add_button.click(add_to_cart, inputs=[food_item_input, quantity_input], outputs=cart_output)
99
  cart_button.click(view_cart, outputs=cart_output)
100
 
101
  demo.launch()