geethareddy commited on
Commit
302914a
·
verified ·
1 Parent(s): 299bcff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -180
app.py CHANGED
@@ -95,8 +95,7 @@ def filter_menu(preference):
95
  for item in items:
96
  html_content += f"""
97
  <div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;">
98
- <img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;"
99
- onclick="openModal('{item['Name']}', '{item.get('Image2__c', '')}', '{item['Description__c']}', '${item['Price__c']}')">
100
  <div style="padding: 10px;">
101
  <h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3>
102
  <p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p>
@@ -112,152 +111,37 @@ def filter_menu(preference):
112
 
113
  return html_content
114
 
115
- # Create Modal Window HTML
116
- def create_modal_window():
117
- add_ons = load_add_ons_from_salesforce()
118
- add_ons_html = ""
119
- for add_on in add_ons:
120
- add_ons_html += f"""
121
- <label>
122
- <input type="checkbox" name="biryani-extra" value="{add_on['Name']}" data-price="{add_on['Price__c']}" />
123
- {add_on['Name']} + ${add_on['Price__c']}
124
- </label>
125
- <br>
 
 
 
 
 
 
 
 
 
 
126
  """
 
127
 
128
- modal_html = f"""
129
- <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;">
130
- <div style="text-align: right;">
131
- <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
132
- </div>
133
- <img id="modal-image" style="width: 100%; height: 300px; border-radius: 8px; margin-bottom: 20px;" />
134
- <h2 id="modal-name"></h2>
135
- <p id="modal-description"></p>
136
- <p id="modal-price"></p>
137
- <label for="biryani-extras"><strong>Add-ons :</strong></label>
138
- <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
139
- {add_ons_html}
140
- </div>
141
- <label for="quantity">Quantity:</label>
142
- <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
143
- <textarea id="special-instructions" placeholder="Add your special instructions here..." style="width: 100%; height: 60px;"></textarea>
144
- <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>
145
- </div>
146
- """
147
- return modal_html
148
-
149
- # JavaScript for Modal and Cart
150
- def modal_js():
151
- modal_script = """
152
- <script>
153
- let cart = [];
154
- let totalCartCost = 0;
155
- function openModal(name, image2, description, price) {
156
- const modal = document.getElementById('modal');
157
- modal.style.display = 'block';
158
- modal.style.position = 'fixed';
159
- modal.style.width = window.innerWidth <= 768 ? '90%' : '30%';
160
- modal.style.top = ${event.clientY}px;
161
- modal.style.left = '50%';
162
- modal.style.transform = 'translate(-50%, -50%)';
163
- document.getElementById('modal-image').src = image2;
164
- document.getElementById('modal-name').innerText = name;
165
- document.getElementById('modal-description').innerText = description;
166
- document.getElementById('modal-price').innerText = price;
167
- document.getElementById('quantity').value = 1;
168
- document.getElementById('special-instructions').value = '';
169
- resetAddOns(); // Reset add-ons when opening the modal
170
- }
171
- function closeModal() {
172
- document.getElementById('modal').style.display = 'none';
173
- }
174
- function addToCart() {
175
- const name = document.getElementById('modal-name').innerText;
176
- const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
177
- const quantity = parseInt(document.getElementById('quantity').value) || 1;
178
- const instructions = document.getElementById('special-instructions').value;
179
- const selectedAddOns = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'));
180
- const extras = selectedAddOns.map(extra => ({
181
- name: extra.value,
182
- price: parseFloat(extra.getAttribute('data-price')),
183
- quantity: 1 // Default quantity for add-ons is 1
184
- }));
185
- const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
186
- const totalCost = (price * quantity) + extrasCost;
187
- // Add the item to the cart with its specific add-ons
188
- cart.push({ name, price, quantity, extras, instructions, totalCost });
189
- totalCartCost += totalCost; // Update the total cost of the cart
190
- updateCartButton();
191
- updateCartTotalCost(); // Update total cost displayed
192
- closeModal();
193
- }
194
- function updateCartButton() {
195
- const cartButton = document.getElementById('cart-button');
196
- cartButton.innerText = View Cart (${cart.length} items);
197
- }
198
- function openCartModal() {
199
- const cartModal = document.getElementById('cart-modal');
200
- const cartItemsContainer = document.getElementById('cart-items');
201
- cartItemsContainer.innerHTML = "";
202
- cart.forEach((item, index) => {
203
- const extrasList = item.extras.map(extra => ${extra.name} x<input type="number" value="${extra.quantity}" min="1" style="width: 50px;" onchange="updateCartItem(${index}, 'extra', this.value)" /> (+$${(extra.price * extra.quantity).toFixed(2)})).join(', ');
204
- cartItemsContainer.innerHTML +=
205
- <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
206
- <h3>${item.name}</h3>
207
- <p>Quantity: <input type="number" value="${item.quantity}" min="1" style="width: 50px;" onchange="updateCartItem(${index}, 'item', this.value)" /></p>
208
- <p>Extras: ${extrasList || 'None'}</p>
209
- <p>Special Instructions: ${item.instructions || 'None'}</p>
210
- <p>Total Cost: $<span id="item-${index}-total">${item.totalCost.toFixed(2)}</span></p>
211
- <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
212
- </div>
213
- ;
214
- });
215
- cartModal.style.display = 'block';
216
- }
217
- function closeCartModal() {
218
- document.getElementById('cart-modal').style.display = 'none';
219
- }
220
- function removeFromCart(index) {
221
- totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
222
- cart.splice(index, 1);
223
- updateCartButton();
224
- updateCartTotalCost(); // Update total cost displayed
225
- openCartModal();
226
- }
227
- function updateCartItem(index, type, value) {
228
- if (type === 'item') {
229
- cart[index].quantity = parseInt(value);
230
- } else if (type === 'extra') {
231
- cart[index].extras[0].quantity = parseInt(value); // Assuming one add-on for simplicity
232
- }
233
- const item = cart[index];
234
- const price = item.price;
235
- const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
236
- item.totalCost = (price * item.quantity) + extrasCost;
237
- document.getElementById(item-${index}-total).innerText = item.totalCost.toFixed(2);
238
- updateCartTotalCost(); // Update total cost displayed
239
- }
240
- function updateCartTotalCost() {
241
- const totalCostElement = document.getElementById('cart-total-cost');
242
- totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
243
- totalCostElement.innerText = Total Cart Cost: $${totalCartCost.toFixed(2)};
244
- }
245
- function proceedToCheckout() {
246
- alert("Proceeding to checkout...");
247
- }
248
- // Reset all selected add-ons when opening a new item modal
249
- function resetAddOns() {
250
- const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
251
- checkboxes.forEach(checkbox => checkbox.checked = false); // Uncheck all add-ons
252
- }
253
- </script>
254
- """
255
- return modal_script
256
-
257
- # Updated Gradio App
258
- with gr.Blocks() as app:
259
- cart_data = gr.State([]) # State to hold cart data
260
 
 
261
  with gr.Row():
262
  gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
263
 
@@ -284,45 +168,59 @@ with gr.Blocks() as app:
284
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
285
  menu_output = gr.HTML()
286
  gr.HTML("<div id='cart-button' style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>")
287
- gr.HTML("<div id='cart-modal' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;'><div style='padding: 20px;'><div style='text-align: right;'><button onclick='closeCartModal()' style='background: none; border: none; font-size: 24px; cursor: pointer;'>&times;</button></div><h1>Your Cart</h1><div id='cart-items'></div><p id='cart-total-cost' style='font-size: 1.2em; font-weight: bold;'>Total Cart Cost: $0.00</p><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button><button id='final-submit' style='background: #28a745; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;'>Final Submit</button></div></div>")
288
  gr.HTML(create_modal_window())
289
- gr.HTML(modal_js())
290
-
291
- # New Page to Display Final Cart Details
292
- with gr.Row(visible=False) as final_page:
293
- cart_details = gr.HTML()
294
- back_to_menu = gr.Button("Back to Menu")
295
-
296
- # Show Final Page with Cart Details
297
- def show_final_page(cart):
298
- if not cart:
299
- return gr.update(visible=False), "Your cart is empty!"
300
- details = "<h2>Your Order Summary:</h2><ul>"
301
- for item in cart:
302
- details += f"<li>{item['quantity']} x {item['name']} (${item['price']} each)"
303
- if item['extras']:
304
- details += f"<br>Add-ons: {', '.join([extra['name'] for extra in item['extras']])}</li>"
305
- details += "</li>"
306
- details += f"</ul><p><strong>Total Cost: ${sum(item['totalCost'] for item in cart):.2f}</strong></p>"
307
- return gr.update(visible=True), details
308
-
309
- # Button Click Handlers
310
  login_button.click(
311
- lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
312
- if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
313
- [login_email, login_password], [login_page, menu_page, menu_output, login_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  )
315
- preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
316
- gr.HTML(modal_js())
317
-
318
- # Final Submit Button
319
- menu_page.children[-1].click(
320
- lambda cart: (gr.update(visible=False), gr.update(visible=True, value=show_final_page(cart)[1])),
321
- [cart_data],
322
- [menu_page, final_page, cart_details]
 
 
 
 
 
 
 
 
 
323
  )
324
 
325
- # Back to Menu Button
326
- back_to_menu.click(lambda: (gr.update(visible=True), gr.update(visible=False)), None, [menu_page, final_page])
 
 
 
 
 
 
 
 
 
327
 
328
  app.launch()
 
95
  for item in items:
96
  html_content += f"""
97
  <div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;">
98
+ <img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;">
 
99
  <div style="padding: 10px;">
100
  <h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3>
101
  <p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p>
 
111
 
112
  return html_content
113
 
114
+ # Generate final order summary
115
+ def generate_order_summary(cart):
116
+ if not cart:
117
+ return "<p>Your cart is empty.</p>"
118
+
119
+ summary_html = "<h2>Order Summary</h2>"
120
+ total_cost = 0
121
+
122
+ for item in cart:
123
+ item_cost = item['price'] * item['quantity']
124
+ add_ons_summary = "<ul>" + "".join([f"<li>{addon['name']} x {addon['quantity']} (+${addon['price'] * addon['quantity']:.2f})</li>" for addon in item['extras']]) + "</ul>"
125
+ total_item_cost = item_cost + sum(addon['price'] * addon['quantity'] for addon in item['extras'])
126
+ summary_html += f"""
127
+ <div style='margin-bottom: 20px;'>
128
+ <h3>{item['name']}</h3>
129
+ <p>Quantity: {item['quantity']}</p>
130
+ <p>Special Instructions: {item['instructions'] or 'None'}</p>
131
+ <p>Add-ons:</p>
132
+ {add_ons_summary}
133
+ <p>Total Cost for this Item: ${total_item_cost:.2f}</p>
134
+ </div>
135
  """
136
+ total_cost += total_item_cost
137
 
138
+ summary_html += f"<h3>Total Cart Cost: ${total_cost:.2f}</h3>"
139
+ return summary_html
140
+
141
+ # Gradio App
142
+ cart = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
+ with gr.Blocks() as app:
145
  with gr.Row():
146
  gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
147
 
 
168
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
169
  menu_output = gr.HTML()
170
  gr.HTML("<div id='cart-button' style='position: fixed; top: 20px; right: 20px; background: #28a745; color: white; padding: 10px 20px; border-radius: 30px; cursor: pointer; z-index: 1000;' onclick='openCartModal()'>View Cart</div>")
171
+ gr.HTML("<div id='cart-modal' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;'><div style='padding: 20px;'><div style='text-align: right;'><button onclick='closeCartModal()' style='background: none; border: none; font-size: 24px; cursor: pointer;'>&times;</button></div><h1>Your Cart</h1><div id='cart-items'></div><p id='cart-total-cost' style='font-size: 1.2em; font-weight: bold;'>Total Cart Cost: $0.00</p><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button></div></div>")
172
  gr.HTML(create_modal_window())
173
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  login_button.click(
175
+ lambda email, password: (
176
+ gr.update(visible=False),
177
+ gr.update(visible=True),
178
+ gr.update(value=filter_menu("All")),
179
+ "Login successful!"
180
+ ) if login(email, password)[0] == "Login successful!" else (
181
+ gr.update(),
182
+ gr.update(),
183
+ gr.update(),
184
+ "Invalid email or password."
185
+ ),
186
+ [login_email, login_password],
187
+ [login_page, menu_page, menu_output, login_output]
188
+ )
189
+
190
+ signup_button.click(
191
+ lambda: (gr.update(visible=False), gr.update(visible=True)),
192
+ inputs=[],
193
+ outputs=[login_page, signup_page]
194
  )
195
+
196
+ login_redirect.click(
197
+ lambda: (gr.update(visible=True), gr.update(visible=False)),
198
+ inputs=[],
199
+ outputs=[login_page, signup_page]
200
+ )
201
+
202
+ submit_signup.click(
203
+ lambda name, email, phone, password: signup(name, email, phone, password),
204
+ [signup_name, signup_email, signup_phone, signup_password],
205
+ signup_output
206
+ )
207
+
208
+ preference.change(
209
+ lambda pref: filter_menu(pref),
210
+ [preference],
211
+ menu_output
212
  )
213
 
214
+ submit_order = gr.Button("Submit Order")
215
+ order_summary = gr.HTML(visible=False)
216
+
217
+ submit_order.click(
218
+ lambda: gr.update(visible=True, value=generate_order_summary(cart)),
219
+ inputs=[],
220
+ outputs=[order_summary]
221
+ )
222
+
223
+ with gr.Row():
224
+ gr.HTML(value="", visible=False).style(name="order-summary")
225
 
226
  app.launch()