dschandra commited on
Commit
078431c
·
verified ·
1 Parent(s): 7ad0ad6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -25
app.py CHANGED
@@ -1,50 +1,51 @@
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 dynamically based on column headers.
8
  """
9
  menu_df = pd.read_excel(file_path)
 
 
10
 
11
- # Automatically normalize column names
12
- columns = menu_df.columns.str.strip() # Trim leading/trailing spaces
13
- name_column = [col for col in columns if "name" in col.lower()][0]
14
- price_column = [col for col in columns if "price" in col.lower()][0]
15
- description_column = [col for col in columns if "description" in col.lower()][0]
16
- image_column = [col for col in columns if "image" in col.lower()][0]
17
-
18
  menu = []
19
  for _, row in menu_df.iterrows():
20
  menu.append({
21
- "name": row[name_column],
22
- "price": row[price_column],
23
- "description": row[description_column],
24
- "image": row[image_column]
25
  })
26
  return menu
27
 
28
  # Initialize menu and cart
29
- menu_data = load_menu()
30
  cart = []
31
 
32
  def display_menu():
33
  """
34
- Function to render the menu as HTML.
35
  """
36
  menu_html = ""
37
  for item in menu_data:
38
  menu_html += f"""
39
  <div style="display: flex; margin-bottom: 15px; align-items: center; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
40
- <img src="{item['Image']}" alt="{item['Name']}" style="width: 100px; height: 100px; margin-right: 15px; border-radius: 5px;">
41
  <div style="flex-grow: 1;">
42
- <h4 style="margin: 0;">{item['Name']} - ${item['Price']}</h4>
43
- <p style="margin: 5px 0; font-size: 12px;">{item['Description']}</p>
44
- <label for="quantity-{item['Name']}" style="margin-right: 10px;">Quantity:</label>
45
- <input type="number" id="quantity-{item['Name']}" value="1" min="1" style="width: 50px;">
46
  </div>
47
- <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>
48
  </div>
49
  """
50
  return menu_html
@@ -62,8 +63,8 @@ def add_to_cart(item_name, quantity):
62
  else:
63
  # Add new item to cart
64
  for item in menu_data:
65
- if item["Name"] == item_name:
66
- cart.append({"name": item_name, "price": item["Price"], "quantity": quantity})
67
  break
68
  return f"{item_name} added to cart! Total items: {len(cart)}"
69
 
@@ -120,6 +121,3 @@ with gr.Blocks() as demo:
120
  demo += notification
121
 
122
  demo.launch()
123
-
124
-
125
-
 
1
  import gradio as gr
2
  import pandas as pd
3
 
4
+ # Load menu data 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
+ # Normalize column names (strip spaces and make lowercase)
11
+ menu_df.columns = menu_df.columns.str.strip().str.lower()
12
 
13
+ # Check for required columns
14
+ required_columns = {"name", "price", "description", "image"}
15
+ if not required_columns.issubset(set(menu_df.columns)):
16
+ raise ValueError(f"Excel file is missing one or more required columns: {required_columns}")
17
+
18
+ # Map data to required structure
 
19
  menu = []
20
  for _, row in menu_df.iterrows():
21
  menu.append({
22
+ "name": row["name"],
23
+ "price": row["price"],
24
+ "description": row["description"],
25
+ "image": row["image"]
26
  })
27
  return menu
28
 
29
  # Initialize menu and cart
30
+ menu_data = load_menu("menu.xlsx")
31
  cart = []
32
 
33
  def display_menu():
34
  """
35
+ Render the menu dynamically as HTML.
36
  """
37
  menu_html = ""
38
  for item in menu_data:
39
  menu_html += f"""
40
  <div style="display: flex; margin-bottom: 15px; align-items: center; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
41
+ <img src="{item['image']}" alt="{item['name']}" style="width: 100px; height: 100px; margin-right: 15px; border-radius: 5px;">
42
  <div style="flex-grow: 1;">
43
+ <h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
44
+ <p style="margin: 5px 0; font-size: 12px;">{item['description']}</p>
45
+ <label for="quantity-{item['name']}" style="margin-right: 10px;">Quantity:</label>
46
+ <input type="number" id="quantity-{item['name']}" value="1" min="1" style="width: 50px;">
47
  </div>
48
+ <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>
49
  </div>
50
  """
51
  return menu_html
 
63
  else:
64
  # Add new item to cart
65
  for item in menu_data:
66
+ if item["name"] == item_name:
67
+ cart.append({"name": item_name, "price": item["price"], "quantity": quantity})
68
  break
69
  return f"{item_name} added to cart! Total items: {len(cart)}"
70
 
 
121
  demo += notification
122
 
123
  demo.launch()