nagasurendra commited on
Commit
54b5ff0
·
verified ·
1 Parent(s): 2653a35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -58
app.py CHANGED
@@ -12,6 +12,17 @@ def load_menu():
12
  # Initialize cart globally
13
  cart_items = []
14
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Function to filter menu items based on preference
16
  def filter_menu(preference):
17
  menu_data = load_menu()
@@ -27,31 +38,34 @@ def filter_menu(preference):
27
  html_content = ""
28
  for _, item in filtered_data.iterrows():
29
  html_content += f"""
30
- <div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
31
- <div style="flex: 1; margin-right: 15px;">
32
- <h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
33
- <p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
34
- <p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
35
  </div>
36
- <div style="flex-shrink: 0; text-align: center;">
37
- <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
38
- <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>
39
  </div>
40
  </div>
41
  """
42
  return html_content
43
 
44
  # Function to update the cart display
45
- def update_cart(cart):
46
- global cart_items
47
- cart_items = cart # Update global cart items
48
  if len(cart_items) == 0:
49
  return "Your cart is empty."
 
 
50
  cart_html = "<h3>Your Cart:</h3><ul>"
51
  for item in cart_items:
52
- extras = ", ".join(item["extras"])
53
- cart_html += f"<li>{item['name']} (x{item['quantity']}, Spice: {item['spiceLevel']}, Extras: {extras}) - {item['price']}</li>"
54
- cart_html += "</ul>"
 
 
 
55
  return cart_html
56
 
57
  # Gradio app definition
@@ -69,8 +83,63 @@ def app():
69
  # Output area for menu items
70
  menu_output = gr.HTML(value=filter_menu("All"))
71
 
72
- # Cart display
73
- cart_output = gr.HTML(value="Your cart is empty.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  # Modal window
76
  modal_window = gr.HTML("""
@@ -82,7 +151,6 @@ def app():
82
  <h2 id="modal-name"></h2>
83
  <p id="modal-description"></p>
84
  <p id="modal-price"></p>
85
-
86
  <!-- Spice Levels -->
87
  <label for="spice-level">Choose a Spice Level (Required):</label>
88
  <div id="spice-level-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
@@ -93,7 +161,6 @@ def app():
93
  <label><input type="radio" name="spice-level" value="Indian Medium" required /> Indian Medium</label>
94
  <label><input type="radio" name="spice-level" value="Indian Very Spicy" required /> Indian Very Spicy</label>
95
  </div>
96
-
97
  <!-- Biryani Extras -->
98
  <label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
99
  <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
@@ -103,65 +170,28 @@ def app():
103
  <label><input type="checkbox" name="biryani-extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label>
104
  <label><input type="checkbox" name="biryani-extra" value="Extra Onion" /> Extra Onion + $1.00</label>
105
  <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
106
- <label><input type="checkbox" name="biryani-extra" value="Extra Fried Onion" /> Extra Fried Onion 4oz + $2.00</label>
107
  </div>
108
-
109
  <!-- Quantity and Special Instructions -->
110
  <label for="quantity">Quantity:</label>
111
  <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
112
  <br><br>
113
  <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
114
  <br><br>
115
-
116
  <!-- Add to Cart Button -->
117
  <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>
118
  </div>
119
- <script>
120
- let cart = [];
121
-
122
- function openModal(name, image, description, price) {
123
- document.getElementById('modal').style.display = 'block';
124
- document.getElementById('modal-image').src = image;
125
- document.getElementById('modal-name').innerText = name;
126
- document.getElementById('modal-description').innerText = description;
127
- document.getElementById('modal-price').innerText = price;
128
- }
129
-
130
- function closeModal() {
131
- document.getElementById('modal').style.display = 'none';
132
- }
133
-
134
- function addToCart() {
135
- const name = document.getElementById('modal-name').innerText;
136
- const price = document.getElementById('modal-price').innerText;
137
- const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
138
- const quantity = parseInt(document.getElementById('quantity').value) || 1;
139
- const instructions = document.getElementById('special-instructions').value;
140
- const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
141
-
142
- const cartItem = { name, price, spiceLevel, quantity, instructions, extras };
143
- cart.push(cartItem);
144
-
145
- fetch("/update_cart", {
146
- method: "POST",
147
- headers: { "Content-Type": "application/json" },
148
- body: JSON.stringify(cart)
149
- });
150
-
151
- alert(`${name} added to cart!`);
152
- closeModal();
153
- }
154
- </script>
155
  """)
156
 
157
  # Interactivity
158
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
159
 
160
- # Add the cart output
161
  gr.Row([selected_preference])
162
  gr.Row(menu_output)
163
  gr.Row(cart_output)
164
  gr.Row(modal_window)
 
165
 
166
  return demo
167
 
 
12
  # Initialize cart globally
13
  cart_items = []
14
 
15
+ # Pricing for extras
16
+ EXTRAS_PRICES = {
17
+ "Extra Raitha 4oz": 1,
18
+ "Extra Raitha 8oz": 2,
19
+ "Extra Salan 4oz": 1,
20
+ "Extra Salan 8oz": 2,
21
+ "Extra Onion": 1,
22
+ "Extra Onion & Lemon": 2,
23
+ "Extra Fried Onion 4oz": 2,
24
+ }
25
+
26
  # Function to filter menu items based on preference
27
  def filter_menu(preference):
28
  menu_data = load_menu()
 
38
  html_content = ""
39
  for _, item in filtered_data.iterrows():
40
  html_content += f"""
41
+ <div style=\"display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);\">
42
+ <div style=\"flex: 1; margin-right: 15px;\">
43
+ <h3 style=\"margin: 0; font-size: 18px;\">{item['Dish Name']}</h3>
44
+ <p style=\"margin: 5px 0; font-size: 16px; color: #888;\">${item['Price ($)']}</p>
45
+ <p style=\"margin: 5px 0; font-size: 14px; color: #555;\">{item['Description']}</p>
46
  </div>
47
+ <div style=\"flex-shrink: 0; text-align: center;\">
48
+ <img src=\"{item['Image URL']}\" alt=\"{item['Dish Name']}\" style=\"width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;\">
49
+ <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>
50
  </div>
51
  </div>
52
  """
53
  return html_content
54
 
55
  # Function to update the cart display
56
+ def update_cart():
 
 
57
  if len(cart_items) == 0:
58
  return "Your cart is empty."
59
+
60
+ total_bill = 0
61
  cart_html = "<h3>Your Cart:</h3><ul>"
62
  for item in cart_items:
63
+ extras = ", ".join(item.get("extras", []))
64
+ extras_cost = sum(EXTRAS_PRICES.get(extra, 0) for extra in item.get("extras", []))
65
+ item_total = (float(item['price'].strip('$')) + extras_cost) * item['quantity']
66
+ total_bill += item_total
67
+ cart_html += f"<li>{item['name']} (x{item['quantity']}, Spice: {item['spiceLevel']}, Extras: {extras}, Instructions: {item['instructions']}) - ${item_total:.2f}</li>"
68
+ cart_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
69
  return cart_html
70
 
71
  # Gradio app definition
 
83
  # Output area for menu items
84
  menu_output = gr.HTML(value=filter_menu("All"))
85
 
86
+ # Floating cart display
87
+ cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
88
+
89
+ # JavaScript for modal and cart behavior
90
+ modal_and_cart_js = """
91
+ <script>
92
+ let cart = [];
93
+
94
+ function openModal(name, image, description, price) {
95
+ document.getElementById('modal').style.display = 'block';
96
+ document.getElementById('modal-image').src = image;
97
+ document.getElementById('modal-name').innerText = name;
98
+ document.getElementById('modal-description').innerText = description;
99
+ document.getElementById('modal-price').innerText = price;
100
+ }
101
+
102
+ function closeModal() {
103
+ document.getElementById('modal').style.display = 'none';
104
+ }
105
+
106
+ function addToCart() {
107
+ const name = document.getElementById('modal-name').innerText;
108
+ const price = document.getElementById('modal-price').innerText;
109
+ const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
110
+ const quantity = parseInt(document.getElementById('quantity').value) || 1;
111
+ const instructions = document.getElementById('special-instructions').value;
112
+ const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
113
+
114
+ const cartItem = { name, price, spiceLevel, quantity, instructions, extras };
115
+ cart.push(cartItem);
116
+
117
+ fetch("/update_cart", {
118
+ method: "POST",
119
+ headers: { "Content-Type": "application/json" },
120
+ body: JSON.stringify(cart)
121
+ });
122
+
123
+ alert(`${name} added to cart!`);
124
+ closeModal();
125
+ updateCartDisplay();
126
+ }
127
+
128
+ function updateCartDisplay() {
129
+ let totalBill = 0;
130
+ let cartHTML = "<h3>Your Cart:</h3><ul>";
131
+ cart.forEach(item => {
132
+ let extrasCost = item.extras.reduce((sum, extra) => sum + (EXTRAS_PRICES[extra] || 0), 0);
133
+ let itemTotal = (parseFloat(item.price.replace('$', '')) + extrasCost) * item.quantity;
134
+ totalBill += itemTotal;
135
+ let extras = item.extras.join(', ');
136
+ cartHTML += `<li>${item.name} (x${item.quantity}, Spice: ${item.spiceLevel}, Extras: ${extras}, Instructions: ${item.instructions}) - $${itemTotal.toFixed(2)}</li>`;
137
+ });
138
+ cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
139
+ document.getElementById('floating-cart').innerHTML = cartHTML;
140
+ }
141
+ </script>
142
+ """
143
 
144
  # Modal window
145
  modal_window = gr.HTML("""
 
151
  <h2 id="modal-name"></h2>
152
  <p id="modal-description"></p>
153
  <p id="modal-price"></p>
 
154
  <!-- Spice Levels -->
155
  <label for="spice-level">Choose a Spice Level (Required):</label>
156
  <div id="spice-level-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
 
161
  <label><input type="radio" name="spice-level" value="Indian Medium" required /> Indian Medium</label>
162
  <label><input type="radio" name="spice-level" value="Indian Very Spicy" required /> Indian Very Spicy</label>
163
  </div>
 
164
  <!-- Biryani Extras -->
165
  <label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
166
  <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
 
170
  <label><input type="checkbox" name="biryani-extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label>
171
  <label><input type="checkbox" name="biryani-extra" value="Extra Onion" /> Extra Onion + $1.00</label>
172
  <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
173
+ <label><input type="checkbox" name="biryani-extra" value="Extra Fried Onion 4oz" /> Extra Fried Onion 4oz + $2.00</label>
174
  </div>
 
175
  <!-- Quantity and Special Instructions -->
176
  <label for="quantity">Quantity:</label>
177
  <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
178
  <br><br>
179
  <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
180
  <br><br>
 
181
  <!-- Add to Cart Button -->
182
  <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>
183
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  """)
185
 
186
  # Interactivity
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.HTML(modal_and_cart_js)
195
 
196
  return demo
197