Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -112,19 +112,7 @@ def filter_menu(preference):
|
|
112 |
|
113 |
return html_content
|
114 |
|
115 |
-
#
|
116 |
-
def show_order_summary(cart):
|
117 |
-
total_cost = sum(item['totalCost'] for item in cart)
|
118 |
-
order_summary = "<h2>Your Final Order</h2>"
|
119 |
-
order_summary += "<ul>"
|
120 |
-
for item in cart:
|
121 |
-
order_summary += f"""
|
122 |
-
<li>{item['name']} (x{item['quantity']}) - ${item['totalCost']}</li>
|
123 |
-
"""
|
124 |
-
order_summary += f"</ul><h3>Total Bill: ${total_cost}</h3>"
|
125 |
-
return order_summary
|
126 |
-
|
127 |
-
# Function to create Modal Window for Menu Item Details
|
128 |
def create_modal_window():
|
129 |
add_ons = load_add_ons_from_salesforce()
|
130 |
add_ons_html = ""
|
@@ -158,14 +146,12 @@ def create_modal_window():
|
|
158 |
"""
|
159 |
return modal_html
|
160 |
|
161 |
-
# JavaScript for Modal and Cart
|
162 |
def modal_js():
|
163 |
modal_script = """
|
164 |
<script>
|
165 |
let cart = [];
|
166 |
let totalCartCost = 0;
|
167 |
-
|
168 |
-
// Open Item Modal
|
169 |
function openModal(name, image2, description, price) {
|
170 |
const modal = document.getElementById('modal');
|
171 |
modal.style.display = 'block';
|
@@ -180,15 +166,11 @@ def modal_js():
|
|
180 |
document.getElementById('modal-price').innerText = price;
|
181 |
document.getElementById('quantity').value = 1;
|
182 |
document.getElementById('special-instructions').value = '';
|
183 |
-
resetAddOns();
|
184 |
}
|
185 |
-
|
186 |
-
// Close the Modal
|
187 |
function closeModal() {
|
188 |
document.getElementById('modal').style.display = 'none';
|
189 |
}
|
190 |
-
|
191 |
-
// Add Item to Cart
|
192 |
function addToCart() {
|
193 |
const name = document.getElementById('modal-name').innerText;
|
194 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
@@ -198,25 +180,21 @@ def modal_js():
|
|
198 |
const extras = selectedAddOns.map(extra => ({
|
199 |
name: extra.value,
|
200 |
price: parseFloat(extra.getAttribute('data-price')),
|
201 |
-
quantity: 1
|
202 |
}));
|
203 |
const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
204 |
const totalCost = (price * quantity) + extrasCost;
|
205 |
-
|
206 |
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
207 |
-
totalCartCost += totalCost;
|
208 |
updateCartButton();
|
209 |
-
updateCartTotalCost();
|
210 |
closeModal();
|
211 |
}
|
212 |
-
|
213 |
-
// Update Cart Button
|
214 |
function updateCartButton() {
|
215 |
const cartButton = document.getElementById('cart-button');
|
216 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
217 |
}
|
218 |
-
|
219 |
-
// View Cart Modal
|
220 |
function openCartModal() {
|
221 |
const cartModal = document.getElementById('cart-modal');
|
222 |
const cartItemsContainer = document.getElementById('cart-items');
|
@@ -236,70 +214,48 @@ def modal_js():
|
|
236 |
});
|
237 |
cartModal.style.display = 'block';
|
238 |
}
|
239 |
-
|
240 |
-
// Close Cart Modal
|
241 |
function closeCartModal() {
|
242 |
document.getElementById('cart-modal').style.display = 'none';
|
243 |
}
|
244 |
-
|
245 |
-
// Remove Item from Cart
|
246 |
function removeFromCart(index) {
|
247 |
-
totalCartCost -= cart[index].totalCost;
|
248 |
cart.splice(index, 1);
|
249 |
updateCartButton();
|
250 |
-
updateCartTotalCost();
|
251 |
openCartModal();
|
252 |
}
|
253 |
-
|
254 |
-
// Update Cart Item
|
255 |
function updateCartItem(index, type, value) {
|
256 |
if (type === 'item') {
|
257 |
cart[index].quantity = parseInt(value);
|
258 |
} else if (type === 'extra') {
|
259 |
-
cart[index].extras[0].quantity = parseInt(value);
|
260 |
}
|
261 |
const item = cart[index];
|
262 |
const price = item.price;
|
263 |
const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
264 |
item.totalCost = (price * item.quantity) + extrasCost;
|
265 |
document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
|
266 |
-
updateCartTotalCost();
|
267 |
}
|
268 |
-
|
269 |
-
// Update Total Cart Cost
|
270 |
function updateCartTotalCost() {
|
271 |
const totalCostElement = document.getElementById('cart-total-cost');
|
272 |
totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
|
273 |
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
274 |
}
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
<h2>Your Final Order</h2>
|
281 |
-
<ul>
|
282 |
-
${cart.map(item => `
|
283 |
-
<li>${item.name} (x${item.quantity}) - $${item.totalCost}</li>
|
284 |
-
`).join('')}
|
285 |
-
</ul>
|
286 |
-
<h3>Total Bill: $${totalCartCost.toFixed(2)}</h3>
|
287 |
-
</div>
|
288 |
-
`;
|
289 |
-
document.getElementById('order-summary').innerHTML = orderSummary;
|
290 |
-
}
|
291 |
-
|
292 |
-
// Reset all selected add-ons when opening a new item modal
|
293 |
-
function resetAddOns() {
|
294 |
-
const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
|
295 |
-
checkboxes.forEach(checkbox => checkbox.checked = false);
|
296 |
}
|
297 |
</script>
|
298 |
"""
|
299 |
return modal_script
|
300 |
|
|
|
301 |
# Gradio App
|
302 |
with gr.Blocks() as app:
|
|
|
303 |
with gr.Row():
|
304 |
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
305 |
|
@@ -326,10 +282,9 @@ with gr.Blocks() as app:
|
|
326 |
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
327 |
menu_output = gr.HTML()
|
328 |
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>")
|
329 |
-
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;'>×</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='
|
330 |
gr.HTML(create_modal_window())
|
331 |
gr.HTML(modal_js())
|
332 |
-
gr.HTML("<div id='order-summary'></div>")
|
333 |
|
334 |
login_button.click(
|
335 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
|
|
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 = ""
|
|
|
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';
|
|
|
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('$', ''));
|
|
|
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');
|
|
|
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 submitOrder() {
|
246 |
+
const orderDetails = cart.map(item => `${item.name} (x${item.quantity}) - $${item.totalCost.toFixed(2)}`).join('<br>');
|
247 |
+
const totalCost = cart.reduce((total, item) => total + item.totalCost, 0).toFixed(2);
|
248 |
+
document.getElementById('order-summary').innerHTML = orderDetails;
|
249 |
+
document.getElementById('total-bill').innerText = `$${totalCost}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
}
|
251 |
</script>
|
252 |
"""
|
253 |
return modal_script
|
254 |
|
255 |
+
|
256 |
# Gradio App
|
257 |
with gr.Blocks() as app:
|
258 |
+
# Login and Signup pages
|
259 |
with gr.Row():
|
260 |
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
261 |
|
|
|
282 |
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
283 |
menu_output = gr.HTML()
|
284 |
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>")
|
285 |
+
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;'>×</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='submitOrder()'>Submit Order</button></div></div>")
|
286 |
gr.HTML(create_modal_window())
|
287 |
gr.HTML(modal_js())
|
|
|
288 |
|
289 |
login_button.click(
|
290 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|