Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ menu_data = [
|
|
| 21 |
# Function to add an item to the cart
|
| 22 |
def add_to_cart(item_name):
|
| 23 |
cart.append({"item_name": item_name})
|
| 24 |
-
return f"'{item_name}' has been added to your cart!"
|
| 25 |
|
| 26 |
# Function to display the current cart
|
| 27 |
def display_cart():
|
|
@@ -59,7 +59,7 @@ def display_dishes(category):
|
|
| 59 |
# JavaScript for handling the popup
|
| 60 |
popup_js = """
|
| 61 |
<script>
|
| 62 |
-
function showPopup(name, description, price, image) {
|
| 63 |
const popup = document.getElementById("custom-popup");
|
| 64 |
const overlay = document.getElementById("overlay");
|
| 65 |
popup.innerHTML = `
|
|
@@ -77,18 +77,18 @@ popup_js = """
|
|
| 77 |
|
| 78 |
document.getElementById("add-to-cart-button").onclick = async function () {
|
| 79 |
try {
|
| 80 |
-
const response = await fetch("/
|
| 81 |
method: "POST",
|
| 82 |
-
|
| 83 |
-
|
| 84 |
});
|
| 85 |
|
| 86 |
const result = await response.json();
|
| 87 |
|
| 88 |
-
if (
|
| 89 |
-
alert(result);
|
| 90 |
} else {
|
| 91 |
-
alert("Error adding item to cart: " +
|
| 92 |
}
|
| 93 |
} catch (error) {
|
| 94 |
console.error("Error:", error);
|
|
@@ -107,12 +107,11 @@ popup_js = """
|
|
| 107 |
"""
|
| 108 |
|
| 109 |
# HTML structure for the popup
|
| 110 |
-
popup_html = """
|
| 111 |
<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>
|
| 112 |
<div id="custom-popup"
|
| 113 |
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;">
|
| 114 |
</div>
|
| 115 |
-
|
| 116 |
|
| 117 |
# Main Gradio app
|
| 118 |
with gr.Blocks() as demo:
|
|
@@ -153,5 +152,4 @@ with gr.Blocks() as demo:
|
|
| 153 |
outputs=cart_display
|
| 154 |
)
|
| 155 |
|
| 156 |
-
# Register backend functions
|
| 157 |
demo.launch()
|
|
|
|
| 21 |
# Function to add an item to the cart
|
| 22 |
def add_to_cart(item_name):
|
| 23 |
cart.append({"item_name": item_name})
|
| 24 |
+
return {"status": "success", "message": f"'{item_name}' has been added to your cart!"}
|
| 25 |
|
| 26 |
# Function to display the current cart
|
| 27 |
def display_cart():
|
|
|
|
| 59 |
# JavaScript for handling the popup
|
| 60 |
popup_js = """
|
| 61 |
<script>
|
| 62 |
+
async function showPopup(name, description, price, image) {
|
| 63 |
const popup = document.getElementById("custom-popup");
|
| 64 |
const overlay = document.getElementById("overlay");
|
| 65 |
popup.innerHTML = `
|
|
|
|
| 77 |
|
| 78 |
document.getElementById("add-to-cart-button").onclick = async function () {
|
| 79 |
try {
|
| 80 |
+
const response = await fetch("/add_to_cart", {
|
| 81 |
method: "POST",
|
| 82 |
+
headers: { "Content-Type": "application/json" },
|
| 83 |
+
body: JSON.stringify({ item_name: name })
|
| 84 |
});
|
| 85 |
|
| 86 |
const result = await response.json();
|
| 87 |
|
| 88 |
+
if (result.status === "success") {
|
| 89 |
+
alert(result.message); // Show success message
|
| 90 |
} else {
|
| 91 |
+
alert("Error adding item to cart: " + result.message);
|
| 92 |
}
|
| 93 |
} catch (error) {
|
| 94 |
console.error("Error:", error);
|
|
|
|
| 107 |
"""
|
| 108 |
|
| 109 |
# HTML structure for the popup
|
|
|
|
| 110 |
<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>
|
| 111 |
<div id="custom-popup"
|
| 112 |
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;">
|
| 113 |
</div>
|
| 114 |
+
|
| 115 |
|
| 116 |
# Main Gradio app
|
| 117 |
with gr.Blocks() as demo:
|
|
|
|
| 152 |
outputs=cart_display
|
| 153 |
)
|
| 154 |
|
|
|
|
| 155 |
demo.launch()
|