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
|
| 25 |
|
| 26 |
# Function to display the current cart
|
| 27 |
def display_cart():
|
|
@@ -46,7 +46,7 @@ def display_dishes(category):
|
|
| 46 |
<h3 style='margin: 10px 0; font-size: 18px;'>{dish['name']}</h3>
|
| 47 |
<p style='margin: 5px 0; font-size: 14px; color: #555;'>{dish['description']}</p>
|
| 48 |
<p style='margin: 10px 0; font-size: 16px; font-weight: bold;'>Price: {dish['price']}</p>
|
| 49 |
-
<button onclick="
|
| 50 |
Add to Cart
|
| 51 |
</button>
|
| 52 |
</div>
|
|
@@ -54,70 +54,35 @@ def display_dishes(category):
|
|
| 54 |
html_content += "</div>"
|
| 55 |
return html_content
|
| 56 |
|
| 57 |
-
# JavaScript for Popup
|
| 58 |
popup_js = """
|
| 59 |
<script>
|
| 60 |
-
function
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
popup.style.display = "block";
|
| 74 |
-
overlay.style.display = "block";
|
| 75 |
-
|
| 76 |
-
document.getElementById("add-to-cart-button").onclick = async function () {
|
| 77 |
-
try {
|
| 78 |
-
const response = await fetch("/add_to_cart", {
|
| 79 |
-
method: "POST",
|
| 80 |
-
headers: { "Content-Type": "application/json" },
|
| 81 |
-
body: JSON.stringify({ item_name: name })
|
| 82 |
-
});
|
| 83 |
-
const result = await response.json();
|
| 84 |
-
|
| 85 |
-
if (result.status === "success") {
|
| 86 |
-
alert(result.message); // Show success message
|
| 87 |
-
} else {
|
| 88 |
-
alert("Error: " + result.message); // Show error message
|
| 89 |
-
}
|
| 90 |
-
} catch (error) {
|
| 91 |
-
console.error("Error:", error);
|
| 92 |
-
alert("Something went wrong!");
|
| 93 |
}
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
function closePopup() {
|
| 100 |
-
const popup = document.getElementById("custom-popup");
|
| 101 |
-
const overlay = document.getElementById("overlay");
|
| 102 |
-
popup.style.display = "none";
|
| 103 |
-
overlay.style.display = "none";
|
| 104 |
}
|
| 105 |
</script>
|
| 106 |
"""
|
| 107 |
|
| 108 |
-
# HTML for the Popup Container
|
| 109 |
-
popup_html = """
|
| 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:
|
| 118 |
gr.HTML("<h1 style='text-align: center;'>π Welcome to the Biryani Hub π</h1>")
|
| 119 |
-
gr.HTML(
|
| 120 |
-
gr.HTML(popup_js) # Add JavaScript for handling interactions
|
| 121 |
|
| 122 |
# Tabs for Menu and Cart
|
| 123 |
with gr.Tabs():
|
|
@@ -152,5 +117,12 @@ with gr.Blocks() as demo:
|
|
| 152 |
outputs=cart_display
|
| 153 |
)
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
# Launch Gradio app
|
| 156 |
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 f"'{item_name}' has been added to your cart!"
|
| 25 |
|
| 26 |
# Function to display the current cart
|
| 27 |
def display_cart():
|
|
|
|
| 46 |
<h3 style='margin: 10px 0; font-size: 18px;'>{dish['name']}</h3>
|
| 47 |
<p style='margin: 5px 0; font-size: 14px; color: #555;'>{dish['description']}</p>
|
| 48 |
<p style='margin: 10px 0; font-size: 16px; font-weight: bold;'>Price: {dish['price']}</p>
|
| 49 |
+
<button onclick="addItemToCart('{dish['name']}')" style='padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;'>
|
| 50 |
Add to Cart
|
| 51 |
</button>
|
| 52 |
</div>
|
|
|
|
| 54 |
html_content += "</div>"
|
| 55 |
return html_content
|
| 56 |
|
| 57 |
+
# JavaScript for Popup and Backend Calls
|
| 58 |
popup_js = """
|
| 59 |
<script>
|
| 60 |
+
async function addItemToCart(itemName) {
|
| 61 |
+
try {
|
| 62 |
+
const response = await fetch("/run/add_to_cart", {
|
| 63 |
+
method: "POST",
|
| 64 |
+
headers: { "Content-Type": "application/json" },
|
| 65 |
+
body: JSON.stringify({ item_name: itemName })
|
| 66 |
+
});
|
| 67 |
+
const result = await response.json();
|
| 68 |
+
|
| 69 |
+
if (response.ok) {
|
| 70 |
+
alert(result);
|
| 71 |
+
} else {
|
| 72 |
+
alert("Error adding item to cart!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
+
} catch (error) {
|
| 75 |
+
console.error("Error:", error);
|
| 76 |
+
alert("Something went wrong!");
|
| 77 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
</script>
|
| 80 |
"""
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
# Main Gradio app
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
gr.HTML("<h1 style='text-align: center;'>π Welcome to the Biryani Hub π</h1>")
|
| 85 |
+
gr.HTML(popup_js) # Add JavaScript for handling interactions
|
|
|
|
| 86 |
|
| 87 |
# Tabs for Menu and Cart
|
| 88 |
with gr.Tabs():
|
|
|
|
| 117 |
outputs=cart_display
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# Add-to-Cart Backend Function
|
| 121 |
+
demo.load(
|
| 122 |
+
fn=add_to_cart,
|
| 123 |
+
inputs=gr.Textbox(label="Item Name", visible=False), # Hidden input
|
| 124 |
+
outputs=gr.Textbox(label="Cart Update", visible=False) # Hidden output
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
# Launch Gradio app
|
| 128 |
demo.launch()
|