Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,49 @@ import gradio as gr
|
|
| 2 |
|
| 3 |
# Menu data
|
| 4 |
menu_data = [
|
| 5 |
-
{"name": "Veg Burger", "category": "VEGAN", "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veg_burger.jpg", "description": "A delicious vegan burger with plant-based patty, lettuce, and tomato.", "price":
|
| 6 |
-
{"name": "Chicken Biryani", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/chicken_biryani.jpg", "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.", "price":
|
| 7 |
-
# Add other items as needed...
|
| 8 |
]
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
cart.append(dish_name)
|
| 13 |
-
return cart, update_cart(cart)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if not cart:
|
| 18 |
-
return "<p>Your cart is empty.</p>"
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
for item in cart:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Function to filter dishes by category
|
| 26 |
def filter_dishes(category):
|
|
@@ -38,8 +62,8 @@ def display_dishes(category="ALL"):
|
|
| 38 |
<img src='{dish['image']}' alt='{dish['name']}' style='width: 100%; height: 150px; object-fit: cover; border-radius: 5px;'>
|
| 39 |
<h4>{dish['name']}</h4>
|
| 40 |
<p>{dish['description']}</p>
|
| 41 |
-
<p><strong>Price:</strong> {dish['price']}</p>
|
| 42 |
-
<button onclick="
|
| 43 |
</div>
|
| 44 |
"""
|
| 45 |
html_content += "</div>"
|
|
@@ -47,8 +71,6 @@ def display_dishes(category="ALL"):
|
|
| 47 |
|
| 48 |
# Main Gradio App
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
-
cart = gr.State([]) # Use gr.State to manage cart
|
| 51 |
-
|
| 52 |
gr.HTML("<h1 style='text-align: center;'>Dynamic Menu with Cart</h1>")
|
| 53 |
|
| 54 |
with gr.Row():
|
|
@@ -62,70 +84,17 @@ with gr.Blocks() as demo:
|
|
| 62 |
btn_vegan.click(lambda: display_dishes("VEGAN"), outputs=dish_display)
|
| 63 |
btn_halal.click(lambda: display_dishes("HALAL"), outputs=dish_display)
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
inputs=[cart, selected_item],
|
| 72 |
-
outputs=[cart]
|
| 73 |
-
)
|
| 74 |
|
| 75 |
-
gr.
|
| 76 |
-
|
| 77 |
-
<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()'>
|
| 78 |
-
</div>
|
| 79 |
-
<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);'>
|
| 80 |
-
<span onclick='toggleCartPopup()' style='position: absolute; top: 10px; right: 15px; font-size: 20px; font-weight: bold; cursor: pointer;'>×</span>
|
| 81 |
-
<div id='cart-content'>Your cart is empty.</div>
|
| 82 |
-
<button onclick='alert("Order placed!")' style='margin-top: 10px; padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px;'>Place Order</button>
|
| 83 |
-
</div>
|
| 84 |
-
|
| 85 |
-
<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);'>
|
| 86 |
-
<span onclick='closeMenuPopup()' style='position: absolute; top: 5px; right: 10px; cursor: pointer;'>×</span>
|
| 87 |
-
<img id='popup-image' src='' style='width: 100%; border-radius: 5px; margin-bottom: 10px;'>
|
| 88 |
-
<h4 id='popup-title'></h4>
|
| 89 |
-
<p id='popup-description'></p>
|
| 90 |
-
<p id='popup-price'></p>
|
| 91 |
-
<button onclick='addItemToCart()' style='padding: 5px 10px; background-color: #28a745; color: white; border: none; border-radius: 3px;'>Add to Cart</button>
|
| 92 |
-
</div>
|
| 93 |
|
| 94 |
-
|
| 95 |
-
let selectedItem = '';
|
| 96 |
-
|
| 97 |
-
function toggleCartPopup() {
|
| 98 |
-
const popup = document.getElementById('cart-popup');
|
| 99 |
-
popup.style.display = popup.style.display === 'block' ? 'none' : 'block';
|
| 100 |
-
fetch('/run/predict', {
|
| 101 |
-
method: 'POST',
|
| 102 |
-
headers: { 'Content-Type': 'application/json' },
|
| 103 |
-
body: JSON.stringify({ data: [] })
|
| 104 |
-
}).then(response => response.json()).then(data => {
|
| 105 |
-
document.getElementById('cart-content').innerHTML = data.data[1];
|
| 106 |
-
});
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
function showMenuPopup(name, description, price, image) {
|
| 110 |
-
selectedItem = name;
|
| 111 |
-
document.getElementById('popup-title').textContent = name;
|
| 112 |
-
document.getElementById('popup-description').textContent = description;
|
| 113 |
-
document.getElementById('popup-price').textContent = `Price: ${price}`;
|
| 114 |
-
document.getElementById('popup-image').src = image;
|
| 115 |
-
document.getElementById('menu-popup').style.display = 'block';
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
function closeMenuPopup() {
|
| 119 |
-
document.getElementById('menu-popup').style.display = 'none';
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
function addItemToCart() {
|
| 123 |
-
closeMenuPopup();
|
| 124 |
-
const inputBox = gradioApp().querySelector("textarea[aria-label='Selected Item']");
|
| 125 |
-
inputBox.value = selectedItem;
|
| 126 |
-
gradioApp().querySelector("button[aria-label='Add to Cart']").click();
|
| 127 |
-
}
|
| 128 |
-
</script>
|
| 129 |
-
""")
|
| 130 |
-
|
| 131 |
-
demo.launch()
|
|
|
|
| 2 |
|
| 3 |
# Menu data
|
| 4 |
menu_data = [
|
| 5 |
+
{"name": "Veg Burger", "category": "VEGAN", "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veg_burger.jpg", "description": "A delicious vegan burger with plant-based patty, lettuce, and tomato.", "price": 8.99},
|
| 6 |
+
{"name": "Chicken Biryani", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/chicken_biryani.jpg", "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.", "price": 12.99},
|
|
|
|
| 7 |
]
|
| 8 |
|
| 9 |
+
# Cart management
|
| 10 |
+
cart = []
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def add_item_to_cart(item_name, price, extras="None"):
|
| 13 |
+
for item in cart:
|
| 14 |
+
if item['name'] == item_name and item['extras'] == extras:
|
| 15 |
+
item['count'] += 1
|
| 16 |
+
return update_cart()
|
| 17 |
+
cart.append({"name": item_name, "price": price, "extras": extras, "count": 1})
|
| 18 |
+
return update_cart()
|
| 19 |
+
|
| 20 |
+
def remove_item_from_cart(item_name):
|
| 21 |
+
for item in cart:
|
| 22 |
+
if item['name'] == item_name:
|
| 23 |
+
item['count'] -= 1
|
| 24 |
+
if item['count'] == 0:
|
| 25 |
+
cart.remove(item)
|
| 26 |
+
break
|
| 27 |
+
return update_cart()
|
| 28 |
+
|
| 29 |
+
def clear_cart():
|
| 30 |
+
global cart
|
| 31 |
+
cart = []
|
| 32 |
+
return update_cart()
|
| 33 |
+
|
| 34 |
+
def update_cart():
|
| 35 |
if not cart:
|
| 36 |
+
return "<p>Your cart is empty.</p>", "0", "$0.00"
|
| 37 |
+
cart_html = "<table><tr><th>Item</th><th>Extras</th><th>Price</th><th>Count</th><th>Total</th><th>Action</th></tr>"
|
| 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 |
+
total_count += item['count']
|
| 44 |
+
cart_html += f"<tr><td>{item['name']}</td><td>{item['extras']}</td><td>${item['price']}</td><td>{item['count']}</td>"
|
| 45 |
+
cart_html += f"<td>${item_total:.2f}</td><td><button onclick=\"remove_item('{item['name']}')\">Remove</button></td></tr>"
|
| 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):
|
|
|
|
| 62 |
<img src='{dish['image']}' alt='{dish['name']}' style='width: 100%; height: 150px; object-fit: cover; border-radius: 5px;'>
|
| 63 |
<h4>{dish['name']}</h4>
|
| 64 |
<p>{dish['description']}</p>
|
| 65 |
+
<p><strong>Price:</strong> ${dish['price']}</p>
|
| 66 |
+
<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>
|
| 67 |
</div>
|
| 68 |
"""
|
| 69 |
html_content += "</div>"
|
|
|
|
| 71 |
|
| 72 |
# Main Gradio App
|
| 73 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 74 |
gr.HTML("<h1 style='text-align: center;'>Dynamic Menu with Cart</h1>")
|
| 75 |
|
| 76 |
with gr.Row():
|
|
|
|
| 84 |
btn_vegan.click(lambda: display_dishes("VEGAN"), outputs=dish_display)
|
| 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 |
+
add_button = gr.Button("Add to Cart")
|
| 95 |
+
add_button.click(add_to_cart_wrapper, inputs=[gr.Textbox(), gr.Number()], outputs=[cart_display, total_items, total_price])
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
clear_button = gr.Button("Clear Cart")
|
| 98 |
+
clear_button.click(clear_cart, outputs=[cart_display, total_items, total_price])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|