nagasurendra commited on
Commit
f690d7c
·
verified ·
1 Parent(s): 9c7ec1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -105
app.py CHANGED
@@ -34,9 +34,7 @@ def filter_menu(preference):
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 value="{item['Dish Name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">
38
- View Details
39
- </button>
40
  </div>
41
  </div>
42
  """
@@ -44,146 +42,67 @@ def filter_menu(preference):
44
 
45
  # Function to render detailed view of a dish
46
  def render_dish_details(dish_name):
 
47
  menu_data = load_menu()
48
  try:
49
  dish = menu_data[menu_data["Dish Name"] == dish_name].iloc[0]
50
  return (
51
- dish["Image URL"], # Image of the dish
52
  f"## {dish_name}", # Dish name
53
  dish["Description"], # Description
54
- f"**Price:** ${dish['Price ($)']}" # Price
55
  )
56
  except IndexError:
57
  raise ValueError(f"Dish '{dish_name}' not found!")
58
 
59
  # Gradio app definition
60
  def app():
61
- with gr.Blocks(title="Dynamic Menu with Suggestions Page") as demo:
62
- gr.Markdown("## Dynamic Menu with Preferences")
63
-
64
- # Radio button for selecting preference
65
- selected_preference = gr.Radio(
66
- choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
67
- value="All",
68
- label="Choose a Preference",
69
- )
70
 
71
- # Main menu output area
72
- menu_output = gr.HTML(value=filter_menu("All"), visible=True)
73
 
74
- # Suggestion page components
75
  suggestion_page = gr.Column(visible=False)
76
- dish_image = gr.Image()
77
  dish_name = gr.Markdown()
78
  dish_description = gr.Markdown()
79
  dish_price = gr.Markdown()
80
- spice_level = gr.Radio(
81
- choices=["Mild", "Medium", "Spicy"],
82
- label="Choose Spice Level",
83
- )
84
- extras = gr.CheckboxGroup(
85
- choices=["Extra Raita", "Extra Salan", "Extra Fried Onion"],
86
- label="Choose Extras",
87
- )
88
- quantity = gr.Number(label="Quantity", value=1, interactive=True)
89
- special_instructions = gr.Textbox(
90
- label="Special Instructions",
91
- placeholder="Add any requests here",
92
- )
93
- add_to_bag_button = gr.Button("Add to Bag")
94
- back_button = gr.Button("Back to Menu")
95
-
96
- # Cart state and display
97
- cart_state = gr.State([])
98
- cart_output = gr.HTML(value="Your cart is empty.")
99
 
100
- # Define interactivity
101
- def update_menu(preference):
102
- return filter_menu(preference)
103
-
104
- def show_suggestion_page(dish_name):
105
  try:
106
  img, name, desc, price = render_dish_details(dish_name)
107
  return (
108
- gr.update(visible=False), # Hide menu
109
- gr.update(visible=True), # Show suggestion page
110
  img,
111
  name,
112
  desc,
113
  price,
114
  )
115
  except ValueError as e:
116
- return (
117
- gr.update(visible=True),
118
- gr.update(visible=False),
119
- "",
120
- "Error: Dish not found.",
121
- "",
122
- "",
123
- )
124
-
125
- def add_to_cart(dish_name, spice_level, extras, quantity, instructions, cart):
126
- cart.append({
127
- "name": dish_name,
128
- "spice_level": spice_level,
129
- "extras": extras,
130
- "quantity": quantity,
131
- "instructions": instructions
132
- })
133
- cart_html = "<br>".join(
134
- [f"{item['quantity']}x {item['name']} - {item['spice_level']} (Extras: {', '.join(item['extras'])})" for item in cart]
135
- )
136
- return cart, cart_html
137
-
138
- # Event handlers
139
- selected_preference.change(
140
- update_menu,
141
- inputs=[selected_preference],
142
- outputs=[menu_output],
143
- )
144
-
145
- menu_output.change(
146
- show_suggestion_page,
147
- inputs=[menu_output],
148
- outputs=[
149
- menu_output,
150
- suggestion_page,
151
- dish_image,
152
- dish_name,
153
- dish_description,
154
- dish_price,
155
- ],
156
- )
157
 
158
- add_to_bag_button.click(
159
- add_to_cart,
160
- inputs=[dish_name, spice_level, extras, quantity, special_instructions, cart_state],
161
- outputs=[cart_state, cart_output],
162
- )
163
-
164
- back_button.click(
165
- lambda: (gr.update(visible=True), gr.update(visible=False)),
166
- outputs=[menu_output, suggestion_page],
167
  )
168
 
169
  # Layout
170
- with gr.Row(visible=True):
171
- menu_output
172
 
173
  with suggestion_page:
174
  dish_image
175
  dish_name
176
  dish_description
177
  dish_price
178
- spice_level
179
- extras
180
- quantity
181
- special_instructions
182
- add_to_bag_button
183
- back_button
184
-
185
- with gr.Row():
186
- cart_output
187
 
188
  return demo
189
 
 
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 value="{item['Dish Name']}" onclick="select('{item['Dish Name']}')" style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
 
 
38
  </div>
39
  </div>
40
  """
 
42
 
43
  # Function to render detailed view of a dish
44
  def render_dish_details(dish_name):
45
+ print(f"Selected dish: {dish_name}") # Debugging line
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"], # Dish image
51
  f"## {dish_name}", # Dish name
52
  dish["Description"], # Description
53
+ f"${dish['Price ($)']}" # Price
54
  )
55
  except IndexError:
56
  raise ValueError(f"Dish '{dish_name}' not found!")
57
 
58
  # Gradio app definition
59
  def app():
60
+ with gr.Blocks() as demo:
61
+ gr.Markdown("## Menu with Suggestions")
62
+
63
+ selected_dish = gr.State("") # Track the selected dish name
 
 
 
 
 
64
 
65
+ # Menu page
66
+ menu_html = gr.HTML(value=filter_menu("All"))
67
 
68
+ # Suggestion page
69
  suggestion_page = gr.Column(visible=False)
70
+ dish_image = gr.Image(label="Dish Image")
71
  dish_name = gr.Markdown()
72
  dish_description = gr.Markdown()
73
  dish_price = gr.Markdown()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
+ # Functions
76
+ def show_suggestion(dish_name):
 
 
 
77
  try:
78
  img, name, desc, price = render_dish_details(dish_name)
79
  return (
80
+ gr.update(visible=False),
81
+ gr.update(visible=True),
82
  img,
83
  name,
84
  desc,
85
  price,
86
  )
87
  except ValueError as e:
88
+ return gr.update(value=f"<p style='color:red;'>Error: {str(e)}</p>")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
+ # Event Handlers
91
+ menu_html.change(
92
+ show_suggestion,
93
+ inputs=[selected_dish],
94
+ outputs=[menu_html, suggestion_page, dish_image, dish_name, dish_description, dish_price]
 
 
 
 
95
  )
96
 
97
  # Layout
98
+ with gr.Row():
99
+ menu_html
100
 
101
  with suggestion_page:
102
  dish_image
103
  dish_name
104
  dish_description
105
  dish_price
 
 
 
 
 
 
 
 
 
106
 
107
  return demo
108