nagasurendra commited on
Commit
d1b2656
·
verified ·
1 Parent(s): 1ee5aa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -83
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import pandas as pd
 
3
 
4
  # Function to load the menu data
5
  def load_menu():
@@ -32,15 +33,15 @@ 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
  """
39
  return html_content
40
 
41
  # Function to finalize the order and display the final page
42
- def finalize_order():
43
- global cart
44
  if not cart:
45
  return "<p style='color: red; font-size: 18px;'>Your cart is empty. Please add items before finalizing.</p>"
46
 
@@ -67,36 +68,18 @@ modal_and_cart_js = """
67
  "Extra Onion & Lemon": 2,
68
  "Extra Fried Onion 4oz": 2
69
  };
70
- function openModal(name, image, description, price) {
71
- document.getElementById('modal').style.display = 'block';
72
- document.getElementById('modal-image').src = image;
73
- document.getElementById('modal-name').innerText = name;
74
- document.getElementById('modal-description').innerText = description;
75
- document.getElementById('modal-price').innerText = price;
76
- const spiceLevelInputs = document.querySelectorAll('input[name="spice-level"]');
77
- spiceLevelInputs.forEach(input => input.checked = false);
78
- const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
79
- extrasInputs.forEach(input => input.checked = false);
80
- document.getElementById('quantity').value = 1;
81
- document.getElementById('special-instructions').value = '';
82
- }
83
- function closeModal() {
84
- document.getElementById('modal').style.display = 'none';
85
- }
86
- function addToCart() {
87
- const name = document.getElementById('modal-name').innerText;
88
- const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
89
- const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
90
- const quantity = parseInt(document.getElementById('quantity').value) || 1;
91
- const instructions = document.getElementById('special-instructions').value;
92
- const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
93
- const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
94
- const itemTotal = (price + extrasCost) * quantity;
95
- const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal };
96
- cart.push(cartItem);
97
- alert(`${name} added to cart!`);
98
  updateCartDisplay();
99
- closeModal();
100
  }
101
  function updateCartDisplay() {
102
  let totalBill = 0;
@@ -104,23 +87,20 @@ modal_and_cart_js = """
104
  cart.forEach((item, index) => {
105
  totalBill += item.itemTotal;
106
  const extras = item.extras.join(', ');
107
- 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>`;
108
  });
109
  cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
110
  document.getElementById('floating-cart').innerHTML = cartHTML;
 
111
  }
112
- function removeItem(index) {
113
- cart.splice(index, 1);
114
- updateCartDisplay();
115
  }
116
  </script>
117
  """
118
 
119
  # Gradio app definition
120
  def app():
121
- global cart
122
- cart = []
123
-
124
  with gr.Blocks() as demo:
125
  gr.Markdown("## Dynamic Menu with Preferences")
126
 
@@ -137,56 +117,17 @@ def app():
137
  # Floating cart display
138
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
139
 
 
 
 
140
  # Final order display
141
  final_order_output = gr.HTML(value="", elem_id="final-page")
142
 
143
- # Modal window
144
- modal_window = gr.HTML("""
145
- <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;">
146
- <div style="text-align: right;">
147
- <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
148
- </div>
149
- <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
150
- <h2 id="modal-name"></h2>
151
- <p id="modal-description"></p>
152
- <p id="modal-price"></p>
153
- <!-- Spice Levels -->
154
- <label for="spice-level">Choose a Spice Level (Required):</label>
155
- <div id="spice-level-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
156
- <label><input type="radio" name="spice-level" value="American Mild" /> American Mild</label>
157
- <label><input type="radio" name="spice-level" value="American Medium" /> American Medium</label>
158
- <label><input type="radio" name="spice-level" value="American Spicy" /> American Spicy</label>
159
- <label><input type="radio" name="spice-level" value="Indian Mild" /> Indian Mild</label>
160
- <label><input type="radio" name="spice-level" value="Indian Medium" /> Indian Medium</label>
161
- <label><input type="radio" name="spice-level" value="Indian Very Spicy" /> Indian Very Spicy</label>
162
- </div>
163
- <!-- Biryani Extras -->
164
- <label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
165
- <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
166
- <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 4oz" /> Extra Raitha 4oz + $1.00</label>
167
- <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 8oz" /> Extra Raitha 8oz + $2.00</label>
168
- <label><input type="checkbox" name="biryani-extra" value="Extra Salan 4oz" /> Extra Salan 4oz + $1.00</label>
169
- <label><input type="checkbox" name="biryani-extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label>
170
- <label><input type="checkbox" name="biryani-extra" value="Extra Onion" /> Extra Onion + $1.00</label>
171
- <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
172
- <label><input type="checkbox" name="biryani-extra" value="Extra Fried Onion 4oz" /> Extra Fried Onion 4oz + $2.00</label>
173
- </div>
174
- <!-- Quantity and Special Instructions -->
175
- <label for="quantity">Quantity:</label>
176
- <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
177
- <br><br>
178
- <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
179
- <br><br>
180
- <!-- Add to Cart Button -->
181
- <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>
182
- </div>
183
- """)
184
-
185
  # Finalize Order Button
186
  finalize_button = gr.Button("Finalize Order")
187
 
188
  # Finalize order button click event
189
- finalize_button.click(finalize_order, outputs=[final_order_output])
190
 
191
  # Update menu dynamically based on preference
192
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
@@ -195,7 +136,6 @@ def app():
195
  gr.Row([selected_preference])
196
  gr.Row(menu_output)
197
  gr.Row(cart_output)
198
- gr.Row(modal_window)
199
  gr.Row(finalize_button)
200
  gr.Row(final_order_output)
201
  gr.HTML(modal_and_cart_js)
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import json
4
 
5
  # Function to load the menu data
6
  def load_menu():
 
33
  </div>
34
  <div style=\"flex-shrink: 0; text-align: center;\">
35
  <img src=\"{item['Image URL']}\" alt=\"{item['Dish Name']}\" style=\"width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;\">
36
+ <button style=\"background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;\" onclick=\"addItemToCart('{item['Dish Name']}', '{item['Price ($)']}', '{item['Description']}')\">Add</button>
37
  </div>
38
  </div>
39
  """
40
  return html_content
41
 
42
  # Function to finalize the order and display the final page
43
+ def finalize_order(cart_json):
44
+ cart = json.loads(cart_json)
45
  if not cart:
46
  return "<p style='color: red; font-size: 18px;'>Your cart is empty. Please add items before finalizing.</p>"
47
 
 
68
  "Extra Onion & Lemon": 2,
69
  "Extra Fried Onion 4oz": 2
70
  };
71
+ function addItemToCart(name, price, description) {
72
+ const item = {
73
+ name: name,
74
+ price: parseFloat(price),
75
+ spiceLevel: "Not Selected",
76
+ quantity: 1,
77
+ instructions: "",
78
+ extras: [],
79
+ itemTotal: parseFloat(price)
80
+ };
81
+ cart.push(item);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  updateCartDisplay();
 
83
  }
84
  function updateCartDisplay() {
85
  let totalBill = 0;
 
87
  cart.forEach((item, index) => {
88
  totalBill += item.itemTotal;
89
  const extras = item.extras.join(', ');
90
+ cartHTML += `<li>${item.name} (x${item.quantity}, Spice: ${item.spiceLevel}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
91
  });
92
  cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
93
  document.getElementById('floating-cart').innerHTML = cartHTML;
94
+ document.getElementById('cart-data').value = JSON.stringify(cart);
95
  }
96
+ function finalizeCart() {
97
+ document.getElementById('cart-data').value = JSON.stringify(cart);
 
98
  }
99
  </script>
100
  """
101
 
102
  # Gradio app definition
103
  def app():
 
 
 
104
  with gr.Blocks() as demo:
105
  gr.Markdown("## Dynamic Menu with Preferences")
106
 
 
117
  # Floating cart display
118
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
119
 
120
+ # Hidden input to store cart data
121
+ cart_data = gr.Textbox(visible=False, elem_id="cart-data")
122
+
123
  # Final order display
124
  final_order_output = gr.HTML(value="", elem_id="final-page")
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  # Finalize Order Button
127
  finalize_button = gr.Button("Finalize Order")
128
 
129
  # Finalize order button click event
130
+ finalize_button.click(finalize_order, inputs=[cart_data], outputs=[final_order_output])
131
 
132
  # Update menu dynamically based on preference
133
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
 
136
  gr.Row([selected_preference])
137
  gr.Row(menu_output)
138
  gr.Row(cart_output)
 
139
  gr.Row(finalize_button)
140
  gr.Row(final_order_output)
141
  gr.HTML(modal_and_cart_js)