Rammohan0504 commited on
Commit
fc5b6b9
·
verified ·
1 Parent(s): a1522b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py CHANGED
@@ -138,6 +138,95 @@ with gr.Blocks() as demo:
138
 
139
  # Dish display
140
  dish_display = gr.HTML(value=display_dishes("ALL"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
  # JavaScript for managing the pop-up
143
  gr.HTML(f"""
 
138
 
139
  # Dish display
140
  dish_display = gr.HTML(value=display_dishes("ALL"))
141
+ popup_js = """
142
+ <script>
143
+ document.addEventListener("mousemove", (event) => {
144
+ const popup = document.getElementById("custom-popup");
145
+ if (popup && popup.style.display === "block") {
146
+ popup.style.left = event.clientX + "px";
147
+ popup.style.top = event.clientY + "px";
148
+ }
149
+ });
150
+
151
+ function showPopup(message) {
152
+ const popup = document.getElementById("custom-popup");
153
+ popup.innerHTML = message;
154
+ popup.style.display = "block";
155
+ }
156
+
157
+ function hidePopup() {
158
+ const popup = document.getElementById("custom-popup");
159
+ popup.style.display = "none";
160
+ }
161
+ </script>
162
+ """
163
+
164
+ # HTML for popup container
165
+ popup_html = """
166
+ <div id="custom-popup"
167
+ style="position: fixed; display: none; background: white; border: 1px solid black; padding: 10px; z-index: 1000;">
168
+ </div>
169
+ """
170
+
171
+ # Filter dishes by category
172
+ def filter_dishes(category):
173
+ if category == "ALL":
174
+ return menu_data
175
+ return [dish for dish in menu_data if dish["category"] == category]
176
+
177
+ # Generate dish cards
178
+ def display_dishes(category):
179
+ filtered_dishes = filter_dishes(category)
180
+ html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: space-between;'>"
181
+ for dish in filtered_dishes:
182
+ html_content += f"""
183
+ <div style='margin: 10px; width: 30%; border: 1px solid #ddd; border-radius: 10px; padding: 10px;'>
184
+ <img src='{dish['image']}' alt='{dish['name']}' style='width: 100%; height: auto; border-radius: 5px;'>
185
+ <h3>{dish['name']}</h3>
186
+ <p>{dish['description']}</p>
187
+ <p>Price: {dish['price']}</p>
188
+ <button onmouseover="showPopup('{dish['name']} - {dish['price']}')"
189
+ onmouseout="hidePopup()"
190
+ style='background-color: #007bff; color: white; border: none; padding: 5px 10px; cursor: pointer;'>
191
+ Add
192
+ </button>
193
+ </div>
194
+ """
195
+ html_content += "</div>"
196
+ return html_content
197
+
198
+ # Main Gradio App
199
+ with gr.Blocks() as demo:
200
+ gr.HTML(popup_html) # Add the popup container
201
+ gr.HTML(popup_js) # Add the JavaScript
202
+
203
+ gr.HTML("<h1 style='text-align: center;'>Dynamic Restaurant Menu</h1>")
204
+
205
+ # Buttons for filtering categories
206
+ with gr.Row():
207
+ btn_all = gr.Button("ALL")
208
+ btn_vegan = gr.Button("VEGAN")
209
+ btn_halal = gr.Button("HALAL")
210
+
211
+ # Dish display
212
+ dish_display = gr.HTML(value=display_dishes("ALL"))
213
+
214
+ # Button click events
215
+ btn_all.click(
216
+ lambda: gr.update(value=display_dishes("ALL")),
217
+ outputs=dish_display
218
+ )
219
+ btn_vegan.click(
220
+ lambda: gr.update(value=display_dishes("VEGAN")),
221
+ outputs=dish_display
222
+ )
223
+ btn_halal.click(
224
+ lambda: gr.update(value=display_dishes("HALAL")),
225
+ outputs=dish_display
226
+ )
227
+
228
+ demo.launch()
229
+
230
 
231
  # JavaScript for managing the pop-up
232
  gr.HTML(f"""