Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|