nagasurendra commited on
Commit
e44b01e
·
verified ·
1 Parent(s): 840a665

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -53
app.py CHANGED
@@ -1,6 +1,5 @@
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,28 +32,12 @@ def filter_menu(preference):
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=\"openModal('{item['Dish Name']}', '{item['Price ($)']}', '{item['Description']}', '{item['Image URL']}')\">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
-
48
- total_bill = 0
49
- final_order_html = "<h3>Your Final Order:</h3><ul>"
50
- for item in cart:
51
- total_bill += item['itemTotal']
52
- extras = ', '.join(item['extras']) if item['extras'] else 'None'
53
- final_order_html += f"<li>{item['name']} (x{item['quantity']}, Spice: {item['spiceLevel']}, Extras: {extras}, Instructions: {item['instructions']}) - ${item['itemTotal']:.2f}</li>"
54
- final_order_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
55
- final_order_html += "<p style='color: green; font-size: 18px;'>Your final order has been placed. Thank you!</p>"
56
- return final_order_html
57
-
58
  # JavaScript for modal and cart behavior
59
  modal_and_cart_js = """
60
  <script>
@@ -68,12 +51,18 @@ modal_and_cart_js = """
68
  "Extra Onion & Lemon": 2,
69
  "Extra Fried Onion 4oz": 2
70
  };
71
- function openModal(name, price, description, image) {
 
 
 
 
 
72
  document.getElementById('modal').style.display = 'block';
 
 
73
  document.getElementById('modal-name').innerText = name;
74
- document.getElementById('modal-price').innerText = price;
75
  document.getElementById('modal-description').innerText = description;
76
- document.getElementById('modal-image').src = image;
77
 
78
  const spiceLevelInputs = document.querySelectorAll('input[name="spice-level"]');
79
  spiceLevelInputs.forEach(input => input.checked = false);
@@ -84,8 +73,13 @@ modal_and_cart_js = """
84
  }
85
  function closeModal() {
86
  document.getElementById('modal').style.display = 'none';
 
87
  }
88
  function addToCart() {
 
 
 
 
89
  const name = document.getElementById('modal-name').innerText;
90
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
91
  const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
@@ -96,24 +90,9 @@ modal_and_cart_js = """
96
  const itemTotal = (price + extrasCost) * quantity;
97
  const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal };
98
  cart.push(cartItem);
99
- updateCartDisplay();
100
  closeModal();
101
  }
102
- function updateCartDisplay() {
103
- let totalBill = 0;
104
- let cartHTML = "<h3>Your Cart:</h3><ul>";
105
- cart.forEach((item, index) => {
106
- totalBill += item.itemTotal;
107
- const extras = item.extras.join(', ');
108
- cartHTML += `<li>${item.name} (x${item.quantity}, Spice: ${item.spiceLevel}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
109
- });
110
- cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
111
- document.getElementById('floating-cart').innerHTML = cartHTML;
112
- document.getElementById('cart-data').value = JSON.stringify(cart);
113
- }
114
- function finalizeCart() {
115
- document.getElementById('cart-data').value = JSON.stringify(cart);
116
- }
117
  </script>
118
  """
119
 
@@ -122,26 +101,16 @@ def app():
122
  with gr.Blocks() as demo:
123
  gr.Markdown("## Dynamic Menu with Preferences")
124
 
125
- # Radio button for selecting preference
126
  selected_preference = gr.Radio(
127
  choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
128
  value="All",
129
  label="Choose a Preference",
130
  )
131
 
132
- # Output area for menu items
133
  menu_output = gr.HTML(value=filter_menu("All"))
134
 
135
- # Floating cart display
136
  cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
137
 
138
- # Hidden input to store cart data
139
- cart_data = gr.Textbox(visible=False, elem_id="cart-data")
140
-
141
- # Final order display
142
- final_order_output = gr.HTML(value="", elem_id="final-page")
143
-
144
- # Modal window for item customization
145
  modal_window = gr.HTML("""
146
  <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;">
147
  <div style="text-align: right;">
@@ -183,22 +152,17 @@ def app():
183
  </div>
184
  """)
185
 
186
- # Finalize Order Button
187
  finalize_button = gr.Button("Finalize Order")
188
 
189
- # Finalize order button click event
190
- finalize_button.click(finalize_order, inputs=[cart_data], outputs=[final_order_output])
191
 
192
- # Update menu dynamically based on preference
193
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
194
 
195
- # Layout
196
  gr.Row([selected_preference])
197
  gr.Row(menu_output)
198
  gr.Row(cart_output)
199
  gr.Row(modal_window)
200
  gr.Row(finalize_button)
201
- gr.Row(final_order_output)
202
  gr.HTML(modal_and_cart_js)
203
 
204
  return demo
 
1
  import gradio as gr
2
  import pandas as pd
 
3
 
4
  # Function to load the menu data
5
  def load_menu():
 
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
  # JavaScript for modal and cart behavior
42
  modal_and_cart_js = """
43
  <script>
 
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.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);
 
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) {
80
+ alert("You cannot add more items after finalizing your order.");
81
+ return;
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";
 
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
  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;">
 
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