nagasurendra commited on
Commit
45bc2f8
·
verified ·
1 Parent(s): f519f2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -115
app.py CHANGED
@@ -79,6 +79,32 @@ def update_cart():
79
  cart_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
80
  return cart_html
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  # Gradio app definition
83
  def app():
84
  with gr.Blocks() as demo:
@@ -97,129 +123,24 @@ def app():
97
  # Floating cart display
98
  cart_output = gr.HTML(value=update_cart(), elem_id="floating-cart")
99
 
100
- # JavaScript for modal and cart behavior
101
- modal_and_cart_js = """
102
- <script>
103
- let cart = [];
104
- const extrasPrices = {
105
- "Extra Raitha 4oz": 1,
106
- "Extra Raitha 8oz": 2,
107
- "Extra Salan 4oz": 1,
108
- "Extra Salan 8oz": 2,
109
- "Extra Onion": 1,
110
- "Extra Onion & Lemon": 2,
111
- "Extra Fried Onion 4oz": 2
112
- };
113
-
114
- function openModal(name, image, description, price) {
115
- document.getElementById('modal').style.display = 'block';
116
- document.getElementById('modal-image').src = image;
117
- document.getElementById('modal-name').innerText = name;
118
- document.getElementById('modal-description').innerText = description;
119
- document.getElementById('modal-price').innerText = price;
120
-
121
- // Reset spice levels and extras
122
- const spiceLevelInputs = document.querySelectorAll('input[name="spice-level"]');
123
- spiceLevelInputs.forEach(input => input.checked = false);
124
-
125
- const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
126
- extrasInputs.forEach(input => input.checked = false);
127
-
128
- document.getElementById('quantity').value = 1;
129
- document.getElementById('special-instructions').value = '';
130
- }
131
-
132
- function closeModal() {
133
- document.getElementById('modal').style.display = 'none';
134
- }
135
-
136
- function addToCart() {
137
- const name = document.getElementById('modal-name').innerText;
138
- const price = document.getElementById('modal-price').innerText;
139
- const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
140
- const quantity = parseInt(document.getElementById('quantity').value) || 1;
141
- const instructions = document.getElementById('special-instructions').value;
142
- const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
143
-
144
- const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
145
- const itemTotal = (parseFloat(price.replace('$', '')) + extrasCost) * quantity;
146
-
147
- const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal };
148
- cart.push(cartItem);
149
-
150
- alert(${name} added to cart!);
151
- updateCartDisplay();
152
- closeModal();
153
- }
154
-
155
- function updateCartDisplay() {
156
- let totalBill = 0;
157
- let cartHTML = "<h3>Your Cart:</h3><ul>";
158
- cart.forEach(item => {
159
- totalBill += item.itemTotal;
160
- const extras = item.extras.join(', ');
161
- cartHTML += <li>${item.name} (x${item.quantity}, Spice: ${item.spiceLevel}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>;
162
- });
163
- cartHTML += </ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>;
164
- document.getElementById('floating-cart').innerHTML = cartHTML;
165
- }
166
- </script>
167
- """
168
 
169
- # Modal window
170
- modal_window = gr.HTML("""
171
- <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;">
172
- <div style="text-align: right;">
173
- <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
174
- </div>
175
- <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
176
- <h2 id="modal-name"></h2>
177
- <p id="modal-description"></p>
178
- <p id="modal-price"></p>
179
- <!-- Spice Levels -->
180
- <label for="spice-level">Choose a Spice Level (Required):</label>
181
- <div id="spice-level-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
182
- <label><input type="radio" name="spice-level" value="American Mild" /> American Mild</label>
183
- <label><input type="radio" name="spice-level" value="American Medium" /> American Medium</label>
184
- <label><input type="radio" name="spice-level" value="American Spicy" /> American Spicy</label>
185
- <label><input type="radio" name="spice-level" value="Indian Mild" /> Indian Mild</label>
186
- <label><input type="radio" name="spice-level" value="Indian Medium" /> Indian Medium</label>
187
- <label><input type="radio" name="spice-level" value="Indian Very Spicy" /> Indian Very Spicy</label>
188
- </div>
189
- <!-- Biryani Extras -->
190
- <label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
191
- <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
192
- <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 4oz" /> Extra Raitha 4oz + $1.00</label>
193
- <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 8oz" /> Extra Raitha 8oz + $2.00</label>
194
- <label><input type="checkbox" name="biryani-extra" value="Extra Salan 4oz" /> Extra Salan 4oz + $1.00</label>
195
- <label><input type="checkbox" name="biryani-extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label>
196
- <label><input type="checkbox" name="biryani-extra" value="Extra Onion" /> Extra Onion + $1.00</label>
197
- <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
198
- <label><input type="checkbox" name="biryani-extra" value="Extra Fried Onion 4oz" /> Extra Fried Onion 4oz + $2.00</label>
199
- </div>
200
- <!-- Quantity and Special Instructions -->
201
- <label for="quantity">Quantity:</label>
202
- <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
203
- <br><br>
204
- <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
205
- <br><br>
206
- <!-- Add to Cart Button -->
207
- <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>
208
- </div>
209
- """)
210
 
211
- # Interactivity
212
- selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
213
 
214
  # Layout
215
  gr.Row([selected_preference])
216
  gr.Row(menu_output)
217
  gr.Row(cart_output)
218
- gr.Row(modal_window)
219
- gr.HTML(modal_and_cart_js)
220
 
221
  return demo
222
 
223
- if _name_ == "_main_":
224
  demo = app()
225
- demo.launch()
 
79
  cart_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
80
  return cart_html
81
 
82
+ # Function to submit the cart and display on a new page
83
+ def submit_cart():
84
+ if len(cart_items) == 0:
85
+ return "<h3>Your Order</h3><p>Your cart is empty.</p>"
86
+
87
+ total_bill = 0
88
+ order_html = "<h3>Your Order</h3><ul style='list-style-type: none; padding: 0;'>"
89
+ for item in cart_items:
90
+ extras = ", ".join(item.get("extras", []))
91
+ extras_cost = sum(EXTRAS_PRICES.get(extra, 0) for extra in item.get("extras", []))
92
+ item_price = float(item['price'].strip('$'))
93
+ item_total = (item_price + extras_cost) * item['quantity']
94
+ total_bill += item_total
95
+
96
+ order_html += f"<li style='margin-bottom: 20px; border: 1px solid #ddd; padding: 10px; border-radius: 8px;'>"
97
+ order_html += f"<strong>Item:</strong> {item['name']} - ${item_price:.2f}<br>"
98
+ order_html += f"<strong>Quantity x Price:</strong> {item['quantity']} x ${item_price:.2f} = ${item_price * item['quantity']:.2f}<br>"
99
+ order_html += f"<strong>Spice Level:</strong> {item['spiceLevel']}<br>"
100
+ order_html += f"<strong>Extras:</strong> {extras} - ${extras_cost:.2f}<br>"
101
+ order_html += f"<strong>Instructions:</strong> {item['instructions']}<br>"
102
+ order_html += f"<strong>Item Total:</strong> ${item_total:.2f}"
103
+ order_html += "</li>"
104
+
105
+ order_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
106
+ return order_html
107
+
108
  # Gradio app definition
109
  def app():
110
  with gr.Blocks() as demo:
 
123
  # Floating cart display
124
  cart_output = gr.HTML(value=update_cart(), elem_id="floating-cart")
125
 
126
+ # Submit button for the cart
127
+ submit_button = gr.Button("Submit Order")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
+ # Output for the new page (order details)
130
+ order_output = gr.HTML()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
+ # Submit button action to display the order details on a new page
133
+ submit_button.click(fn=submit_cart, inputs=[], outputs=order_output)
134
 
135
  # Layout
136
  gr.Row([selected_preference])
137
  gr.Row(menu_output)
138
  gr.Row(cart_output)
139
+ gr.Row([submit_button])
140
+ gr.Row(order_output)
141
 
142
  return demo
143
 
144
+ if __name__ == "__main__":
145
  demo = app()
146
+ demo.launch()