Spaces:
Sleeping
Sleeping
Update app.py
Browse files20 unmodified lines2122232425262731 unmodified lines5960616263646511 unmodified lines77787980818283848586878889909192939412 unmodified lines10710810911011111211311411511611711834 unmodified lines15315415515615720 unmodified lines# Function to add an item to the cartdef add_to_cart(item_name):cart.append({"item_name": item_name})return f"'{item_name}' has been added to your cart!"# Function to display the current cartdef display_cart():31 unmodified lines# JavaScript for handling the popuppopup_js = """<script>function showPopup(name, description, price, image) {const popup = document.getElementById("custom-popup");const overlay = document.getElementById("overlay");popup.innerHTML = `11 unmodified linesdocument.getElementById("add-to-cart-button").onclick = async function () {try {const response = await fetch("/run/add_to_cart", {method: "POST",body: JSON.stringify({ item_name: name }),headers: { "Content-Type": "application/json" }});const result = await response.json();if (response.ok) {alert(result); // Show success message} else {alert("Error adding item to cart: " + (result.error || "Unknown error"));}} catch (error) {console.error("Error:", error);12 unmodified lines"""# HTML structure for the popuppopup_html = """<div id="overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; z-index: 999;"></div><div id="custom-popup"style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 400px; max-width: 90%; background: white; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 6px rgba(0,0,0,0.1); display: none; z-index: 1000; position: relative;"></div>"""# Main Gradio appwith gr.Blocks() as demo:34 unmodified linesoutputs=cart_display)# Register backend functionsdemo.launch()20 unmodified lines2122232425262731 unmodified lines5960616263646511 unmodified lines77787980818283848586878889909192939412 unmodified lines10710810911011111211311411511611734 unmodified lines15215315415520 unmodified lines# Function to add an item to the cartdef add_to_cart(item_name):cart.append({"item_name": item_name})return {"status": "success", "message": f"'{item_name}' has been added to your cart!"}# Function to display the current cartdef display_cart():31 unmodified lines# JavaScript for handling the popuppopup_js = """<script>async function showPopup(name, description, price, image) {const popup = document.getElementById("custom-popup");const overlay = document.getElementById("overlay");popup.innerHTML = `11 unmodified linesdocument.getElementById("add-to-cart-button").onclick = async function () {try {const response = await fetch("/add_to_cart", {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({ item_name: name })});const result = await response.json();if (result.status === "success") {alert(result.message); // Show success message} else {alert("Error adding item to cart: " + result.message);}} catch (error) {console.error("Error:", error);12 unmodified lines"""# HTML structure for the popup<div id="overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; z-index: 999;"></div><div id="custom-popup"style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 400px; max-width: 90%; background: white; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 6px rgba(0,0,0,0.1); display: none; z-index: 1000; position: relative;"></div># Main Gradio appwith gr.Blocks() as demo:34 unmodified linesoutputs=cart_display)demo.launch()
app.py+9-11