nagasurendra commited on
Commit
f086cff
·
verified ·
1 Parent(s): 49b1cab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -106
app.py CHANGED
@@ -32,7 +32,8 @@ def filter_menu(preference):
32
  </div>
33
  <div style="flex-shrink: 0; text-align: center;">
34
  <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
35
- <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}')">Add</button>
 
36
  </div>
37
  </div>
38
  """
@@ -40,43 +41,6 @@ def filter_menu(preference):
40
 
41
  # JavaScript for modal and cart behavior
42
  modal_and_cart_js = """
43
- <style>
44
- .cart-container {
45
- border: 1px solid #ddd;
46
- border-radius: 8px;
47
- padding: 15px;
48
- margin-bottom: 15px;
49
- background-color: #f9f9f9;
50
- display: flex;
51
- flex-direction: column;
52
- gap: 10px;
53
- }
54
- .cart-item {
55
- display: flex;
56
- justify-content: space-between;
57
- align-items: center;
58
- border-bottom: 1px solid #ddd;
59
- padding: 10px 0;
60
- }
61
- .cart-item:last-child {
62
- border-bottom: none;
63
- }
64
- .cart-item span {
65
- font-size: 16px;
66
- margin-right: 10px;
67
- }
68
- .cart-item .quantity-container {
69
- display: flex;
70
- align-items: center;
71
- gap: 5px;
72
- }
73
- .cart-total {
74
- font-size: 18px;
75
- font-weight: bold;
76
- text-align: right;
77
- margin-top: 10px;
78
- }
79
- </style>
80
  <script>
81
  let cart = [];
82
  const extrasPrices = {
@@ -89,52 +53,45 @@ modal_and_cart_js = """
89
  "Veg Manchurian": 12
90
  };
91
  let finalized = false;
92
- function openModal(name, image, description, price) {
 
93
  if (finalized) {
94
  alert("You cannot add more items after finalizing your order.");
95
  return;
96
  }
97
  const modal = document.getElementById('modal');
98
  modal.style.display = 'block';
99
- modal.style.position = 'fixed';
 
 
 
 
 
100
  if (window.innerWidth <= 768) {
101
  modal.style.width = '90%';
 
 
102
  } else {
103
  modal.style.width = '30%';
 
104
  }
105
- modal.style.top = '15%';
106
- modal.style.left = '50%';
107
- modal.style.transform = 'translate(-50%, -50%)';
108
  document.getElementById('modal-image').src = image;
109
  document.getElementById('modal-name').innerText = name;
110
  document.getElementById('modal-description').innerText = description;
111
  document.getElementById('modal-price').innerText = price;
 
112
  const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
113
  extrasInputs.forEach(input => input.checked = false);
114
  document.getElementById('quantity').value = 1;
115
  document.getElementById('special-instructions').value = '';
116
  }
 
117
  function closeModal() {
118
  document.getElementById('modal').style.display = 'none';
119
  }
120
- function addToCart() {
121
- if (finalized) {
122
- alert("You cannot add more items after finalizing your order.");
123
- return;
124
- }
125
- const name = document.getElementById('modal-name').innerText;
126
- const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
127
- const quantity = parseInt(document.getElementById('quantity').value) || 1;
128
- const instructions = document.getElementById('special-instructions').value;
129
- const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
130
- const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
131
- const itemTotal = (price + extrasCost) * quantity;
132
- const cartItem = { name, price, quantity, instructions, extras, itemTotal, extrasQuantities: extras.map(() => 1) };
133
- cart.push(cartItem);
134
- alert(`${name} added to cart!`);
135
- updateCartDisplay();
136
- closeModal();
137
- }
138
  function updateCartDisplay() {
139
  let totalBill = 0;
140
  let cartHTML = "<div class='cart-container'>";
@@ -149,7 +106,7 @@ modal_and_cart_js = """
149
  <span>Price: $${extrasPrices[extra].toFixed(2)}</span>
150
  <div class='quantity-container'>
151
  <label for='extra-quantity-${index}-${i}'>Quantity:</label>
152
- <input type='number' id='extra-quantity-${index}-${i}' value='${extraQuantity}' min='1' style='width: 50px;' onchange='updateExtraQuantity(${index}, ${i}, this.value)'>
153
  </div>
154
  <span>Total: $${extraTotal.toFixed(2)}</span>
155
  <input type='checkbox' id='extra-remove-${index}-${i}' onclick='removeExtra(${index}, ${i})'> Remove
@@ -160,7 +117,7 @@ modal_and_cart_js = """
160
  <span>Item Price: $${item.price.toFixed(2)}</span>
161
  <div class='quantity-container'>
162
  <label for='item-quantity-${index}'>Quantity:</label>
163
- <input type='number' id='item-quantity-${index}' value='${item.quantity}' min='1' style='width: 50px;' onchange='updateItemQuantity(${index}, this.value)'>
164
  </div>
165
  <span>Total: $${(item.price * item.quantity).toFixed(2)}</span>
166
  <input type='checkbox' id='item-remove-${index}' onclick='removeItem(${index})'> Remove
@@ -172,18 +129,22 @@ modal_and_cart_js = """
172
  cartHTML += `<button style='margin-top: 10px; background-color: #007bff; color: white; border: none; padding: 10px; border-radius: 5px; width: 100%; cursor: pointer;' onclick='submitCart()'>Submit</button>`;
173
  document.getElementById('floating-cart').innerHTML = cartHTML;
174
  }
 
 
175
  function updateItemQuantity(index, newQuantity) {
176
  const quantity = parseInt(newQuantity) || 1;
177
  cart[index].quantity = quantity;
178
  cart[index].itemTotal = cart[index].price * quantity;
179
  updateCartDisplay();
180
  }
 
181
  function updateExtraQuantity(cartIndex, extraIndex, newQuantity) {
182
  const quantity = parseInt(newQuantity) || 1;
183
  cart[cartIndex].extrasQuantities = cart[cartIndex].extrasQuantities || [];
184
  cart[cartIndex].extrasQuantities[extraIndex] = quantity;
185
  updateCartDisplay();
186
  }
 
187
  function removeExtra(cartIndex, extraIndex) {
188
  cart[cartIndex].extras.splice(extraIndex, 1);
189
  if (cart[cartIndex].extrasQuantities) {
@@ -191,45 +152,33 @@ modal_and_cart_js = """
191
  }
192
  updateCartDisplay();
193
  }
 
194
  function removeItem(index) {
195
  cart.splice(index, 1);
196
  updateCartDisplay();
197
  }
198
- function submitCart() {
199
- console.log("Submit button clicked.");
200
- console.log(cart); // Check the cart contents here
201
 
202
- if (cart.length === 0) {
203
- alert("Your cart is empty.");
204
- return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  }
206
-
207
- let finalOrderHTML = "<h3>Final Order:</h3><ul>";
208
- let totalBill = 0;
209
-
210
- cart.forEach(item => {
211
- totalBill += item.itemTotal;
212
- const extras = item.extras.map((extra, i) => {
213
- const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
214
- const extraTotal = extrasPrices[extra] * extraQuantity;
215
- totalBill += extraTotal;
216
- return `${extra} (x${extraQuantity}) - $${extraTotal.toFixed(2)}`;
217
- }).join(', ');
218
-
219
- finalOrderHTML += `<li>
220
- ${item.name} (x${item.quantity}) - $${item.itemTotal.toFixed(2)}
221
- <br>Extras: ${extras}
222
- <br>Instructions: ${item.instructions || "None"}
223
- </li>`;
224
- });
225
-
226
- finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
227
-
228
- sessionStorage.setItem('finalOrderSummary', finalOrderHTML);
229
- window.location.href = "/final_order.html"; // Ensure this page exists
230
- }
231
-
232
-
233
  </script>
234
  """
235
 
@@ -238,23 +187,17 @@ def app():
238
  with gr.Blocks() as demo:
239
  gr.Markdown("## Dynamic Menu with Preferences")
240
 
241
- # Radio button for selecting preference
242
  selected_preference = gr.Radio(
243
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
244
  value="All",
245
  label="Choose a Preference",
246
  )
247
 
248
- # Output area for menu items
249
  menu_output = gr.HTML(value=filter_menu("All"))
250
-
251
- # Floating cart display
252
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
253
-
254
- # Final order display
255
  final_order_output = gr.HTML(value="", elem_id="final-order")
256
 
257
- # Modal window
258
  modal_window = gr.HTML("""
259
  <div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
260
  <div style="text-align: right;">
@@ -286,10 +229,8 @@ def app():
286
  </div>
287
  """)
288
 
289
- # Update menu dynamically based on preference
290
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
291
 
292
- # Layout
293
  gr.Row([selected_preference])
294
  gr.Row(menu_output)
295
  gr.Row(cart_output)
@@ -301,4 +242,4 @@ def app():
301
 
302
  if __name__ == "__main__":
303
  demo = app()
304
- demo.launch()
 
32
  </div>
33
  <div style="flex-shrink: 0; text-align: center;">
34
  <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
35
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;"
36
+ onclick="openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}', event)">Add</button>
37
  </div>
38
  </div>
39
  """
 
41
 
42
  # JavaScript for modal and cart behavior
43
  modal_and_cart_js = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  <script>
45
  let cart = [];
46
  const extrasPrices = {
 
53
  "Veg Manchurian": 12
54
  };
55
  let finalized = false;
56
+
57
+ function openModal(name, image, description, price, event) {
58
  if (finalized) {
59
  alert("You cannot add more items after finalizing your order.");
60
  return;
61
  }
62
  const modal = document.getElementById('modal');
63
  modal.style.display = 'block';
64
+ modal.style.position = 'absolute';
65
+
66
+ const buttonRect = event.target.getBoundingClientRect();
67
+ modal.style.top = `${window.scrollY + buttonRect.top}px`;
68
+ modal.style.left = `${window.scrollX + buttonRect.left}px`;
69
+
70
  if (window.innerWidth <= 768) {
71
  modal.style.width = '90%';
72
+ modal.style.left = '50%';
73
+ modal.style.transform = 'translateX(-50%)';
74
  } else {
75
  modal.style.width = '30%';
76
+ modal.style.transform = 'none';
77
  }
78
+
 
 
79
  document.getElementById('modal-image').src = image;
80
  document.getElementById('modal-name').innerText = name;
81
  document.getElementById('modal-description').innerText = description;
82
  document.getElementById('modal-price').innerText = price;
83
+
84
  const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
85
  extrasInputs.forEach(input => input.checked = false);
86
  document.getElementById('quantity').value = 1;
87
  document.getElementById('special-instructions').value = '';
88
  }
89
+
90
  function closeModal() {
91
  document.getElementById('modal').style.display = 'none';
92
  }
93
+
94
+ // Update the cart display dynamically
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  function updateCartDisplay() {
96
  let totalBill = 0;
97
  let cartHTML = "<div class='cart-container'>";
 
106
  <span>Price: $${extrasPrices[extra].toFixed(2)}</span>
107
  <div class='quantity-container'>
108
  <label for='extra-quantity-${index}-${i}'>Quantity:</label>
109
+ <input type='number' id='extra-quantity-${index}-${i}' value='${extraQuantity}' min='1' style='width: 50px;' onchange='updateExtraQuantity(${index}, ${i}, this.value)' />
110
  </div>
111
  <span>Total: $${extraTotal.toFixed(2)}</span>
112
  <input type='checkbox' id='extra-remove-${index}-${i}' onclick='removeExtra(${index}, ${i})'> Remove
 
117
  <span>Item Price: $${item.price.toFixed(2)}</span>
118
  <div class='quantity-container'>
119
  <label for='item-quantity-${index}'>Quantity:</label>
120
+ <input type='number' id='item-quantity-${index}' value='${item.quantity}' min='1' style='width: 50px;' onchange='updateItemQuantity(${index}, this.value)' />
121
  </div>
122
  <span>Total: $${(item.price * item.quantity).toFixed(2)}</span>
123
  <input type='checkbox' id='item-remove-${index}' onclick='removeItem(${index})'> Remove
 
129
  cartHTML += `<button style='margin-top: 10px; background-color: #007bff; color: white; border: none; padding: 10px; border-radius: 5px; width: 100%; cursor: pointer;' onclick='submitCart()'>Submit</button>`;
130
  document.getElementById('floating-cart').innerHTML = cartHTML;
131
  }
132
+
133
+ // Additional functions for cart management (unchanged from your original code)
134
  function updateItemQuantity(index, newQuantity) {
135
  const quantity = parseInt(newQuantity) || 1;
136
  cart[index].quantity = quantity;
137
  cart[index].itemTotal = cart[index].price * quantity;
138
  updateCartDisplay();
139
  }
140
+
141
  function updateExtraQuantity(cartIndex, extraIndex, newQuantity) {
142
  const quantity = parseInt(newQuantity) || 1;
143
  cart[cartIndex].extrasQuantities = cart[cartIndex].extrasQuantities || [];
144
  cart[cartIndex].extrasQuantities[extraIndex] = quantity;
145
  updateCartDisplay();
146
  }
147
+
148
  function removeExtra(cartIndex, extraIndex) {
149
  cart[cartIndex].extras.splice(extraIndex, 1);
150
  if (cart[cartIndex].extrasQuantities) {
 
152
  }
153
  updateCartDisplay();
154
  }
155
+
156
  function removeItem(index) {
157
  cart.splice(index, 1);
158
  updateCartDisplay();
159
  }
 
 
 
160
 
161
+ function submitCart() {
162
+ let finalOrderHTML = "<h3>Final Order:</h3><ul>";
163
+ let totalBill = 0;
164
+ cart.forEach(item => {
165
+ totalBill += item.itemTotal;
166
+ const extras = item.extras.map((extra, i) => {
167
+ const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
168
+ const extraTotal = extrasPrices[extra] * extraQuantity;
169
+ totalBill += extraTotal;
170
+ return `${extra} (x${extraQuantity}) - $${extraTotal.toFixed(2)}`;
171
+ }).join(', ');
172
+ finalOrderHTML += `<li>
173
+ ${item.name} (x${item.quantity}) - $${item.itemTotal.toFixed(2)}
174
+ <br>Extras: ${extras}
175
+ <br>Instructions: ${item.instructions || "None"}
176
+ </li>`;
177
+ });
178
+ finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
179
+ document.getElementById('final-order').innerHTML = finalOrderHTML;
180
+ alert("Your final order has been submitted!");
181
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  </script>
183
  """
184
 
 
187
  with gr.Blocks() as demo:
188
  gr.Markdown("## Dynamic Menu with Preferences")
189
 
 
190
  selected_preference = gr.Radio(
191
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
192
  value="All",
193
  label="Choose a Preference",
194
  )
195
 
 
196
  menu_output = gr.HTML(value=filter_menu("All"))
 
 
197
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
 
 
198
  final_order_output = gr.HTML(value="", elem_id="final-order")
199
 
200
+ # Modal window preserved as requested
201
  modal_window = gr.HTML("""
202
  <div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
203
  <div style="text-align: right;">
 
229
  </div>
230
  """)
231
 
 
232
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
233
 
 
234
  gr.Row([selected_preference])
235
  gr.Row(menu_output)
236
  gr.Row(cart_output)
 
242
 
243
  if __name__ == "__main__":
244
  demo = app()
245
+ demo.launch()