Spaces:
Build error
Build error
Update app.py
Browse files78 unmodified lines79808182838485868788899091929394959697989910010110210310410510610710810911012 unmodified lines12312412510010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616712612712816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820912913013121121213213313413513613713821821913914014114214322314414522514678 unmodified lines cart_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>" return cart_html
# Function to submit the cart and display on a new pagedef submit_cart(): if len(cart_items) == 0: return "<h3>Your Order</h3><p>Your cart is empty.</p>"
total_bill = 0 order_html = "<h3>Your Order</h3><ul style='list-style-type: none; padding: 0;'>" for item in cart_items: extras = ", ".join(item.get("extras", [])) extras_cost = sum(EXTRAS_PRICES.get(extra, 0) for extra in item.get("extras", [])) item_price = float(item['price'].strip('$')) item_total = (item_price + extras_cost) * item['quantity'] total_bill += item_total
order_html += f"<li style='margin-bottom: 20px; border: 1px solid #ddd; padding: 10px; border-radius: 8px;'>" order_html += f"<strong>Item:</strong> {item['name']} - ${item_price:.2f}<br>" order_html += f"<strong>Quantity x Price:</strong> {item['quantity']} x ${item_price:.2f} = ${item_price * item['quantity']:.2f}<br>" order_html += f"<strong>Spice Level:</strong> {item['spiceLevel']}<br>" order_html += f"<strong>Extras:</strong> {extras} - ${extras_cost:.2f}<br>" order_html += f"<strong>Instructions:</strong> {item['instructions']}<br>" order_html += f"<strong>Item Total:</strong> ${item_total:.2f}" order_html += "</li>"
order_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>" return order_html
# Gradio app definitiondef app(): with gr.Blocks() as demo:12 unmodified lines # Floating cart display cart_output = gr.HTML(value=update_cart(), elem_id="floating-cart")
# JavaScript for modal and cart behavior modal_and_cart_js = """ <script> let cart = []; const extrasPrices = { "Extra Raitha 4oz": 1, "Extra Raitha 8oz": 2, "Extra Salan 4oz": 1, "Extra Salan 8oz": 2, "Extra Onion": 1, "Extra Onion & Lemon": 2, "Extra Fried Onion 4oz": 2 };
function openModal(name, image, description, price) { document.getElementById('modal').style.display = 'block'; document.getElementById('modal-image').src = image; document.getElementById('modal-name').innerText = name; document.getElementById('modal-description').innerText = description; document.getElementById('modal-price').innerText = price; // Reset spice levels and extras const spiceLevelInputs = document.querySelectorAll('input[name="spice-level"]'); spiceLevelInputs.forEach(input => input.checked = false);
const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]'); extrasInputs.forEach(input => input.checked = false);
document.getElementById('quantity').value = 1; document.getElementById('special-instructions').value = ''; }
function closeModal() { document.getElementById('modal').style.display = 'none'; }
function addToCart() { const name = document.getElementById('modal-name').innerText; const price = document.getElementById('modal-price').innerText; const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected"; const quantity = parseInt(document.getElementById('quantity').value) || 1; const instructions = document.getElementById('special-instructions').value; const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0); const itemTotal = (parseFloat(price.replace('$', '')) + extrasCost) * quantity;
const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal }; cart.push(cartItem);
alert(${name} added to cart!); updateCartDisplay(); closeModal(); }
function updateCartDisplay() { let totalBill = 0; let cartHTML = "<h3>Your Cart:</h3><ul>"; cart.forEach(item => { 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; } </script> """ # Submit button for the cart submit_button = gr.Button("Submit Order")
# Modal window modal_window = gr.HTML(""" <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;"> <div style="text-align: right;"> <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">×</button> </div> <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" /> <h2 id="modal-name"></h2> <p id="modal-description"></p> <p id="modal-price"></p> <!-- Spice Levels --> <label for="spice-level">Choose a Spice Level (Required):</label> <div id="spice-level-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;"> <label><input type="radio" name="spice-level" value="American Mild" /> American Mild</label> <label><input type="radio" name="spice-level" value="American Medium" /> American Medium</label> <label><input type="radio" name="spice-level" value="American Spicy" /> American Spicy</label> <label><input type="radio" name="spice-level" value="Indian Mild" /> Indian Mild</label> <label><input type="radio" name="spice-level" value="Indian Medium" /> Indian Medium</label> <label><input type="radio" name="spice-level" value="Indian Very Spicy" /> Indian Very Spicy</label> </div> <!-- Biryani Extras --> <label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label> <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;"> <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 4oz" /> Extra Raitha 4oz + $1.00</label> <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 8oz" /> Extra Raitha 8oz + $2.00</label> <label><input type="checkbox" name="biryani-extra" value="Extra Salan 4oz" /> Extra Salan 4oz + $1.00</label> <label><input type="checkbox" name="biryani-extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label> <label><input type="checkbox" name="biryani-extra" value="Extra Onion" /> Extra Onion + $1.00</label> <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label> <label><input type="checkbox" name="biryani-extra" value="Extra Fried Onion 4oz" /> Extra Fried Onion 4oz + $2.00</label> </div> <!-- Quantity and Special Instructions --> <label for="quantity">Quantity:</label> <input type="number" id="quantity" value="1" min="1" style="width: 50px;" /> <br><br> <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea> <br><br> <!-- Add to Cart Button --> <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> </div> """) # Output for the new page (order details) order_output = gr.HTML()
# Interactivity selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output]) # Submit button action to display the order details on a new page submit_button.click(fn=submit_cart, inputs=[], outputs=order_output)
# Layout gr.Row([selected_preference]) gr.Row(menu_output) gr.Row(cart_output) gr.Row(modal_window) gr.HTML(modal_and_cart_js) gr.Row([submit_button]) gr.Row(order_output)
return demo
if _name_ == "_main_":if __name__ == "__main__": demo = app() demo.launch() demo.launch()app.py+36-115