nagasurendra commited on
Commit
92b51ea
·
verified ·
1 Parent(s): 91f92dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -1,16 +1,20 @@
1
  import gradio as gr
2
  import pandas as pd
3
 
4
- # Function to load menu data
5
  def load_menu():
 
6
  try:
7
- return pd.read_excel("menu.xlsx")
8
  except Exception as e:
9
  raise ValueError(f"Error loading menu file: {e}")
10
 
11
- # Function to filter menu items
12
  def filter_menu(preference):
 
13
  menu_data = load_menu()
 
 
14
  if preference == "Halal/Non-Veg":
15
  filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
16
  elif preference == "Vegetarian":
@@ -20,39 +24,41 @@ def filter_menu(preference):
20
  else: # Default to "All"
21
  filtered_data = menu_data
22
 
23
- # Generate HTML for menu items
24
  html_content = ""
25
  for _, item in filtered_data.iterrows():
26
  html_content += f"""
27
  <div class="menu-card">
28
- <div class="menu-card-content">
29
  <h3>{item['Dish Name']}</h3>
 
30
  <p>{item['Description']}</p>
31
- <p><strong>${item['Price ($)']}</strong></p>
32
  </div>
33
  <div class="menu-card-actions">
34
  <img src="{item['Image URL']}" alt="{item['Dish Name']}" class="menu-card-image">
35
- <button class="add-button" onclick="openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}')">Add</button>
36
  </div>
37
  </div>
38
  """
39
  return html_content
40
 
41
- # Main Gradio App
42
  def app():
43
  with gr.Blocks(css="style.css") as demo:
44
  gr.Markdown("## Dynamic Menu with Preferences")
45
 
46
- # Menu and Filter Section
47
- preference_selector = gr.Radio(
48
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
49
  value="All",
50
- label="Choose a Preference"
51
  )
 
 
52
  menu_output = gr.HTML(value=filter_menu("All"))
53
 
54
- # Modal Logic Injected via HTML
55
- modal_logic = gr.HTML("""
56
  <div id="modal" class="modal">
57
  <div class="modal-content">
58
  <span class="close-button" onclick="closeModal()">&times;</span>
@@ -86,16 +92,20 @@ def app():
86
  </script>
87
  """)
88
 
 
 
 
89
  # Layout
90
  with gr.Row():
91
- preference_selector.change(filter_menu, inputs=[preference_selector], outputs=[menu_output])
92
  with gr.Row():
93
  menu_output
94
  with gr.Row():
95
- modal_logic
96
 
97
  return demo
98
 
99
-
100
  if __name__ == "__main__":
101
- app().launch()
 
 
1
  import gradio as gr
2
  import pandas as pd
3
 
4
+ # Function to load the menu data from Excel
5
  def load_menu():
6
+ menu_file = "menu.xlsx" # Ensure this file exists in the same directory
7
  try:
8
+ return pd.read_excel(menu_file)
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":
 
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 class="menu-card">
32
+ <div class="menu-card-details">
33
  <h3>{item['Dish Name']}</h3>
34
+ <p>${item['Price ($)']}</p>
35
  <p>{item['Description']}</p>
 
36
  </div>
37
  <div class="menu-card-actions">
38
  <img src="{item['Image URL']}" alt="{item['Dish Name']}" class="menu-card-image">
39
+ <button onclick="openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}')" class="add-button">Add</button>
40
  </div>
41
  </div>
42
  """
43
  return html_content
44
 
45
+ # Gradio app definition
46
  def app():
47
  with gr.Blocks(css="style.css") as demo:
48
  gr.Markdown("## Dynamic Menu with Preferences")
49
 
50
+ # Radio button for selecting preference
51
+ selected_preference = gr.Radio(
52
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
53
  value="All",
54
+ label="Choose a Preference",
55
  )
56
+
57
+ # Output area for menu items
58
  menu_output = gr.HTML(value=filter_menu("All"))
59
 
60
+ # Modal window (hidden by default)
61
+ modal_window = gr.HTML("""
62
  <div id="modal" class="modal">
63
  <div class="modal-content">
64
  <span class="close-button" onclick="closeModal()">&times;</span>
 
92
  </script>
93
  """)
94
 
95
+ # Interactivity
96
+ selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
97
+
98
  # Layout
99
  with gr.Row():
100
+ selected_preference
101
  with gr.Row():
102
  menu_output
103
  with gr.Row():
104
+ modal_window
105
 
106
  return demo
107
 
108
+ # Run the app
109
  if __name__ == "__main__":
110
+ demo = app()
111
+ demo.launch()