Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,51 +10,32 @@ menu_data = [
|
|
| 10 |
# Cart data (persistent across sessions)
|
| 11 |
cart_items = []
|
| 12 |
|
| 13 |
-
# Extras
|
| 14 |
-
extras = [
|
| 15 |
-
{"name": "Coke 500ml", "price": "$2.00"},
|
| 16 |
-
{"name": "Sprite 500ml", "price": "$2.00"},
|
| 17 |
-
{"name": "Extra Raitha 4oz", "price": "$1.00"},
|
| 18 |
-
{"name": "Extra Salan 8oz", "price": "$2.00"},
|
| 19 |
-
]
|
| 20 |
-
|
| 21 |
# Function to handle adding items to the cart
|
| 22 |
-
def add_to_cart(dish_name
|
| 23 |
-
cart_items.append(
|
| 24 |
-
|
| 25 |
-
"extras": selected_extras,
|
| 26 |
-
"quantity": quantity,
|
| 27 |
-
"custom_description": custom_description
|
| 28 |
-
})
|
| 29 |
-
return "Your item has been added to the cart!"
|
| 30 |
|
| 31 |
-
# Function to
|
| 32 |
-
def
|
| 33 |
-
if
|
| 34 |
-
return
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Function to display dishes
|
| 38 |
-
def display_dishes(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
for dish in filtered_dishes:
|
| 42 |
html_content += f"""
|
| 43 |
-
<div style='
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
<
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
<p style='font-size: 16px; color: #444; margin: 0;'>Price: {dish['price']}</p>
|
| 50 |
-
</div>
|
| 51 |
-
<div style='text-align: center;'>
|
| 52 |
-
<img src='{dish['image']}' alt='{dish['name']}'
|
| 53 |
-
style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'>
|
| 54 |
-
<button onclick="showMenuPopup('{dish['name']}', '{dish['description']}', '{dish['price']}', '{dish['image']}')"
|
| 55 |
-
style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
|
| 56 |
-
border-radius: 5px; cursor: pointer;">Add</button>
|
| 57 |
-
</div>
|
| 58 |
</div>
|
| 59 |
"""
|
| 60 |
html_content += "</div>"
|
|
@@ -62,31 +43,42 @@ def display_dishes(category):
|
|
| 62 |
|
| 63 |
# Main Gradio App
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
-
gr.HTML("<h1 style='text-align: center;
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
gr.HTML("""
|
| 75 |
-
<div id=
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
<
|
| 79 |
-
<
|
| 80 |
-
<
|
| 81 |
-
<
|
| 82 |
-
<p id="popup-price" style="font-size: 16px; color: #444;"></p>
|
| 83 |
-
<button onclick="addToCartFromPopup()" style="margin-top: 10px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">Add to Cart</button>
|
| 84 |
</div>
|
| 85 |
<script>
|
|
|
|
|
|
|
| 86 |
function showMenuPopup(name, description, price, image) {
|
|
|
|
| 87 |
document.getElementById('popup-title').textContent = name;
|
| 88 |
document.getElementById('popup-description').textContent = description;
|
| 89 |
-
document.getElementById('popup-price').textContent =
|
| 90 |
document.getElementById('popup-image').src = image;
|
| 91 |
document.getElementById('menu-popup').style.display = 'block';
|
| 92 |
}
|
|
@@ -95,17 +87,23 @@ with gr.Blocks() as demo:
|
|
| 95 |
document.getElementById('menu-popup').style.display = 'none';
|
| 96 |
}
|
| 97 |
|
| 98 |
-
function
|
| 99 |
-
// Add your integration with Gradio's backend here if necessary.
|
| 100 |
-
alert('Item added to cart!');
|
| 101 |
closeMenuPopup();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
}
|
| 103 |
</script>
|
| 104 |
""")
|
| 105 |
|
| 106 |
-
|
| 107 |
-
btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
|
| 108 |
-
btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
|
| 109 |
-
btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
|
| 110 |
|
| 111 |
demo.launch()
|
|
|
|
| 10 |
# Cart data (persistent across sessions)
|
| 11 |
cart_items = []
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Function to handle adding items to the cart
|
| 14 |
+
def add_to_cart(dish_name):
|
| 15 |
+
cart_items.append(dish_name)
|
| 16 |
+
return update_cart()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Function to update the cart display
|
| 19 |
+
def update_cart():
|
| 20 |
+
if not cart_items:
|
| 21 |
+
return "<p>Your cart is empty.</p>"
|
| 22 |
+
html_content = "<h3>Cart Items</h3><ul>"
|
| 23 |
+
for item in cart_items:
|
| 24 |
+
html_content += f"<li>{item}</li>"
|
| 25 |
+
html_content += "</ul><button onclick='alert(\'Order placed!\')' style='padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px;'>Place Order</button>"
|
| 26 |
+
return html_content
|
| 27 |
|
| 28 |
# Function to display dishes
|
| 29 |
+
def display_dishes():
|
| 30 |
+
html_content = "<div style='display: flex; flex-wrap: wrap; gap: 10px;'>"
|
| 31 |
+
for dish in menu_data:
|
|
|
|
| 32 |
html_content += f"""
|
| 33 |
+
<div style='border: 1px solid #ddd; padding: 10px; border-radius: 5px; width: 200px;'>
|
| 34 |
+
<img src='{dish['image']}' alt='{dish['name']}' style='width: 100%; height: 150px; object-fit: cover; border-radius: 5px;'>
|
| 35 |
+
<h4>{dish['name']}</h4>
|
| 36 |
+
<p>{dish['description']}</p>
|
| 37 |
+
<p><strong>Price:</strong> {dish['price']}</p>
|
| 38 |
+
<button onclick="showMenuPopup('{dish['name']}', '{dish['description']}', '{dish['price']}', '{dish['image']}')" style='padding: 5px 10px; background-color: #28a745; color: white; border: none; border-radius: 3px;'>Add to Cart</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
</div>
|
| 40 |
"""
|
| 41 |
html_content += "</div>"
|
|
|
|
| 43 |
|
| 44 |
# Main Gradio App
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
+
gr.HTML("<h1 style='text-align: center;'>Dynamic Menu with Cart</h1>")
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
+
gr.HTML("""
|
| 50 |
+
<div id='cart-icon' style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
|
| 51 |
+
<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()'>
|
| 52 |
+
</div>
|
| 53 |
+
<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);'>
|
| 54 |
+
<div id='cart-content'>{update_cart()}</div>
|
| 55 |
+
<button onclick='toggleCartPopup()' style='margin-top: 10px; padding: 5px 10px; background-color: #007bff; color: white; border: none; border-radius: 3px;'>Close</button>
|
| 56 |
+
</div>
|
| 57 |
+
<script>
|
| 58 |
+
function toggleCartPopup() {
|
| 59 |
+
const popup = document.getElementById('cart-popup');
|
| 60 |
+
popup.style.display = popup.style.display === 'block' ? 'none' : 'block';
|
| 61 |
+
}
|
| 62 |
+
</script>
|
| 63 |
+
""")
|
| 64 |
|
| 65 |
gr.HTML("""
|
| 66 |
+
<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);'>
|
| 67 |
+
<span onclick='closeMenuPopup()' style='position: absolute; top: 5px; right: 10px; cursor: pointer;'>×</span>
|
| 68 |
+
<img id='popup-image' src='' style='width: 100%; border-radius: 5px; margin-bottom: 10px;'>
|
| 69 |
+
<h4 id='popup-title'></h4>
|
| 70 |
+
<p id='popup-description'></p>
|
| 71 |
+
<p id='popup-price'></p>
|
| 72 |
+
<button onclick='addItemToCart()' style='padding: 5px 10px; background-color: #28a745; color: white; border: none; border-radius: 3px;'>Add to Cart</button>
|
|
|
|
|
|
|
| 73 |
</div>
|
| 74 |
<script>
|
| 75 |
+
let selectedItem = '';
|
| 76 |
+
|
| 77 |
function showMenuPopup(name, description, price, image) {
|
| 78 |
+
selectedItem = name;
|
| 79 |
document.getElementById('popup-title').textContent = name;
|
| 80 |
document.getElementById('popup-description').textContent = description;
|
| 81 |
+
document.getElementById('popup-price').textContent = price;
|
| 82 |
document.getElementById('popup-image').src = image;
|
| 83 |
document.getElementById('menu-popup').style.display = 'block';
|
| 84 |
}
|
|
|
|
| 87 |
document.getElementById('menu-popup').style.display = 'none';
|
| 88 |
}
|
| 89 |
|
| 90 |
+
function addItemToCart() {
|
|
|
|
|
|
|
| 91 |
closeMenuPopup();
|
| 92 |
+
toggleCartPopup();
|
| 93 |
+
alert('Item added to cart!');
|
| 94 |
+
fetch('/run/predict', {
|
| 95 |
+
method: 'POST',
|
| 96 |
+
headers: { 'Content-Type': 'application/json' },
|
| 97 |
+
body: JSON.stringify({ data: [selectedItem] })
|
| 98 |
+
})
|
| 99 |
+
.then(response => response.json())
|
| 100 |
+
.then(data => {
|
| 101 |
+
document.getElementById('cart-content').innerHTML = data.data[0];
|
| 102 |
+
});
|
| 103 |
}
|
| 104 |
</script>
|
| 105 |
""")
|
| 106 |
|
| 107 |
+
gr.HTML(value=display_dishes())
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
demo.launch()
|