nagasurendra commited on
Commit
849e208
·
verified ·
1 Parent(s): 59a2c0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -29
app.py CHANGED
@@ -40,6 +40,43 @@ def filter_menu(preference):
40
 
41
  # JavaScript for modal and cart behavior
42
  modal_and_cart_js = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  <script>
44
  let cart = [];
45
  const extrasPrices = {
@@ -51,8 +88,12 @@ modal_and_cart_js = """
51
  "Chilli Chicken": 14,
52
  "Veg Manchurian": 12
53
  };
54
-
55
  function openModal(name, image, description, price) {
 
 
 
 
56
  const modal = document.getElementById('modal');
57
  modal.style.display = 'block';
58
  modal.style.position = 'fixed';
@@ -73,12 +114,14 @@ modal_and_cart_js = """
73
  document.getElementById('quantity').value = 1;
74
  document.getElementById('special-instructions').value = '';
75
  }
76
-
77
  function closeModal() {
78
  document.getElementById('modal').style.display = 'none';
79
  }
80
-
81
  function addToCart() {
 
 
 
 
82
  const name = document.getElementById('modal-name').innerText;
83
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
84
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
@@ -92,40 +135,68 @@ modal_and_cart_js = """
92
  updateCartDisplay();
93
  closeModal();
94
  }
95
-
96
  function updateCartDisplay() {
97
  let totalBill = 0;
98
- let cartHTML = "<h3>Your Cart:</h3><ul style='list-style: none; padding: 0;'>";
99
  cart.forEach((item, index) => {
100
  totalBill += item.itemTotal;
101
  const extras = item.extras.map((extra, i) => {
102
  const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
103
  const extraTotal = extrasPrices[extra] * extraQuantity;
104
  totalBill += extraTotal;
105
- return `${extra} (x${extraQuantity}) - $${extraTotal.toFixed(2)}`;
106
- }).join(', ');
107
-
108
- cartHTML += `<li>
109
- ${item.name} (x${item.quantity}) - $${item.itemTotal.toFixed(2)}
110
- <br>Extras: ${extras}
111
- <br>Instructions: ${item.instructions || "None"}
112
- </li>`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  });
114
- cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
115
- cartHTML += `<button style='margin-top: 10px; background-color: #007bff; color: white; border: none; padding: 10px; border-radius: 5px; width: 100%; cursor: pointer;' onclick='navigateToFinalPage()'>Submit</button>`;
116
  document.getElementById('floating-cart').innerHTML = cartHTML;
117
  }
118
-
119
- function navigateToFinalPage() {
120
- const finalOrderHTML = generateFinalOrderHTML();
121
- const newPage = window.open();
122
- newPage.document.write(finalOrderHTML);
123
- newPage.document.close();
124
  }
125
-
126
- function generateFinalOrderHTML() {
127
- let finalOrderHTML = "<html><head><title>Final Order</title></head><body style='font-family: Arial, sans-serif; margin: 20px;'>";
128
- finalOrderHTML += "<h3>Final Order:</h3><ul style='list-style: none; padding: 0;'>";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  let totalBill = 0;
130
  cart.forEach(item => {
131
  totalBill += item.itemTotal;
@@ -135,7 +206,6 @@ modal_and_cart_js = """
135
  totalBill += extraTotal;
136
  return `${extra} (x${extraQuantity}) - $${extraTotal.toFixed(2)}`;
137
  }).join(', ');
138
-
139
  finalOrderHTML += `<li>
140
  ${item.name} (x${item.quantity}) - $${item.itemTotal.toFixed(2)}
141
  <br>Extras: ${extras}
@@ -143,8 +213,8 @@ modal_and_cart_js = """
143
  </li>`;
144
  });
145
  finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
146
- finalOrderHTML += "</body></html>";
147
- return finalOrderHTML;
148
  }
149
  </script>
150
  """
@@ -167,6 +237,9 @@ def app():
167
  # Floating cart display
168
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
169
 
 
 
 
170
  # Modal window
171
  modal_window = gr.HTML("""
172
  <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;">
@@ -207,10 +280,11 @@ def app():
207
  gr.Row(menu_output)
208
  gr.Row(cart_output)
209
  gr.Row(modal_window)
 
210
  gr.HTML(modal_and_cart_js)
211
 
212
  return demo
213
 
214
  if __name__ == "__main__":
215
  demo = app()
216
- demo.launch()
 
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 = {
 
88
  "Chilli Chicken": 14,
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';
 
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;
 
135
  updateCartDisplay();
136
  closeModal();
137
  }
 
138
  function updateCartDisplay() {
139
  let totalBill = 0;
140
+ let cartHTML = "<div class='cart-container'>";
141
  cart.forEach((item, index) => {
142
  totalBill += item.itemTotal;
143
  const extras = item.extras.map((extra, i) => {
144
  const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
145
  const extraTotal = extrasPrices[extra] * extraQuantity;
146
  totalBill += extraTotal;
147
+ return `<div class='cart-item'>
148
+ <span>${extra}</span>
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
156
+ </div>`;
157
+ }).join('');
158
+ cartHTML += `<div class='cart-item'>
159
+ <span>${item.name}</span>
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
167
+ </div>
168
+ ${extras}
169
+ <div class='cart-item'><strong>Instructions:</strong> ${item.instructions || "None"}</div>`;
170
  });
171
+ cartHTML += `</div><p class='cart-total'>Total Bill: $${totalBill.toFixed(2)}</p>`;
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) {
190
+ cart[cartIndex].extrasQuantities.splice(extraIndex, 1);
191
+ }
192
+ updateCartDisplay();
193
+ }
194
+ function removeItem(index) {
195
+ cart.splice(index, 1);
196
+ updateCartDisplay();
197
+ }
198
+ function submitCart() {
199
+ let finalOrderHTML = "<h3>Final Order:</h3><ul>";
200
  let totalBill = 0;
201
  cart.forEach(item => {
202
  totalBill += item.itemTotal;
 
206
  totalBill += extraTotal;
207
  return `${extra} (x${extraQuantity}) - $${extraTotal.toFixed(2)}`;
208
  }).join(', ');
 
209
  finalOrderHTML += `<li>
210
  ${item.name} (x${item.quantity}) - $${item.itemTotal.toFixed(2)}
211
  <br>Extras: ${extras}
 
213
  </li>`;
214
  });
215
  finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
216
+ document.getElementById('final-order').innerHTML = finalOrderHTML;
217
+ alert("Your final order has been submitted!");
218
  }
219
  </script>
220
  """
 
237
  # Floating cart display
238
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
239
 
240
+ # Final order display
241
+ final_order_output = gr.HTML(value="", elem_id="final-order")
242
+
243
  # Modal window
244
  modal_window = gr.HTML("""
245
  <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;">
 
280
  gr.Row(menu_output)
281
  gr.Row(cart_output)
282
  gr.Row(modal_window)
283
+ gr.Row(final_order_output)
284
  gr.HTML(modal_and_cart_js)
285
 
286
  return demo
287
 
288
  if __name__ == "__main__":
289
  demo = app()
290
+ demo.launch()