nagasurendra commited on
Commit
8eccc46
·
verified ·
1 Parent(s): e44b01e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -19
app.py CHANGED
@@ -58,14 +58,11 @@ modal_and_cart_js = """
58
  return;
59
  }
60
  document.getElementById('modal').style.display = 'block';
61
- document.body.style.overflow = 'hidden'; // Disable scrolling
62
  document.getElementById('modal-image').src = image;
63
  document.getElementById('modal-name').innerText = name;
64
  document.getElementById('modal-description').innerText = description;
65
  document.getElementById('modal-price').innerText = price;
66
-
67
- const spiceLevelInputs = document.querySelectorAll('input[name="spice-level"]');
68
- spiceLevelInputs.forEach(input => input.checked = false);
69
  const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
70
  extrasInputs.forEach(input => input.checked = false);
71
  document.getElementById('quantity').value = 1;
@@ -73,7 +70,6 @@ modal_and_cart_js = """
73
  }
74
  function closeModal() {
75
  document.getElementById('modal').style.display = 'none';
76
- document.body.style.overflow = ''; // Re-enable scrolling
77
  }
78
  function addToCart() {
79
  if (finalized) {
@@ -82,17 +78,49 @@ modal_and_cart_js = """
82
  }
83
  const name = document.getElementById('modal-name').innerText;
84
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
85
- const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
86
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
87
  const instructions = document.getElementById('special-instructions').value;
88
  const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
89
  const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
90
  const itemTotal = (price + extrasCost) * quantity;
91
- const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal };
92
  cart.push(cartItem);
93
  alert(`${name} added to cart!`);
 
94
  closeModal();
95
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  </script>
97
  """
98
 
@@ -101,18 +129,25 @@ def app():
101
  with gr.Blocks() as demo:
102
  gr.Markdown("## Dynamic Menu with Preferences")
103
 
 
104
  selected_preference = gr.Radio(
105
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
106
  value="All",
107
  label="Choose a Preference",
108
  )
109
 
 
110
  menu_output = gr.HTML(value=filter_menu("All"))
111
 
 
112
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
113
 
 
 
 
 
114
  modal_window = gr.HTML("""
115
- <div id="modal" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 50%; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
116
  <div style="text-align: right;">
117
  <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
118
  </div>
@@ -120,16 +155,6 @@ def app():
120
  <h2 id="modal-name"></h2>
121
  <p id="modal-description"></p>
122
  <p id="modal-price"></p>
123
- <!-- Spice Levels -->
124
- <label for="spice-level">Choose a Spice Level (Required):</label>
125
- <div id="spice-level-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
126
- <label><input type="radio" name="spice-level" value="American Mild" /> American Mild</label>
127
- <label><input type="radio" name="spice-level" value="American Medium" /> American Medium</label>
128
- <label><input type="radio" name="spice-level" value="American Spicy" /> American Spicy</label>
129
- <label><input type="radio" name="spice-level" value="Indian Mild" /> Indian Mild</label>
130
- <label><input type="radio" name="spice-level" value="Indian Medium" /> Indian Medium</label>
131
- <label><input type="radio" name="spice-level" value="Indian Very Spicy" /> Indian Very Spicy</label>
132
- </div>
133
  <!-- Biryani Extras -->
134
  <label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
135
  <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
@@ -152,17 +177,22 @@ def app():
152
  </div>
153
  """)
154
 
 
155
  finalize_button = gr.Button("Finalize Order")
156
 
157
- finalize_button.click(lambda: "Order finalized!", outputs=[])
 
158
 
 
159
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
160
 
 
161
  gr.Row([selected_preference])
162
  gr.Row(menu_output)
163
  gr.Row(cart_output)
164
  gr.Row(modal_window)
165
  gr.Row(finalize_button)
 
166
  gr.HTML(modal_and_cart_js)
167
 
168
  return demo
 
58
  return;
59
  }
60
  document.getElementById('modal').style.display = 'block';
 
61
  document.getElementById('modal-image').src = image;
62
  document.getElementById('modal-name').innerText = name;
63
  document.getElementById('modal-description').innerText = description;
64
  document.getElementById('modal-price').innerText = price;
65
+ // Reset extras
 
 
66
  const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
67
  extrasInputs.forEach(input => input.checked = false);
68
  document.getElementById('quantity').value = 1;
 
70
  }
71
  function closeModal() {
72
  document.getElementById('modal').style.display = 'none';
 
73
  }
74
  function addToCart() {
75
  if (finalized) {
 
78
  }
79
  const name = document.getElementById('modal-name').innerText;
80
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
 
81
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
82
  const instructions = document.getElementById('special-instructions').value;
83
  const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
84
  const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
85
  const itemTotal = (price + extrasCost) * quantity;
86
+ const cartItem = { name, price, quantity, instructions, extras, itemTotal };
87
  cart.push(cartItem);
88
  alert(`${name} added to cart!`);
89
+ updateCartDisplay();
90
  closeModal();
91
  }
92
+ function updateCartDisplay() {
93
+ let totalBill = 0;
94
+ let cartHTML = "<h3>Your Cart:</h3><ul>";
95
+ cart.forEach((item, index) => {
96
+ totalBill += item.itemTotal;
97
+ const extras = item.extras.join(', ');
98
+ cartHTML += `<li><input type='checkbox' id='cart-item-${index}' value='${index}' checked onclick='removeItem(${index})'> ${item.name} (x${item.quantity}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
99
+ });
100
+ cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
101
+ document.getElementById('floating-cart').innerHTML = cartHTML;
102
+ }
103
+ function removeItem(index) {
104
+ cart.splice(index, 1);
105
+ updateCartDisplay();
106
+ }
107
+ function finalizeOrder() {
108
+ if (cart.length === 0) {
109
+ alert("Your cart is empty. Please add items before finalizing.");
110
+ return;
111
+ }
112
+ finalized = true;
113
+ let finalOrderHTML = "<h3>Final Order:</h3><ul>";
114
+ let totalBill = 0;
115
+ cart.forEach(item => {
116
+ totalBill += item.itemTotal;
117
+ const extras = item.extras.join(', ');
118
+ finalOrderHTML += `<li>${item.name} (x${item.quantity}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
119
+ });
120
+ finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
121
+ document.getElementById('final-order').innerHTML = finalOrderHTML;
122
+ alert("Your order has been finalized. You cannot make changes now.");
123
+ }
124
  </script>
125
  """
126
 
 
129
  with gr.Blocks() as demo:
130
  gr.Markdown("## Dynamic Menu with Preferences")
131
 
132
+ # Radio button for selecting preference
133
  selected_preference = gr.Radio(
134
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
135
  value="All",
136
  label="Choose a Preference",
137
  )
138
 
139
+ # Output area for menu items
140
  menu_output = gr.HTML(value=filter_menu("All"))
141
 
142
+ # Floating cart display
143
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
144
 
145
+ # Final order display
146
+ final_order_output = gr.HTML(value="", elem_id="final-order")
147
+
148
+ # Modal window
149
  modal_window = gr.HTML("""
150
+ <div id="modal" style="display: none; position: fixed; top: 10%; left: 5%; width: 90%; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
151
  <div style="text-align: right;">
152
  <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
153
  </div>
 
155
  <h2 id="modal-name"></h2>
156
  <p id="modal-description"></p>
157
  <p id="modal-price"></p>
 
 
 
 
 
 
 
 
 
 
158
  <!-- Biryani Extras -->
159
  <label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
160
  <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
 
177
  </div>
178
  """)
179
 
180
+ # Finalize Order Button
181
  finalize_button = gr.Button("Finalize Order")
182
 
183
+ # Finalize order button click event
184
+ finalize_button.click(lambda: "Order finalized!", outputs=[final_order_output])
185
 
186
+ # Update menu dynamically based on preference
187
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
188
 
189
+ # Layout
190
  gr.Row([selected_preference])
191
  gr.Row(menu_output)
192
  gr.Row(cart_output)
193
  gr.Row(modal_window)
194
  gr.Row(finalize_button)
195
+ gr.Row(final_order_output)
196
  gr.HTML(modal_and_cart_js)
197
 
198
  return demo