nagasurendra commited on
Commit
8144cf3
·
verified ·
1 Parent(s): dd345b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -105
app.py CHANGED
@@ -40,43 +40,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 = {
@@ -88,13 +51,8 @@ modal_and_cart_js = """
88
  "Chilli Chicken": 14,
89
  "Veg Manchurian": 12
90
  };
91
- let finalized = false;
92
 
93
  function openModal(name, image, description, price) {
94
- if (finalized) {
95
- alert("You cannot add more items after finalizing your order.");
96
- return;
97
- }
98
  const modal = document.getElementById('modal');
99
  modal.style.display = 'block';
100
  modal.style.position = 'fixed';
@@ -121,10 +79,6 @@ modal_and_cart_js = """
121
  }
122
 
123
  function addToCart() {
124
- if (finalized) {
125
- alert("You cannot add more items after finalizing your order.");
126
- return;
127
- }
128
  const name = document.getElementById('modal-name').innerText;
129
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
130
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
@@ -141,72 +95,34 @@ modal_and_cart_js = """
141
 
142
  function updateCartDisplay() {
143
  let totalBill = 0;
144
- let cartHTML = "<div class='cart-container'>";
145
  cart.forEach((item, index) => {
146
  totalBill += item.itemTotal;
147
  const extras = item.extras.map((extra, i) => {
148
  const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
149
  const extraTotal = extrasPrices[extra] * extraQuantity;
150
  totalBill += extraTotal;
151
- return `<div class='cart-item'>
152
- <span>${extra}</span>
153
- <span>Price: $${extrasPrices[extra].toFixed(2)}</span>
154
- <div class='quantity-container'>
155
- <label for='extra-quantity-${index}-${i}'>Quantity:</label>
156
- <input type='number' id='extra-quantity-${index}-${i}' value='${extraQuantity}' min='1' style='width: 50px;' onchange='updateExtraQuantity(${index}, ${i}, this.value)'>
157
- </div>
158
- <span>Total: $${extraTotal.toFixed(2)}</span>
159
- <input type='checkbox' id='extra-remove-${index}-${i}' onclick='removeExtra(${index}, ${i})'> Remove
160
- </div>`;
161
- }).join('');
162
 
163
- cartHTML += `<div class='cart-item'>
164
- <span>${item.name}</span>
165
- <span>Item Price: $${item.price.toFixed(2)}</span>
166
- <div class='quantity-container'>
167
- <label for='item-quantity-${index}'>Quantity:</label>
168
- <input type='number' id='item-quantity-${index}' value='${item.quantity}' min='1' style='width: 50px;' onchange='updateItemQuantity(${index}, this.value)'>
169
- </div>
170
- <span>Total: $${(item.price * item.quantity).toFixed(2)}</span>
171
- <input type='checkbox' id='item-remove-${index}' onclick='removeItem(${index})'> Remove
172
- </div>
173
- ${extras}
174
- <div class='cart-item'><strong>Instructions:</strong> ${item.instructions || "None"}</div>`;
175
  });
176
- cartHTML += `</div><p class='cart-total'>Total Bill: $${totalBill.toFixed(2)}</p>`;
177
- 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>`;
178
  document.getElementById('floating-cart').innerHTML = cartHTML;
179
  }
180
 
181
- function updateItemQuantity(index, newQuantity) {
182
- const quantity = parseInt(newQuantity) || 1;
183
- cart[index].quantity = quantity;
184
- cart[index].itemTotal = cart[index].price * quantity;
185
- updateCartDisplay();
186
- }
187
-
188
- function updateExtraQuantity(cartIndex, extraIndex, newQuantity) {
189
- const quantity = parseInt(newQuantity) || 1;
190
- cart[cartIndex].extrasQuantities = cart[cartIndex].extrasQuantities || [];
191
- cart[cartIndex].extrasQuantities[extraIndex] = quantity;
192
- updateCartDisplay();
193
- }
194
-
195
- function removeExtra(cartIndex, extraIndex) {
196
- cart[cartIndex].extras.splice(extraIndex, 1);
197
- if (cart[cartIndex].extrasQuantities) {
198
- cart[cartIndex].extrasQuantities.splice(extraIndex, 1);
199
- }
200
- updateCartDisplay();
201
- }
202
-
203
- function removeItem(index) {
204
- cart.splice(index, 1);
205
- updateCartDisplay();
206
  }
207
 
208
- function submitCart() {
209
- let finalOrderHTML = "<h3>Final Order:</h3><ul>";
210
  let totalBill = 0;
211
  cart.forEach(item => {
212
  totalBill += item.itemTotal;
@@ -224,8 +140,7 @@ modal_and_cart_js = """
224
  </li>`;
225
  });
226
  finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
227
- document.getElementById('final-order').innerHTML = finalOrderHTML;
228
- alert("Your final order has been submitted!");
229
  }
230
  </script>
231
  """
@@ -248,9 +163,6 @@ def app():
248
  # Floating cart display
249
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
250
 
251
- # Final order display
252
- final_order_output = gr.HTML(value="", elem_id="final-order")
253
-
254
  # Modal window
255
  modal_window = gr.HTML("""
256
  <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;">
@@ -291,7 +203,6 @@ def app():
291
  gr.Row(menu_output)
292
  gr.Row(cart_output)
293
  gr.Row(modal_window)
294
- gr.Row(final_order_output)
295
  gr.HTML(modal_and_cart_js)
296
 
297
  return demo
 
40
 
41
  # JavaScript for modal and cart behavior
42
  modal_and_cart_js = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  <script>
44
  let cart = [];
45
  const extrasPrices = {
 
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';
 
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;
 
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
+ window.location.href = `data:text/html;charset=utf-8,${encodeURIComponent(finalOrderHTML)}`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  }
123
 
124
+ function generateFinalOrderHTML() {
125
+ let finalOrderHTML = "<h3>Final Order:</h3><ul style='list-style: none; padding: 0;'>";
126
  let totalBill = 0;
127
  cart.forEach(item => {
128
  totalBill += item.itemTotal;
 
140
  </li>`;
141
  });
142
  finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
143
+ return finalOrderHTML;
 
144
  }
145
  </script>
146
  """
 
163
  # Floating cart display
164
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
165
 
 
 
 
166
  # Modal window
167
  modal_window = gr.HTML("""
168
  <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
  gr.Row(menu_output)
204
  gr.Row(cart_output)
205
  gr.Row(modal_window)
 
206
  gr.HTML(modal_and_cart_js)
207
 
208
  return demo