nagasurendra commited on
Commit
7e8594c
·
verified ·
1 Parent(s): c4008db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -140
app.py CHANGED
@@ -38,95 +38,34 @@ def filter_menu(preference):
38
  """
39
  return html_content
40
 
41
- # JavaScript for modal and cart behavior
42
- modal_and_cart_js = """
43
- <script>
44
- let cart = [];
45
- const extrasPrices = {
46
- "Extra Raitha 4oz": 1,
47
- "Extra Raitha 8oz": 2,
48
- "Extra Salan 4oz": 1,
49
- "Extra Salan 8oz": 2,
50
- "Extra Onion": 1,
51
- "Extra Onion & Lemon": 2,
52
- "Extra Fried Onion 4oz": 2
53
- };
54
- let finalized = false;
55
- function openModal(name, image, description, price) {
56
- if (finalized) {
57
- alert("You cannot add more items after finalizing your order.");
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
- const spiceLevelInputs = document.querySelectorAll('input[name="spice-level"]');
66
- spiceLevelInputs.forEach(input => input.checked = false);
67
- const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
68
- extrasInputs.forEach(input => input.checked = false);
69
- document.getElementById('quantity').value = 1;
70
- document.getElementById('special-instructions').value = '';
71
- }
72
- function closeModal() {
73
- document.getElementById('modal').style.display = 'none';
74
- }
75
- function addToCart() {
76
- if (finalized) {
77
- alert("You cannot add more items after finalizing your order.");
78
- return;
79
- }
80
- const name = document.getElementById('modal-name').innerText;
81
- const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
82
- const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
83
- const quantity = parseInt(document.getElementById('quantity').value) || 1;
84
- const instructions = document.getElementById('special-instructions').value;
85
- const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
86
- const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
87
- const itemTotal = (price + extrasCost) * quantity;
88
- const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal };
89
- cart.push(cartItem);
90
- alert(`${name} added to cart!`);
91
- updateCartDisplay();
92
- closeModal();
93
- }
94
- function updateCartDisplay() {
95
- let totalBill = 0;
96
- let cartHTML = "<h3>Your Cart:</h3><ul>";
97
- cart.forEach((item, index) => {
98
- totalBill += item.itemTotal;
99
- const extras = item.extras.join(', ');
100
- cartHTML += `<li><input type='checkbox' id='cart-item-${index}' value='${index}' checked onclick='removeItem(${index})'> ${item.name} (x${item.quantity}, Spice: ${item.spiceLevel}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
101
- });
102
- cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
103
- document.getElementById('floating-cart').innerHTML = cartHTML;
104
- }
105
- function removeItem(index) {
106
- cart.splice(index, 1);
107
- updateCartDisplay();
108
- }
109
- function finalizeOrder() {
110
- if (cart.length === 0) {
111
- alert("Your cart is empty. Please add items before finalizing.");
112
- return;
113
- }
114
- finalized = true;
115
- let finalOrderHTML = "<h3>Your Final Order:</h3><ul>";
116
- let totalBill = 0;
117
- cart.forEach(item => {
118
- totalBill += item.itemTotal;
119
- const extras = item.extras.join(', ');
120
- finalOrderHTML += `<li>${item.name} (x${item.quantity}, Spice: ${item.spiceLevel}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
121
- });
122
- finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
123
- finalOrderHTML += `<p style='color: green; font-size: 18px;'>Your final order has been placed. Thank you!</p>`;
124
- document.getElementById('final-page').innerHTML = finalOrderHTML;
125
- document.getElementById('floating-cart').style.display = 'none';
126
- document.getElementById('finalize-button').style.display = 'none';
127
- }
128
- </script>
129
- """
130
 
131
  # Gradio app definition
132
  def app():
@@ -144,70 +83,26 @@ def app():
144
  menu_output = gr.HTML(value=filter_menu("All"))
145
 
146
  # Floating cart display
147
- cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
148
 
149
  # Final order display
150
- final_order_output = gr.HTML(value="", elem_id="final-page")
151
 
152
- # Modal window
153
- modal_window = gr.HTML("""
154
- <div id="modal" style="display: none; position: fixed; top: 40%; left: 50%; transform: translate(-50%, -40%); width: 50%; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
155
- <div style="text-align: right;">
156
- <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
157
- </div>
158
- <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
159
- <h2 id="modal-name"></h2>
160
- <p id="modal-description"></p>
161
- <p id="modal-price"></p>
162
- <!-- Spice Levels -->
163
- <label for="spice-level">Choose a Spice Level (Required):</label>
164
- <div id="spice-level-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
165
- <label><input type="radio" name="spice-level" value="American Mild" /> American Mild</label>
166
- <label><input type="radio" name="spice-level" value="American Medium" /> American Medium</label>
167
- <label><input type="radio" name="spice-level" value="American Spicy" /> American Spicy</label>
168
- <label><input type="radio" name="spice-level" value="Indian Mild" /> Indian Mild</label>
169
- <label><input type="radio" name="spice-level" value="Indian Medium" /> Indian Medium</label>
170
- <label><input type="radio" name="spice-level" value="Indian Very Spicy" /> Indian Very Spicy</label>
171
- </div>
172
- <!-- Biryani Extras -->
173
- <label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
174
- <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
175
- <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 4oz" /> Extra Raitha 4oz + $1.00</label>
176
- <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 8oz" /> Extra Raitha 8oz + $2.00</label>
177
- <label><input type="checkbox" name="biryani-extra" value="Extra Salan 4oz" /> Extra Salan 4oz + $1.00</label>
178
- <label><input type="checkbox" name="biryani-extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label>
179
- <label><input type="checkbox" name="biryani-extra" value="Extra Onion" /> Extra Onion + $1.00</label>
180
- <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
181
- <label><input type="checkbox" name="biryani-extra" value="Extra Fried Onion 4oz" /> Extra Fried Onion 4oz + $2.00</label>
182
- </div>
183
- <!-- Quantity and Special Instructions -->
184
- <label for="quantity">Quantity:</label>
185
- <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
186
- <br><br>
187
- <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
188
- <br><br>
189
- <!-- Add to Cart Button -->
190
- <button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
191
- </div>
192
- """)
193
-
194
- # Finalize Order Button
195
- finalize_button = gr.Button("Finalize Order", elem_id="finalize-button")
196
 
197
- # Finalize order button click event
198
- finalize_button.click(None, None, final_order_output, _js="finalizeOrder")
199
 
200
  # Update menu dynamically based on preference
201
- selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
202
 
203
  # Layout
204
  gr.Row([selected_preference])
205
  gr.Row(menu_output)
206
  gr.Row(cart_output)
207
- gr.Row(modal_window)
208
  gr.Row(finalize_button)
209
  gr.Row(final_order_output)
210
- gr.HTML(modal_and_cart_js)
211
 
212
  return demo
213
 
 
38
  """
39
  return html_content
40
 
41
+ # Function to display final order dynamically
42
+ def display_final_order():
43
+ total_bill = 0
44
+ final_order_html = "<h3>Your Final Order:</h3><ul>"
45
+ for item in cart:
46
+ total_bill += item['itemTotal']
47
+ extras = ", ".join(item['extras'])
48
+ final_order_html += f"<li>{item['name']} (x{item['quantity']}, Spice: {item['spiceLevel']}, Extras: {extras}, Instructions: {item['instructions']}) - ${item['itemTotal']:.2f}</li>"
49
+ final_order_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
50
+ final_order_html += "<p style='color: green; font-size: 18px;'>Your final order has been placed. Thank you!</p>"
51
+ return final_order_html
52
+
53
+ # Global cart variable
54
+ cart = []
55
+
56
+ # Function to handle adding items to the cart
57
+ def add_to_cart(name, price, spice_level, quantity, instructions, extras):
58
+ item_total = (price + sum(extras.values())) * quantity
59
+ cart.append({
60
+ 'name': name,
61
+ 'price': price,
62
+ 'spiceLevel': spice_level,
63
+ 'quantity': quantity,
64
+ 'instructions': instructions,
65
+ 'extras': list(extras.keys()),
66
+ 'itemTotal': item_total
67
+ })
68
+ return "Item added to cart!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  # Gradio app definition
71
  def app():
 
83
  menu_output = gr.HTML(value=filter_menu("All"))
84
 
85
  # Floating cart display
86
+ cart_output = gr.HTML(value="Your cart is empty.")
87
 
88
  # Final order display
89
+ final_order_output = gr.HTML(value="")
90
 
91
+ # Button to finalize order
92
+ finalize_button = gr.Button("Finalize Order")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
+ # Finalize button click handler
95
+ finalize_button.click(fn=display_final_order, inputs=[], outputs=final_order_output)
96
 
97
  # Update menu dynamically based on preference
98
+ selected_preference.change(fn=filter_menu, inputs=[selected_preference], outputs=[menu_output])
99
 
100
  # Layout
101
  gr.Row([selected_preference])
102
  gr.Row(menu_output)
103
  gr.Row(cart_output)
 
104
  gr.Row(finalize_button)
105
  gr.Row(final_order_output)
 
106
 
107
  return demo
108