nagasurendra commited on
Commit
f03d6fd
·
verified ·
1 Parent(s): b16d730

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -38
app.py CHANGED
@@ -9,44 +9,14 @@ 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
- # Define filter conditions
18
- if preference == "Halal/Non-Veg":
19
- filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
20
- elif preference == "Vegetarian":
21
- filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
22
- elif preference == "Guilt-Free":
23
- filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
24
- else: # Default to "All"
25
- filtered_data = menu_data
26
-
27
- # Generate HTML for the menu
28
- html_content = ""
29
- for _, item in filtered_data.iterrows():
30
- html_content += f"""
31
- <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);">
32
- <div style="flex: 1; margin-right: 15px;">
33
- <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
34
- <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
35
- <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
36
- </div>
37
- <div style="flex-shrink: 0; text-align: center;">
38
- <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
39
- <button onclick="showPopup('{item['Dish Name']}')" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
40
- </div>
41
- </div>
42
- """
43
- return html_content
44
-
45
  # Function to generate the popup content dynamically
46
  def generate_popup(item_name):
47
  menu_data = load_menu()
48
- item = menu_data[menu_data['Dish Name'] == item_name].iloc[0]
49
-
 
 
 
50
  # Dynamic HTML for the popup
51
  popup_content = f"""
52
  <div style="background-color: white; padding: 20px; border-radius: 8px; width: 80%; margin: auto; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1000;">
@@ -79,6 +49,38 @@ def generate_popup(item_name):
79
  """
80
  return popup_content
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  # Gradio app definition
83
  def app():
84
  with gr.Blocks(title="Dynamic Menu with Filters") as demo:
@@ -92,14 +94,13 @@ def app():
92
  )
93
 
94
  # Output area for menu items
95
- menu_output = gr.HTML(value=filter_menu("All"))
96
 
97
  # Popup area
98
  popup_output = gr.HTML()
99
 
100
  # Define interactivity
101
- selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
102
- menu_output.change(generate_popup, inputs=[], outputs=[popup_output])
103
 
104
  # Layout
105
  gr.Row([selected_preference])
 
9
  except Exception as e:
10
  raise ValueError(f"Error loading menu file: {e}")
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # Function to generate the popup content dynamically
13
  def generate_popup(item_name):
14
  menu_data = load_menu()
15
+ try:
16
+ item = menu_data[menu_data['Dish Name'] == item_name].iloc[0]
17
+ except IndexError:
18
+ return f"<p style='color: red;'>Item '{item_name}' not found!</p>"
19
+
20
  # Dynamic HTML for the popup
21
  popup_content = f"""
22
  <div style="background-color: white; padding: 20px; border-radius: 8px; width: 80%; margin: auto; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1000;">
 
49
  """
50
  return popup_content
51
 
52
+ # Function to filter menu items and include the popup functionality
53
+ def filter_menu_with_popup(preference):
54
+ menu_data = load_menu()
55
+
56
+ # Define filter conditions
57
+ if preference == "Halal/Non-Veg":
58
+ filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
59
+ elif preference == "Vegetarian":
60
+ filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
61
+ elif preference == "Guilt-Free":
62
+ filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
63
+ else: # Default to "All"
64
+ filtered_data = menu_data
65
+
66
+ # Generate HTML for the menu
67
+ html_content = ""
68
+ for _, item in filtered_data.iterrows():
69
+ html_content += f"""
70
+ <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); position: relative;">
71
+ <div style="flex: 1; margin-right: 15px;">
72
+ <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
73
+ <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
74
+ <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
75
+ </div>
76
+ <div style="flex-shrink: 0; text-align: center;">
77
+ <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
78
+ <button onclick="showPopup('{item['Dish Name']}')" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
79
+ </div>
80
+ </div>
81
+ """
82
+ return html_content
83
+
84
  # Gradio app definition
85
  def app():
86
  with gr.Blocks(title="Dynamic Menu with Filters") as demo:
 
94
  )
95
 
96
  # Output area for menu items
97
+ menu_output = gr.HTML(value=filter_menu_with_popup("All"))
98
 
99
  # Popup area
100
  popup_output = gr.HTML()
101
 
102
  # Define interactivity
103
+ selected_preference.change(filter_menu_with_popup, inputs=[selected_preference], outputs=[menu_output])
 
104
 
105
  # Layout
106
  gr.Row([selected_preference])