Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
menu_data = [
|
| 5 |
{"name": "Veg Burger", "category": "VEGAN",
|
|
@@ -16,7 +18,20 @@ menu_data = [
|
|
| 16 |
"price": "$10.99"}
|
| 17 |
]
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Function to generate dish cards
|
| 22 |
def display_dishes(category):
|
|
@@ -55,17 +70,19 @@ popup_js = """
|
|
| 55 |
<button id="add-to-cart-button" style="padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">
|
| 56 |
Add to Cart
|
| 57 |
</button>
|
| 58 |
-
<button onclick="closePopup()" style="padding: 10px 20px; background-color: #dc3545; color: white; border: none; border-radius: 5px; cursor: pointer; margin-left: 10px;">
|
| 59 |
-
Close
|
| 60 |
-
</button>
|
| 61 |
`;
|
| 62 |
popup.style.display = "block";
|
| 63 |
overlay.style.display = "block";
|
| 64 |
|
| 65 |
document.getElementById("add-to-cart-button").onclick = async function() {
|
| 66 |
-
const response = await
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
};
|
| 70 |
}
|
| 71 |
|
|
@@ -123,4 +140,7 @@ with gr.Blocks() as demo:
|
|
| 123 |
outputs=cart_display
|
| 124 |
)
|
| 125 |
|
|
|
|
|
|
|
|
|
|
| 126 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Global variables for cart and menu data
|
| 4 |
+
cart = []
|
| 5 |
|
| 6 |
menu_data = [
|
| 7 |
{"name": "Veg Burger", "category": "VEGAN",
|
|
|
|
| 18 |
"price": "$10.99"}
|
| 19 |
]
|
| 20 |
|
| 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} added to cart!"
|
| 25 |
|
| 26 |
+
# Function to display the current cart
|
| 27 |
+
def display_cart():
|
| 28 |
+
if not cart:
|
| 29 |
+
return "Your cart is empty."
|
| 30 |
+
cart_content = "<h3>Your Cart:</h3><ul>"
|
| 31 |
+
for item in cart:
|
| 32 |
+
cart_content += f"<li>{item['item_name']}</li>"
|
| 33 |
+
cart_content += "</ul>"
|
| 34 |
+
return cart_content
|
| 35 |
|
| 36 |
# Function to generate dish cards
|
| 37 |
def display_dishes(category):
|
|
|
|
| 70 |
<button id="add-to-cart-button" style="padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">
|
| 71 |
Add to Cart
|
| 72 |
</button>
|
|
|
|
|
|
|
|
|
|
| 73 |
`;
|
| 74 |
popup.style.display = "block";
|
| 75 |
overlay.style.display = "block";
|
| 76 |
|
| 77 |
document.getElementById("add-to-cart-button").onclick = async function() {
|
| 78 |
+
const response = await fetch('/run/add_to_cart', {
|
| 79 |
+
method: 'POST',
|
| 80 |
+
body: JSON.stringify({ name }),
|
| 81 |
+
headers: { 'Content-Type': 'application/json' }
|
| 82 |
+
});
|
| 83 |
+
const result = await response.json();
|
| 84 |
+
alert(result.data); // Show success message
|
| 85 |
+
closePopup(); // Automatically close popup
|
| 86 |
};
|
| 87 |
}
|
| 88 |
|
|
|
|
| 140 |
outputs=cart_display
|
| 141 |
)
|
| 142 |
|
| 143 |
+
# Register the add_to_cart function
|
| 144 |
+
demo.api("/add_to_cart", add_to_cart)
|
| 145 |
+
|
| 146 |
demo.launch()
|