Spaces:
Build error
Build error
Update app.py
Browse files12334526 unmodified lines32333436353637383940424344454647484950515253545556574142437 unmodified lines5152535471555657585960616263746476656667684 unmodified lines737475767778798081828384854 unmodified lines909192999394951021031041051061071081091101111121131141151169697982 unmodified lines10110210312510410510610710810913211011113511211313813914014114214314411411511635 unmodified lines152153154186155156189190157158192159160195161162163164165201166167168import gradio as grimport pandas as pdimport json
# Function to load the menu datadef load_menu():26 unmodified lines </div> <div style=\"flex-shrink: 0; text-align: center;\"> <img src=\"{item['Image URL']}\" alt=\"{item['Dish Name']}\" style=\"width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;\"> <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> <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> </div> </div> """ return html_content
# Function to finalize the order and display the final pagedef finalize_order(cart_json): cart = json.loads(cart_json) if not cart: return "<p style='color: red; font-size: 18px;'>Your cart is empty. Please add items before finalizing.</p>"
total_bill = 0 final_order_html = "<h3>Your Final Order:</h3><ul>" for item in cart: total_bill += item['itemTotal'] extras = ', '.join(item['extras']) if item['extras'] else 'None' final_order_html += f"<li>{item['name']} (x{item['quantity']}, Spice: {item['spiceLevel']}, Extras: {extras}, Instructions: {item['instructions']}) - ${item['itemTotal']:.2f}</li>" final_order_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>" final_order_html += "<p style='color: green; font-size: 18px;'>Your final order has been placed. Thank you!</p>" return final_order_html
# JavaScript for modal and cart behaviormodal_and_cart_js = """<script>7 unmodified lines "Extra Onion & Lemon": 2, "Extra Fried Onion 4oz": 2 }; let finalized = false; function openModal(name, price, description, image) { function openModal(name, image, description, price) { if (finalized) { alert("You cannot add more items after finalizing your order."); return; } document.getElementById('modal').style.display = 'block'; document.body.style.overflow = 'hidden'; // Disable scrolling document.getElementById('modal-image').src = image; document.getElementById('modal-name').innerText = name; document.getElementById('modal-price').innerText = price; document.getElementById('modal-description').innerText = description; document.getElementById('modal-image').src = image; document.getElementById('modal-price').innerText = price;
const spiceLevelInputs = document.querySelectorAll('input[name="spice-level"]'); spiceLevelInputs.forEach(input => input.checked = false);4 unmodified lines } function closeModal() { document.getElementById('modal').style.display = 'none'; document.body.style.overflow = ''; // Re-enable scrolling } function addToCart() { if (finalized) { alert("You cannot add more items after finalizing your order."); return; } const name = document.getElementById('modal-name').innerText; const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', '')); const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";4 unmodified lines const itemTotal = (price + extrasCost) * quantity; const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal }; cart.push(cartItem); updateCartDisplay(); alert(`${name} added to cart!`); closeModal(); } function updateCartDisplay() { let totalBill = 0; let cartHTML = "<h3>Your Cart:</h3><ul>"; cart.forEach((item, index) => { totalBill += item.itemTotal; const extras = item.extras.join(', '); cartHTML += `<li>${item.name} (x${item.quantity}, Spice: ${item.spiceLevel}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`; }); cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`; document.getElementById('floating-cart').innerHTML = cartHTML; document.getElementById('cart-data').value = JSON.stringify(cart); } function finalizeCart() { document.getElementById('cart-data').value = JSON.stringify(cart); }</script>"""
2 unmodified lines with gr.Blocks() as demo: gr.Markdown("## Dynamic Menu with Preferences")
# Radio button for selecting preference selected_preference = gr.Radio( choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"], value="All", label="Choose a Preference", )
# Output area for menu items menu_output = gr.HTML(value=filter_menu("All"))
# Floating cart display cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
# Hidden input to store cart data cart_data = gr.Textbox(visible=False, elem_id="cart-data")
# Final order display final_order_output = gr.HTML(value="", elem_id="final-page")
# Modal window for item customization modal_window = gr.HTML(""" <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;"> <div style="text-align: right;">35 unmodified lines </div> """)
# Finalize Order Button finalize_button = gr.Button("Finalize Order")
# Finalize order button click event finalize_button.click(finalize_order, inputs=[cart_data], outputs=[final_order_output]) finalize_button.click(lambda: "Order finalized!", outputs=[])
# Update menu dynamically based on preference selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
# Layout gr.Row([selected_preference]) gr.Row(menu_output) gr.Row(cart_output) gr.Row(modal_window) gr.Row(finalize_button) gr.Row(final_order_output) gr.HTML(modal_and_cart_js)
return demoapp.py+17-53