dschandra commited on
Commit
f3efaf1
·
verified ·
1 Parent(s): 8e5f103

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -45
app.py CHANGED
@@ -4,98 +4,106 @@ import pandas as pd
4
  # Load menu from Excel file
5
  def load_menu(file_path="menu.xlsx"):
6
  """
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]
13
- price_column = [col for col in columns if "price" in col.lower()][0]
14
- description_column = [col for col in columns if "description" in col.lower()][0]
15
- image_column = [col for col in columns if "image" in col.lower()][0]
16
-
17
- menu = []
18
- for _, row in menu_df.iterrows():
19
- menu.append({
20
- "name": row[name_column],
21
- "price": row[price_column],
22
- "description": row[description_column],
23
- "image": row[image_column]
24
- })
25
  return menu
26
 
27
- # Initialize the menu and cart
28
  menu_data = load_menu()
29
  cart = []
30
 
31
  def display_menu():
32
  """
33
- Function to display the menu in a formatted style with an 'Add' button for each item.
34
  """
35
  menu_html = ""
36
  for item in menu_data:
37
  menu_html += f"""
38
  <div style="display: flex; margin-bottom: 15px; align-items: center; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
39
- <img src="{item['image']}" alt="{item['name']}" style="width: 100px; height: 100px; margin-right: 15px; border-radius: 5px;">
40
  <div style="flex-grow: 1;">
41
- <h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
42
- <p style="margin: 5px 0; font-size: 12px;">{item['description']}</p>
 
 
43
  </div>
44
- <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>
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
- return f"{food_item} added to cart!"
 
 
 
 
 
 
62
 
63
  def view_cart():
64
  """
65
- View the cart items and calculate the total price.
66
  """
67
  if not cart:
68
  return "Your cart is empty."
69
 
70
- cart_summary = "<h3>Cart Summary:</h3><ul>"
71
  total_price = 0
72
  for item in cart:
73
- cart_summary += f"<li>{item['item']} (x{item['quantity']}) - ${item['price']}</li>"
74
- total_price += item["price"]
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()
 
 
 
4
  # Load menu from Excel file
5
  def load_menu(file_path="menu.xlsx"):
6
  """
7
+ Load menu data from an Excel file.
8
  """
9
  menu_df = pd.read_excel(file_path)
10
+ menu = menu_df.to_dict(orient="records")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  return menu
12
 
13
+ # Initialize menu and cart
14
  menu_data = load_menu()
15
  cart = []
16
 
17
  def display_menu():
18
  """
19
+ Function to render the menu as HTML.
20
  """
21
  menu_html = ""
22
  for item in menu_data:
23
  menu_html += f"""
24
  <div style="display: flex; margin-bottom: 15px; align-items: center; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
25
+ <img src="{item['Image']}" alt="{item['Name']}" style="width: 100px; height: 100px; margin-right: 15px; border-radius: 5px;">
26
  <div style="flex-grow: 1;">
27
+ <h4 style="margin: 0;">{item['Name']} - ${item['Price']}</h4>
28
+ <p style="margin: 5px 0; font-size: 12px;">{item['Description']}</p>
29
+ <label for="quantity-{item['Name']}" style="margin-right: 10px;">Quantity:</label>
30
+ <input type="number" id="quantity-{item['Name']}" value="1" min="1" style="width: 50px;">
31
  </div>
32
+ <button onclick="add_to_cart('{item['Name']}', document.getElementById('quantity-{item['Name']}').value)" style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer;">Add</button>
33
  </div>
34
  """
35
  return menu_html
36
 
37
+ def add_to_cart(item_name, quantity):
38
  """
39
+ Add an item to the cart and return success message with cart count.
40
  """
41
+ quantity = int(quantity)
42
+ # Check if the item already exists in the cart
43
+ for item in cart:
44
+ if item["name"] == item_name:
45
+ item["quantity"] += quantity
 
 
46
  break
47
+ else:
48
+ # Add new item to cart
49
+ for item in menu_data:
50
+ if item["Name"] == item_name:
51
+ cart.append({"name": item_name, "price": item["Price"], "quantity": quantity})
52
+ break
53
+ return f"{item_name} added to cart! Total items: {len(cart)}"
54
 
55
  def view_cart():
56
  """
57
+ Render the cart contents and calculate the total price.
58
  """
59
  if not cart:
60
  return "Your cart is empty."
61
 
62
+ cart_html = "<h3>Cart Summary:</h3><ul>"
63
  total_price = 0
64
  for item in cart:
65
+ cart_html += f"<li>{item['name']} (x{item['quantity']}) - ${item['price'] * item['quantity']}</li>"
66
+ total_price += item["price"] * item["quantity"]
67
+ cart_html += f"</ul><h4>Total Price: ${total_price}</h4>"
68
+ return cart_html
69
 
70
+ # Gradio Interface
71
  def menu_page():
72
  return gr.update(value=display_menu(), visible=True), gr.update(visible=False)
73
 
74
  def cart_page():
75
+ return gr.update(value=view_cart(), visible=True), gr.update(visible=False)
76
 
77
  with gr.Blocks() as demo:
78
  gr.Markdown("# Dynamic Food Menu")
79
 
80
  # Menu and Cart Views
81
+ menu_display = gr.HTML(value=display_menu(), visible=True)
82
  cart_display = gr.HTML(visible=False)
83
+ notification = gr.Textbox(label="Notification", value="", interactive=False)
84
 
85
  # Navigation Buttons
86
  with gr.Row():
87
+ menu_button = gr.Button("Menu")
88
+ cart_button = gr.Button("View Cart")
89
 
90
+ # Add item interaction
91
+ food_item_input = gr.Textbox(visible=False) # Hidden input to capture item name
92
+ quantity_input = gr.Number(visible=False) # Hidden input to capture quantity
93
+ add_button = gr.Button("Add to Cart", visible=False) # Simulated button for functionality
94
+
95
+ # Notification when item is added
96
+ add_button.click(add_to_cart, inputs=[food_item_input, quantity_input], outputs=notification)
97
+
98
+ # Navigation between Menu and Cart pages
99
  menu_button.click(menu_page, outputs=[menu_display, cart_display])
100
+ cart_button.click(cart_page, outputs=[cart_display, menu_display])
101
+
102
+ # Render menu and cart
103
+ demo += menu_display
104
+ demo += cart_display
105
+ demo += notification
106
 
107
  demo.launch()
108
+
109
+