Rammohan0504 commited on
Commit
5c510e8
·
verified ·
1 Parent(s): 2ed10f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +206 -38
app.py CHANGED
@@ -18,71 +18,239 @@ menu_data = [
18
  "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/grilled_chicken_salad.jpg",
19
  "description": "Healthy grilled chicken slices with a bed of fresh greens.",
20
  "price": "$9.99"},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  ]
22
 
23
  # Filter dishes by category
 
 
 
 
 
 
24
  def display_dishes(category):
25
- dishes = [dish for dish in menu_data if category == "ALL" or dish["category"] == category]
26
- html = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
27
- for dish in dishes:
28
- html += f"""
29
- <div style='margin: 10px; width: 300px; border: 1px solid #ddd; border-radius: 10px; padding: 10px; text-align: center;'>
30
- <img src='{dish['image']}' style='width: 100%; height: 150px; object-fit: cover; border-radius: 10px;'>
31
- <h4>{dish['name']}</h4>
32
- <p>{dish['description']}</p>
33
- <p><strong>{dish['price']}</strong></p>
34
- <button onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}')"
35
- style='padding: 10px 15px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;'>Add</button>
 
 
 
 
 
 
 
 
36
  </div>
37
  """
38
- html += "</div>"
39
- return html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # Main Gradio App
42
  with gr.Blocks() as demo:
43
- gr.HTML("<h1 style='text-align: center;'>Dynamic Menu</h1>")
44
 
45
- with gr.Tabs():
46
  with gr.Tab("Menu"):
47
  with gr.Row():
48
  btn_all = gr.Button("ALL")
49
  btn_vegan = gr.Button("VEGAN")
50
  btn_halal = gr.Button("HALAL")
51
- menu_html = gr.HTML(value=display_dishes("ALL"))
52
 
53
  with gr.Tab("Cart"):
54
- gr.HTML("<p>Your cart is empty.</p>")
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- btn_all.click(lambda: display_dishes("ALL"), outputs=[menu_html])
57
- btn_vegan.click(lambda: display_dishes("VEGAN"), outputs=[menu_html])
58
- btn_halal.click(lambda: display_dishes("HALAL"), outputs=[menu_html])
 
 
 
59
 
60
  gr.HTML(f"""
61
- <div id="popup" style="display: none; position: absolute; left: 50%; transform: translateX(-50%);
62
  background-color: white; padding: 20px; border-radius: 10px;
63
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
64
  <div style="position: absolute; top: 10px; right: 10px; cursor: pointer; font-size: 20px; font-weight: bold;" onclick="closePopup()">&times;</div>
65
  <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
66
  <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
67
  <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
68
- <button onclick="closePopup()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Close</button>
 
 
 
 
 
 
 
 
 
 
 
 
69
  </div>
70
  <script>
71
- function showPopup(title, description, price) {{
72
- const popup = document.getElementById('popup');
73
- document.getElementById('popup-title').textContent = title;
74
- document.getElementById('popup-description').textContent = description;
75
- document.getElementById('popup-price').textContent = price;
76
-
77
- const scrollY = window.scrollY;
78
- popup.style.top = `${{scrollY + 100}}px`;
79
- popup.style.display = 'block';
80
- }}
81
-
82
- function closePopup() {{
83
- document.getElementById('popup').style.display = 'none';
84
- }}
85
- </script>
86
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  demo.launch()
 
 
 
 
18
  "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/grilled_chicken_salad.jpg",
19
  "description": "Healthy grilled chicken slices with a bed of fresh greens.",
20
  "price": "$9.99"},
21
+ {"name": "Margherita Pizza", "category": "VEGAN",
22
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/margherita_pizza.jpg",
23
+ "description": "Classic Italian pizza topped with fresh basil and mozzarella.",
24
+ "price": "$11.99"},
25
+ {"name": "Falafel Wrap", "category": "VEGAN",
26
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/falafel_wrap.jpg",
27
+ "description": "Middle Eastern spiced falafel balls wrapped in pita bread.",
28
+ "price": "$7.99"},
29
+ {"name": "Beef Kebab Platter", "category": "HALAL",
30
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/beef_kebab_platter.jpg",
31
+ "description": "Tender beef kebabs served with aromatic rice and salad.",
32
+ "price": "$14.99"},
33
+ {"name": "Veggie Sushi Rolls", "category": "VEGAN",
34
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veggie_sushi_rolls.jpg",
35
+ "description": "Japanese sushi rolls with fresh vegetables and soy sauce.",
36
+ "price": "$13.99"},
37
+ {"name": "Lamb Curry", "category": "HALAL",
38
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/lamb_curry.jpg",
39
+ "description": "Rich and flavorful lamb curry cooked with Indian spices.",
40
+ "price": "$15.99"},
41
+ {"name": "Pasta Primavera", "category": "VEGAN",
42
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/pasta_primavera.jpg",
43
+ "description": "Italian pasta tossed with seasonal vegetables and olive oil.",
44
+ "price": "$9.99"},
45
+ {"name": "Chicken Shawarma", "category": "HALAL",
46
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/chicken_shawarma.jpg",
47
+ "description": "Juicy chicken wrapped in soft pita with tahini sauce.",
48
+ "price": "$8.99"},
49
+ {"name": "Quinoa Salad", "category": "VEGAN",
50
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/quinoa_salad.jpg",
51
+ "description": "Nutritious quinoa with fresh veggies and a lemon vinaigrette.",
52
+ "price": "$8.49"},
53
+ {"name": "Butter Chicken", "category": "HALAL",
54
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/butter_chicken.jpg",
55
+ "description": "Classic Indian butter chicken in a creamy tomato-based gravy.",
56
+ "price": "$13.99"},
57
+ {"name": "Tofu Stir Fry", "category": "VEGAN",
58
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/tofu_stir_fry.jpg",
59
+ "description": "Asian-style tofu stir-fried with mixed vegetables and soy sauce.",
60
+ "price": "$8.99"},
61
+ {"name": "Mixed Grill Platter", "category": "HALAL",
62
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/mixed_grill_platter.jpg",
63
+ "description": "Selection of grilled chicken, beef, and lamb served with sides.",
64
+ "price": "$18.99"},
65
+ {"name": "Hummus with Pita", "category": "VEGAN",
66
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/hummus_with_pita.jpg",
67
+ "description": "Creamy hummus served with warm pita bread.",
68
+ "price": "$6.99"}
69
+ ]
70
+
71
+ # Cart data (persistent across sessions)
72
+ cart_items = []
73
+
74
+ # Extras
75
+ extras = [
76
+ {"name": "Coke 500ml", "price": "$2.00"},
77
+ {"name": "Sprite 500ml", "price": "$2.00"},
78
+ {"name": "Extra Raitha 4oz", "price": "$1.00"},
79
+ {"name": "Extra Salan 8oz", "price": "$2.00"},
80
  ]
81
 
82
  # Filter dishes by category
83
+ def filter_dishes(category):
84
+ if category == "ALL":
85
+ return menu_data
86
+ return [dish for dish in menu_data if dish["category"] == category]
87
+
88
+ # Generate dish cards with "Add" button
89
  def display_dishes(category):
90
+ filtered_dishes = filter_dishes(category)
91
+ html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%; height:100%; overflow-y: auto;'>"
92
+ for dish in filtered_dishes:
93
+ html_content += f"""
94
+ <div style='margin: 10px; display: flex; align-items: center; justify-content: space-between;
95
+ width: 80%; padding: 10px; border: 1px solid #ddd; border-radius: 10px;
96
+ background-color: #f9f9f9;'>
97
+ <div style='flex: 1; margin-right: 20px; text-align: left;'>
98
+ <h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4>
99
+ <p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p>
100
+ <p style='font-size: 16px; color: #444; margin: 0;'>Price: {dish['price']}</p>
101
+ </div>
102
+ <div style='text-align: center;'>
103
+ <img src='{dish['image']}' alt='{dish['name']}'
104
+ style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'>
105
+ <button onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}')"
106
+ style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
107
+ border-radius: 5px; cursor: pointer;">Add</button>
108
+ </div>
109
  </div>
110
  """
111
+ html_content += "</div>"
112
+ return html_content
113
+
114
+ # Function to handle adding items to the cart
115
+ def add_to_cart(dish_name, selected_extras, quantity, custom_description):
116
+ cart_items.append({
117
+ "name": dish_name,
118
+ "extras": selected_extras,
119
+ "quantity": quantity,
120
+ "custom_description": custom_description
121
+ })
122
+ return update_cart()
123
+
124
+ # Update cart display
125
+ def update_cart():
126
+ if not cart_items:
127
+ return "<p>Your cart is empty.</p>"
128
+ html_content = "<h3>Cart Items</h3><ul style='list-style-type: none; padding: 0;'>"
129
+ for item in cart_items:
130
+ extras = ", ".join(item["extras"])
131
+ html_content += f"<li style='margin-bottom: 10px;'><strong>{item['name']}</strong> (x{item['quantity']})<br>"
132
+ html_content += f"Extras: {extras if extras else 'None'}<br>"
133
+ html_content += f"Note: {item['custom_description']}</li>"
134
+ html_content += "</ul>"
135
+ return html_content
136
 
137
  # Main Gradio App
138
  with gr.Blocks() as demo:
139
+ gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Dynamic Menu with Cart 🍛</h1>")
140
 
141
+ with gr.Tabs() as tabs:
142
  with gr.Tab("Menu"):
143
  with gr.Row():
144
  btn_all = gr.Button("ALL")
145
  btn_vegan = gr.Button("VEGAN")
146
  btn_halal = gr.Button("HALAL")
147
+ dish_display = gr.HTML(value=display_dishes("ALL"))
148
 
149
  with gr.Tab("Cart"):
150
+ cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
151
+
152
+ # Button click events
153
+ btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
154
+ btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
155
+ btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
156
+
157
+ # Add to cart button with input fields
158
+ add_to_cart_inputs = [
159
+ gr.Textbox(label="Dish Name", visible=False),
160
+ gr.Textbox(label="Selected Extras", visible=False),
161
+ gr.Number(label="Quantity", value=1, visible=False),
162
+ gr.Textbox(label="Custom Description", visible=False)
163
+ ]
164
 
165
+ add_to_cart_button = gr.Button("Add to Cart", visible=False)
166
+ add_to_cart_button.click(
167
+ add_to_cart,
168
+ inputs=add_to_cart_inputs,
169
+ outputs=[cart_display]
170
+ )
171
 
172
  gr.HTML(f"""
173
+ <div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
174
  background-color: white; padding: 20px; border-radius: 10px;
175
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
176
  <div style="position: absolute; top: 10px; right: 10px; cursor: pointer; font-size: 20px; font-weight: bold;" onclick="closePopup()">&times;</div>
177
  <h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
178
  <p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
179
  <p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
180
+ <h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4>
181
+ <div style="display: flex; flex-wrap: wrap;">
182
+ """ + "".join([f'<label><input type="checkbox" name="extras" value="{extra["name"]}">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """
183
+ </div>
184
+ <h4 style="text-align: left; margin-top: 20px;">Quantity</h4>
185
+ <div style="display: flex; align-items: center;">
186
+ <button onclick="decreaseQuantity()" style="padding: 5px 10px; margin-right: 5px;">-</button>
187
+ <input type="number" id="quantity" min="1" value="1" style="width: 50px; text-align: center;" readonly>
188
+ <button onclick="increaseQuantity()" style="padding: 5px 10px; margin-left: 5px;">+</button>
189
+ </div>
190
+ <h4 style="text-align: left; margin-top: 20px;">Add Custom Notes</h4>
191
+ <textarea id="custom-description" rows="2" style="width: 100%; padding: 5px; border-radius: 5px; border: 1px solid #ddd;"></textarea>
192
+ <button onclick="addItemToCart()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Add to Cart</button>
193
  </div>
194
  <script>
195
+ let selectedDish = "";
196
+ let selectedExtras = [];
197
+
198
+ function showPopup(title, description, price) {
199
+ selectedDish = title;
200
+ document.getElementById('popup-title').textContent = title;
201
+ document.getElementById('popup-description').textContent = description;
202
+ document.getElementById('popup-price').textContent = price;
203
+ document.getElementById('popup').style.display = 'block';
204
+ }
205
+
206
+ function closePopup() {
207
+ document.getElementById('popup').style.display = 'none';
208
+ }
209
+
210
+ function increaseQuantity() {
211
+ const quantityInput = document.getElementById('quantity');
212
+ quantityInput.value = parseInt(quantityInput.value) + 1;
213
+ }
214
+
215
+ function decreaseQuantity() {
216
+ const quantityInput = document.getElementById('quantity');
217
+ if (parseInt(quantityInput.value) > 1) {
218
+ quantityInput.value = parseInt(quantityInput.value) - 1;
219
+ }
220
+ }
221
+
222
+ function addItemToCart() {
223
+ const extrasInputs = document.querySelectorAll('input[name="extras"]:checked');
224
+ selectedExtras = Array.from(extrasInputs).map(input => input.value);
225
+ const quantity = document.getElementById('quantity').value || 1;
226
+ const customDescription = document.getElementById('custom-description').value || "None";
227
+
228
+ // Pass data to Gradio backend
229
+ const gradioApp = document.querySelector("gradio-app").shadowRoot;
230
+ const dishNameInput = gradioApp.querySelector("textarea[aria-label='Dish Name']");
231
+ const extrasInput = gradioApp.querySelector("textarea[aria-label='Selected Extras']");
232
+ const quantityInput = gradioApp.querySelector("input[aria-label='Quantity']");
233
+ const customDescriptionInput = gradioApp.querySelector("textarea[aria-label='Custom Description']");
234
+ const addToCartButton = gradioApp.querySelector("button[aria-label='Add to Cart']");
235
+
236
+ if (dishNameInput && extrasInput && quantityInput && customDescriptionInput && addToCartButton) {
237
+ dishNameInput.value = selectedDish;
238
+ extrasInput.value = JSON.stringify(selectedExtras);
239
+ quantityInput.value = quantity;
240
+ customDescriptionInput.value = customDescription;
241
+
242
+ // Trigger Gradio's add-to-cart function
243
+ addToCartButton.click();
244
+ closePopup();
245
+ } else {
246
+ console.error("Failed to locate Gradio inputs or button.");
247
+ }
248
+ }
249
+ </script>
250
+
251
+ """)
252
 
253
  demo.launch()
254
+
255
+ ChatGPT said:
256
+ ChatGPT