Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,18 +33,15 @@ def clear_cart():
|
|
| 33 |
|
| 34 |
def update_cart():
|
| 35 |
if not cart:
|
| 36 |
-
return "<p>Your cart is empty.</p>"
|
| 37 |
-
cart_html = "<table><tr><th>Item</th><th>Extras</th><th>Price</th><th>Count</th><th>Total</th><
|
| 38 |
total_price = 0
|
| 39 |
-
total_count = 0
|
| 40 |
for item in cart:
|
| 41 |
item_total = item['price'] * item['count']
|
| 42 |
total_price += item_total
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
cart_html += "</table>"
|
| 47 |
-
return cart_html, str(total_count), f"${total_price:.2f}"
|
| 48 |
|
| 49 |
# Function to filter dishes by category
|
| 50 |
def filter_dishes(category):
|
|
@@ -63,7 +60,7 @@ def display_dishes(category="ALL"):
|
|
| 63 |
<h4>{dish['name']}</h4>
|
| 64 |
<p>{dish['description']}</p>
|
| 65 |
<p><strong>Price:</strong> ${dish['price']}</p>
|
| 66 |
-
<button onclick="
|
| 67 |
</div>
|
| 68 |
"""
|
| 69 |
html_content += "</div>"
|
|
@@ -85,32 +82,15 @@ with gr.Blocks() as demo:
|
|
| 85 |
btn_halal.click(lambda: display_dishes("HALAL"), outputs=dish_display)
|
| 86 |
|
| 87 |
cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
|
| 88 |
-
total_items = gr.Textbox(value="0", label="Total Items", interactive=False)
|
| 89 |
-
total_price = gr.Textbox(value="$0.00", label="Total Price", interactive=False)
|
| 90 |
-
|
| 91 |
-
def add_to_cart_wrapper(dish_name, price):
|
| 92 |
-
return add_item_to_cart(dish_name, price)
|
| 93 |
-
|
| 94 |
-
clear_button = gr.Button("Clear Cart")
|
| 95 |
-
clear_button.click(clear_cart, outputs=[cart_display, total_items, total_price])
|
| 96 |
|
| 97 |
gr.HTML("""
|
| 98 |
<div id='cart-icon' style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
|
| 99 |
<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/Shopping_cart_icon.svg/512px-Shopping_cart_icon.svg.png' alt='Cart' style='width: 40px;' onclick='toggleCartPopup()'>
|
| 100 |
</div>
|
| 101 |
-
<div id='cart-popup' style='display: none; position: fixed; top: 50px; right: 10px; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);'>
|
| 102 |
<span onclick='toggleCartPopup()' style='position: absolute; top: 10px; right: 15px; font-size: 20px; font-weight: bold; cursor: pointer;'>×</span>
|
| 103 |
-
<div id='cart-content'>
|
| 104 |
-
<button onclick='
|
| 105 |
-
</div>
|
| 106 |
-
|
| 107 |
-
<div id='menu-popup' style='display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);'>
|
| 108 |
-
<span onclick='closeMenuPopup()' style='position: absolute; top: 5px; right: 10px; cursor: pointer;'>×</span>
|
| 109 |
-
<img id='popup-image' src='' style='width: 100%; border-radius: 5px; margin-bottom: 10px;'>
|
| 110 |
-
<h4 id='popup-title'></h4>
|
| 111 |
-
<p id='popup-description'></p>
|
| 112 |
-
<p id='popup-price'></p>
|
| 113 |
-
<button onclick='addItemToCart()' style='padding: 5px 10px; background-color: #28a745; color: white; border: none; border-radius: 3px;'>Add to Cart</button>
|
| 114 |
</div>
|
| 115 |
|
| 116 |
<script>
|
|
@@ -124,28 +104,23 @@ with gr.Blocks() as demo:
|
|
| 124 |
headers: { 'Content-Type': 'application/json' },
|
| 125 |
body: JSON.stringify({ data: [] })
|
| 126 |
}).then(response => response.json()).then(data => {
|
| 127 |
-
document.getElementById('cart-content').innerHTML = data.data[
|
| 128 |
});
|
| 129 |
}
|
| 130 |
|
| 131 |
-
function
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
function closeMenuPopup() {
|
| 141 |
-
document.getElementById('menu-popup').style.display = 'none';
|
| 142 |
}
|
| 143 |
|
| 144 |
-
function
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
inputBox.value = selectedItem;
|
| 148 |
-
gradioApp().querySelector("button[aria-label='Add to Cart']").click();
|
| 149 |
}
|
| 150 |
</script>
|
| 151 |
""")
|
|
|
|
| 33 |
|
| 34 |
def update_cart():
|
| 35 |
if not cart:
|
| 36 |
+
return "<p>Your cart is empty.</p>"
|
| 37 |
+
cart_html = "<table style='width:100%; border-collapse:collapse;'><tr><th>Item</th><th>Extras</th><th>Price</th><th>Count</th><th>Total</th></tr>"
|
| 38 |
total_price = 0
|
|
|
|
| 39 |
for item in cart:
|
| 40 |
item_total = item['price'] * item['count']
|
| 41 |
total_price += item_total
|
| 42 |
+
cart_html += f"<tr><td>{item['name']}</td><td>{item['extras']}</td><td>${item['price']}</td><td>{item['count']}</td><td>${item_total:.2f}</td></tr>"
|
| 43 |
+
cart_html += f"<tr><td colspan='4' style='text-align:right;'><strong>Total</strong></td><td><strong>${total_price:.2f}</strong></td></tr></table>"
|
| 44 |
+
return cart_html
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Function to filter dishes by category
|
| 47 |
def filter_dishes(category):
|
|
|
|
| 60 |
<h4>{dish['name']}</h4>
|
| 61 |
<p>{dish['description']}</p>
|
| 62 |
<p><strong>Price:</strong> ${dish['price']}</p>
|
| 63 |
+
<button onclick="addItemToCart('{dish['name']}', {dish['price']}, 'None')" style='padding: 5px 10px; background-color: #28a745; color: white; border: none; border-radius: 3px;'>Add to Cart</button>
|
| 64 |
</div>
|
| 65 |
"""
|
| 66 |
html_content += "</div>"
|
|
|
|
| 82 |
btn_halal.click(lambda: display_dishes("HALAL"), outputs=dish_display)
|
| 83 |
|
| 84 |
cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
gr.HTML("""
|
| 87 |
<div id='cart-icon' style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
|
| 88 |
<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/Shopping_cart_icon.svg/512px-Shopping_cart_icon.svg.png' alt='Cart' style='width: 40px;' onclick='toggleCartPopup()'>
|
| 89 |
</div>
|
| 90 |
+
<div id='cart-popup' style='display: none; position: fixed; top: 50px; right: 10px; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); width: 300px;'>
|
| 91 |
<span onclick='toggleCartPopup()' style='position: absolute; top: 10px; right: 15px; font-size: 20px; font-weight: bold; cursor: pointer;'>×</span>
|
| 92 |
+
<div id='cart-content'></div>
|
| 93 |
+
<button onclick='placeOrder()' style='margin-top: 10px; padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; width: 100%;'>Place Order</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
</div>
|
| 95 |
|
| 96 |
<script>
|
|
|
|
| 104 |
headers: { 'Content-Type': 'application/json' },
|
| 105 |
body: JSON.stringify({ data: [] })
|
| 106 |
}).then(response => response.json()).then(data => {
|
| 107 |
+
document.getElementById('cart-content').innerHTML = data.data[0];
|
| 108 |
});
|
| 109 |
}
|
| 110 |
|
| 111 |
+
function addItemToCart(name, price, extras) {
|
| 112 |
+
fetch('/run/predict', {
|
| 113 |
+
method: 'POST',
|
| 114 |
+
headers: { 'Content-Type': 'application/json' },
|
| 115 |
+
body: JSON.stringify({ data: [name, price, extras] })
|
| 116 |
+
}).then(response => response.json()).then(() => {
|
| 117 |
+
alert(`${name} has been added to your cart.`);
|
| 118 |
+
});
|
|
|
|
|
|
|
|
|
|
| 119 |
}
|
| 120 |
|
| 121 |
+
function placeOrder() {
|
| 122 |
+
alert('Your order has been placed!');
|
| 123 |
+
toggleCartPopup();
|
|
|
|
|
|
|
| 124 |
}
|
| 125 |
</script>
|
| 126 |
""")
|