DSatishchandra commited on
Commit
057b451
·
verified ·
1 Parent(s): 2eb1a73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -40
app.py CHANGED
@@ -9,59 +9,82 @@ def load_menu():
9
  except Exception as e:
10
  raise ValueError(f"Error loading menu file: {e}")
11
 
12
- # Function to filter menu items based on preference
13
- def filter_menu(preference):
14
- # Load menu data
15
  menu_data = load_menu()
 
16
 
17
- # Check for the required columns
18
- required_columns = ["Category", "Dish Name", "Price ($)", "Image URL", "Description"]
19
- for col in required_columns:
20
- if col not in menu_data.columns:
21
- raise ValueError(f"Missing column in Excel file: {col}")
22
-
23
- # Filter data based on preference
24
- if preference != "All":
25
- filtered_data = menu_data[menu_data["Category"].str.contains(preference, case=False, na=False)]
26
- else:
27
- filtered_data = menu_data
28
 
29
- # Generate HTML for the menu
30
- html_content = ""
31
- for _, item in filtered_data.iterrows():
32
- html_content += f"""
33
- <div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
34
- <div style="flex: 1; margin-right: 15px;">
35
- <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
36
- <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
37
- <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
38
- </div>
39
- <div style="flex-shrink: 0; text-align: center;">
40
- <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
41
- <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
42
- </div>
 
43
  </div>
44
- """
 
 
 
 
 
45
  return html_content
46
 
47
  # Gradio app definition
48
  def app():
49
- with gr.Blocks(title="Dynamic Menu with Preferences") as demo:
50
- # Add a title
51
- gr.Markdown("## Dynamic Menu with Preferences")
 
 
 
 
 
 
52
 
53
- # Radio button for selecting preference
54
- selected_preference = gr.Radio(
55
- choices=["All", "Vegan", "Halal", "Guilt-Free"],
56
- value="All",
57
- label="Choose a Preference",
58
  )
59
 
60
- # Output area for menu items
61
- menu_output = gr.HTML(value=filter_menu("All"))
62
 
63
  # Define interactivity
64
- selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  return demo
67
 
 
9
  except Exception as e:
10
  raise ValueError(f"Error loading menu file: {e}")
11
 
12
+ # Function to get unique categories
13
+ def get_categories():
 
14
  menu_data = load_menu()
15
+ return list(menu_data["Category"].unique())
16
 
17
+ # Function to get dishes for a selected category
18
+ def get_dishes(category):
19
+ menu_data = load_menu()
20
+ dishes = menu_data[menu_data["Category"] == category]["Dish Name"].tolist()
21
+ return dishes
 
 
 
 
 
 
22
 
23
+ # Function to display the menu for a selected dish
24
+ def get_menu_item(dish_name):
25
+ menu_data = load_menu()
26
+ item = menu_data[menu_data["Dish Name"] == dish_name]
27
+ if item.empty:
28
+ return "No item found for the selected dish."
29
+
30
+ # Extract item details
31
+ item = item.iloc[0]
32
+ html_content = f"""
33
+ <div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
34
+ <div style="flex: 1; margin-right: 15px;">
35
+ <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
36
+ <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
37
+ <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
38
  </div>
39
+ <div style="flex-shrink: 0; text-align: center;">
40
+ <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
41
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
42
+ </div>
43
+ </div>
44
+ """
45
  return html_content
46
 
47
  # Gradio app definition
48
  def app():
49
+ with gr.Blocks(title="Dynamic Menu with Categories") as demo:
50
+ gr.Markdown("## Dynamic Menu with Categories")
51
+
52
+ # Dropdown for selecting a category
53
+ category_dropdown = gr.Dropdown(
54
+ choices=get_categories(),
55
+ value="Appetizer",
56
+ label="Select a Category",
57
+ )
58
 
59
+ # Dropdown for selecting a dish
60
+ dish_dropdown = gr.Dropdown(
61
+ choices=get_dishes("Appetizer"),
62
+ value="Samosa",
63
+ label="Select a Dish",
64
  )
65
 
66
+ # Output area for the selected menu item
67
+ menu_output = gr.HTML()
68
 
69
  # Define interactivity
70
+ def update_dishes(category):
71
+ return gr.update(choices=get_dishes(category), value=get_dishes(category)[0])
72
+
73
+ category_dropdown.change(
74
+ update_dishes,
75
+ inputs=[category_dropdown],
76
+ outputs=[dish_dropdown]
77
+ )
78
+
79
+ dish_dropdown.change(
80
+ get_menu_item,
81
+ inputs=[dish_dropdown],
82
+ outputs=[menu_output]
83
+ )
84
+
85
+ # Layout
86
+ gr.Row([category_dropdown, dish_dropdown])
87
+ menu_output.render()
88
 
89
  return demo
90