nagasurendra commited on
Commit
7b360d9
·
verified ·
1 Parent(s): 06b4e04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -19
app.py CHANGED
@@ -11,10 +11,8 @@ def load_menu():
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":
@@ -36,8 +34,8 @@ def filter_menu(preference):
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 value="{item['Dish Name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;"
40
- onclick="return '{item['Dish Name']}'">Add</button>
41
  </div>
42
  </div>
43
  """
@@ -46,13 +44,16 @@ def filter_menu(preference):
46
  # Function to render detailed view of a dish
47
  def render_dish_details(dish_name):
48
  menu_data = load_menu()
49
- dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
50
- return (
51
- dish["Image URL"],
52
- f"## {dish_name}",
53
- dish["Description"],
54
- f"**Price:** ${dish['Price ($)']}"
55
- )
 
 
 
56
 
57
  # Gradio app definition
58
  def app():
@@ -66,10 +67,10 @@ def app():
66
  label="Choose a Preference",
67
  )
68
 
69
- # Output area for menu items
70
- menu_output = gr.HTML(value=filter_menu("All"))
71
 
72
- # Detailed view
73
  detailed_view = gr.Column(visible=False)
74
  dish_image = gr.Image()
75
  dish_name = gr.Markdown()
@@ -91,7 +92,7 @@ def app():
91
  add_to_bag_button = gr.Button("Add to Bag")
92
  back_button = gr.Button("Back to Menu")
93
 
94
- # Cart
95
  cart_state = gr.State([])
96
  cart_output = gr.HTML(value="Your cart is empty.")
97
 
@@ -100,8 +101,25 @@ def app():
100
  return filter_menu(preference)
101
 
102
  def show_dish_details(dish_name):
103
- img, name, desc, price = render_dish_details(dish_name)
104
- return gr.update(visible=False), gr.update(visible=True), img, name, desc, price
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
107
  cart.append({
@@ -126,7 +144,14 @@ def app():
126
  menu_output.change(
127
  show_dish_details,
128
  inputs=[menu_output],
129
- outputs=[menu_output, detailed_view, dish_image, dish_name, dish_description, dish_price],
 
 
 
 
 
 
 
130
  )
131
 
132
  add_to_bag_button.click(
@@ -141,7 +166,7 @@ def app():
141
  )
142
 
143
  # Layout
144
- with gr.Row():
145
  menu_output
146
 
147
  with detailed_view:
 
11
 
12
  # Function to filter menu items based on preference
13
  def filter_menu(preference):
 
14
  menu_data = load_menu()
15
 
 
16
  if preference == "Halal/Non-Veg":
17
  filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
18
  elif preference == "Vegetarian":
 
34
  </div>
35
  <div style="flex-shrink: 0; text-align: center;">
36
  <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
37
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;"
38
+ value="{item['Dish Name']}">Add</button>
39
  </div>
40
  </div>
41
  """
 
44
  # Function to render detailed view of a dish
45
  def render_dish_details(dish_name):
46
  menu_data = load_menu()
47
+ try:
48
+ dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
49
+ return (
50
+ dish["Image URL"], # Image of the dish
51
+ f"## {dish_name}", # Dish name
52
+ dish["Description"], # Description
53
+ f"**Price:** ${dish['Price ($)']}" # Price
54
+ )
55
+ except IndexError:
56
+ raise ValueError(f"Dish '{dish_name}' not found!")
57
 
58
  # Gradio app definition
59
  def app():
 
67
  label="Choose a Preference",
68
  )
69
 
70
+ # Main menu output area
71
+ menu_output = gr.HTML(value=filter_menu("All"), visible=True)
72
 
73
+ # Detailed view components
74
  detailed_view = gr.Column(visible=False)
75
  dish_image = gr.Image()
76
  dish_name = gr.Markdown()
 
92
  add_to_bag_button = gr.Button("Add to Bag")
93
  back_button = gr.Button("Back to Menu")
94
 
95
+ # Cart state and display
96
  cart_state = gr.State([])
97
  cart_output = gr.HTML(value="Your cart is empty.")
98
 
 
101
  return filter_menu(preference)
102
 
103
  def show_dish_details(dish_name):
104
+ try:
105
+ img, name, desc, price = render_dish_details(dish_name)
106
+ return (
107
+ gr.update(visible=False), # Hide menu
108
+ gr.update(visible=True), # Show detailed view
109
+ img,
110
+ name,
111
+ desc,
112
+ price,
113
+ )
114
+ except ValueError as e:
115
+ return (
116
+ gr.update(visible=True),
117
+ gr.update(visible=False),
118
+ "",
119
+ "Error: Dish not found.",
120
+ "",
121
+ "",
122
+ )
123
 
124
  def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
125
  cart.append({
 
144
  menu_output.change(
145
  show_dish_details,
146
  inputs=[menu_output],
147
+ outputs=[
148
+ menu_output,
149
+ detailed_view,
150
+ dish_image,
151
+ dish_name,
152
+ dish_description,
153
+ dish_price,
154
+ ],
155
  )
156
 
157
  add_to_bag_button.click(
 
166
  )
167
 
168
  # Layout
169
+ with gr.Row(visible=True):
170
  menu_output
171
 
172
  with detailed_view: