nagasurendra commited on
Commit
807b0c3
·
verified ·
1 Parent(s): 2b426ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -44
app.py CHANGED
@@ -1,60 +1,53 @@
1
  import gradio as gr
2
  import pandas as pd
3
 
4
- # Load menu data from Excel
5
  def load_menu():
6
  try:
7
- return pd.read_excel("menu.xlsx") # Ensure menu.xlsx exists in the same directory
8
  except Exception as e:
9
  raise ValueError(f"Error loading menu file: {e}")
10
 
11
- # Function to filter menu items based on preference
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":
17
- filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
18
- elif preference == "Guilt-Free":
19
- filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
20
- else: # Default to "All"
21
- filtered_data = menu_data
22
-
23
- # Generate HTML for the menu
24
  html_content = ""
25
- for _, item in filtered_data.iterrows():
26
  html_content += f"""
27
- <div class="menu-card">
28
- <img src="{item['Image URL']}" alt="{item['Dish Name']}" class="menu-image">
29
- <h3 class="menu-title">{item['Dish Name']}</h3>
30
- <p class="menu-description">{item['Description']}</p>
31
- <p class="menu-price"><strong>${item['Price ($)']}</strong></p>
32
- <button onclick="showModal('{item['Dish Name']}')" class="add-button">Add</button>
 
 
 
 
33
  </div>
34
  """
35
  return html_content
36
 
37
- # Function to get dish details for the modal
38
  def get_dish_details(dish_name):
39
  menu_data = load_menu()
40
  try:
41
  dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
42
  return (
43
  dish["Image URL"],
44
- dish_name,
45
  dish["Description"],
46
- f"${dish['Price ($)']}",
47
- dish["Ingredients"]
48
  )
49
  except IndexError:
50
  raise ValueError(f"Dish '{dish_name}' not found!")
51
 
52
- # Main Gradio app
53
  def app():
54
  with gr.Blocks(css="style.css") as demo:
55
- gr.Markdown("## Dynamic Menu with Modal Suggestion Page")
56
-
57
- # Homepage
58
  preference_selector = gr.Radio(
59
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
60
  value="All",
@@ -62,8 +55,8 @@ def app():
62
  )
63
  menu_output = gr.HTML(value=filter_menu("All"))
64
 
65
- # Modal Window
66
- modal = gr.Column(visible=False, elem_classes=["popup"])
67
  modal_image = gr.Image(label="Dish Image")
68
  modal_name = gr.Markdown()
69
  modal_description = gr.Markdown()
@@ -75,44 +68,39 @@ def app():
75
  add_to_cart_button = gr.Button("Add to Cart")
76
  close_modal_button = gr.Button("Close")
77
 
78
- # Cart
79
  cart_state = gr.State([])
80
  cart_output = gr.HTML(value="Your cart is empty.")
81
 
82
  # Handlers
83
- def update_menu(preference):
84
- return filter_menu(preference)
85
-
86
  def show_modal(dish_name):
87
- img, name, desc, price, ingredients = get_dish_details(dish_name)
88
  return (
89
  gr.update(visible=True),
90
  img,
91
  f"### {name}",
92
- f"**Description:** {desc}\n\n**Ingredients:** {ingredients}",
93
  price
94
  )
95
 
96
  def close_modal():
97
  return gr.update(visible=False)
98
 
99
- def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
100
  cart.append({
101
- "name": dish_name,
102
- "spice_level": spice_level,
103
  "extras": extras,
104
- "quantity": quantity,
105
  "instructions": instructions
106
  })
107
  cart_html = "<br>".join(
108
- [f"{item['quantity']}x {item['name']} - {item['spice_level']} (Extras: {', '.join(item['extras'])})"
109
- for item in cart]
110
  )
111
  return cart, cart_html
112
 
113
  # Events
114
- preference_selector.change(update_menu, inputs=[preference_selector], outputs=[menu_output])
115
- menu_output.change(show_modal, inputs=[menu_output], outputs=[modal, modal_image, modal_name, modal_description, modal_price])
116
  close_modal_button.click(close_modal, outputs=[modal])
117
  add_to_cart_button.click(add_to_cart, inputs=[modal_name, spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_output])
118
 
 
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
+ # Generate the menu content
12
  def filter_menu(preference):
13
  menu_data = load_menu()
14
+ # Filter logic remains the same
15
+ # Generating menu HTML
 
 
 
 
 
 
 
 
16
  html_content = ""
17
+ for _, item in menu_data.iterrows():
18
  html_content += f"""
19
+ <div class="menu-item">
20
+ <div class="menu-details">
21
+ <h3>{item['Dish Name']}</h3>
22
+ <p>{item['Description']}</p>
23
+ <p><strong>${item['Price ($)']}</strong></p>
24
+ </div>
25
+ <div class="menu-actions">
26
+ <img src="{item['Image URL']}" alt="{item['Dish Name']}" class="menu-img">
27
+ <button class="add-button" onclick="showModal('{item['Dish Name']}')">Add</button>
28
+ </div>
29
  </div>
30
  """
31
  return html_content
32
 
33
+ # Get dish details for the modal
34
  def get_dish_details(dish_name):
35
  menu_data = load_menu()
36
  try:
37
  dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
38
  return (
39
  dish["Image URL"],
40
+ dish["Dish Name"],
41
  dish["Description"],
42
+ f"${dish['Price ($)']}"
 
43
  )
44
  except IndexError:
45
  raise ValueError(f"Dish '{dish_name}' not found!")
46
 
47
+ # Gradio App
48
  def app():
49
  with gr.Blocks(css="style.css") as demo:
50
+ # Menu Filtering Section
 
 
51
  preference_selector = gr.Radio(
52
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
53
  value="All",
 
55
  )
56
  menu_output = gr.HTML(value=filter_menu("All"))
57
 
58
+ # Modal Window (initially hidden)
59
+ modal = gr.Column(visible=False, elem_classes=["modal"])
60
  modal_image = gr.Image(label="Dish Image")
61
  modal_name = gr.Markdown()
62
  modal_description = gr.Markdown()
 
68
  add_to_cart_button = gr.Button("Add to Cart")
69
  close_modal_button = gr.Button("Close")
70
 
71
+ # Cart Section
72
  cart_state = gr.State([])
73
  cart_output = gr.HTML(value="Your cart is empty.")
74
 
75
  # Handlers
 
 
 
76
  def show_modal(dish_name):
77
+ img, name, desc, price = get_dish_details(dish_name)
78
  return (
79
  gr.update(visible=True),
80
  img,
81
  f"### {name}",
82
+ desc,
83
  price
84
  )
85
 
86
  def close_modal():
87
  return gr.update(visible=False)
88
 
89
+ def add_to_cart(name, spice, extras, qty, instructions, cart):
90
  cart.append({
91
+ "name": name,
92
+ "spice_level": spice,
93
  "extras": extras,
94
+ "quantity": qty,
95
  "instructions": instructions
96
  })
97
  cart_html = "<br>".join(
98
+ [f"{item['quantity']}x {item['name']} ({item['spice_level']})" for item in cart]
 
99
  )
100
  return cart, cart_html
101
 
102
  # Events
103
+ preference_selector.change(filter_menu, inputs=[preference_selector], outputs=[menu_output])
 
104
  close_modal_button.click(close_modal, outputs=[modal])
105
  add_to_cart_button.click(add_to_cart, inputs=[modal_name, spice_level, extras, quantity, special_instructions, cart_state], outputs=[cart_state, cart_output])
106