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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -4,25 +4,33 @@ import pandas as pd
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
 
 
4
  # Load menu data from Excel file
5
  def load_menu(file_path="menu.xlsx"):
6
  """
7
+ Load menu data from an Excel file and dynamically map its columns to required fields.
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
+ # Map dynamic column names to required fields
14
+ column_mapping = {
15
+ "name": next((col for col in menu_df.columns if "name" in col), None),
16
+ "price": next((col for col in menu_df.columns if "price" in col), None),
17
+ "description": next((col for col in menu_df.columns if "description" in col), None),
18
+ "image": next((col for col in menu_df.columns if "image" in col), None),
19
+ }
20
+
21
+ # Ensure all required columns are present
22
+ missing_columns = [key for key, col in column_mapping.items() if col is None]
23
+ if missing_columns:
24
+ raise ValueError(f"Excel file is missing one or more required columns: {missing_columns}")
25
 
26
  # Map data to required structure
27
  menu = []
28
  for _, row in menu_df.iterrows():
29
  menu.append({
30
+ "name": row[column_mapping["name"]],
31
+ "price": row[column_mapping["price"]],
32
+ "description": row[column_mapping["description"]],
33
+ "image": row[column_mapping["image"]],
34
  })
35
  return menu
36