Spaces:
Build error
Build error
Update app.py
Browse files12345626 unmodified lines333435363738394041424344454647484950515253545556575859607 unmodified lines6869707172737475767778794 unmodified lines84858687888990914 unmodified lines969798991001011021031041051061071081091101111121131141151161171181192 unmodified lines12212312412512612712812913013113213313413513613713813914014114214314414514614735 unmodified lines183184185186187188189190191192193194195196197198199200201202203204import 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></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 = 0final_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};function openModal(name, price, description, image) {document.getElementById('modal').style.display = 'block';document.getElementById('modal-name').innerText = name;document.getElementById('modal-price').innerText = price;document.getElementById('modal-description').innerText = description;document.getElementById('modal-image').src = image;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';}function addToCart() {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 linesconst itemTotal = (price + extrasCost) * quantity;const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal };cart.push(cartItem);updateCartDisplay();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 lineswith gr.Blocks() as demo:gr.Markdown("## Dynamic Menu with Preferences")# Radio button for selecting preferenceselected_preference = gr.Radio(choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],value="All",label="Choose a Preference",)# Output area for menu itemsmenu_output = gr.HTML(value=filter_menu("All"))# Floating cart displaycart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")# Hidden input to store cart datacart_data = gr.Textbox(visible=False, elem_id="cart-data")# Final order displayfinal_order_output = gr.HTML(value="", elem_id="final-page")# Modal window for item customizationmodal_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 Buttonfinalize_button = gr.Button("Finalize Order")# Finalize order button click eventfinalize_button.click(finalize_order, inputs=[cart_data], outputs=[final_order_output])# Update menu dynamically based on preferenceselected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])# Layoutgr.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 demo1234526 unmodified lines3233343536373839404142437 unmodified lines5152535455565758596061626364656667684 unmodified lines737475767778798081828384854 unmodified lines9091929394959697982 unmodified lines10110210310410510610710810911011111211311411511635 unmodified lines152153154155156157158159160161162163164165166167168import gradio as grimport pandas as pd# 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['Image URL']}', '{item['Description']}', '{item['Price ($)']}')\">Add</button></div></div>"""return html_content# 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, 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 scrollingdocument.getElementById('modal-image').src = image;document.getElementById('modal-name').innerText = name;document.getElementById('modal-description').innerText = description;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 linesconst itemTotal = (price + extrasCost) * quantity;const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal };cart.push(cartItem);alert(`${name} added to cart!`);closeModal();}</script>"""2 unmodified lineswith gr.Blocks() as demo:gr.Markdown("## Dynamic Menu with Preferences")selected_preference = gr.Radio(choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],value="All",label="Choose a Preference",)menu_output = gr.HTML(value=filter_menu("All"))cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")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_button = gr.Button("Finalize Order")finalize_button.click(lambda: "Order finalized!", outputs=[])selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])gr.Row([selected_preference])gr.Row(menu_output)gr.Row(cart_output)gr.Row(modal_window)gr.Row(finalize_button)gr.HTML(modal_and_cart_js)return demo
app.py+17-53