Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Menu data
|
| 4 |
menu_data = [
|
|
@@ -20,15 +22,10 @@ menu_data = [
|
|
| 20 |
{"name": "Chicken Manchurian", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Chicken_manchurian.jpg", "description": "A popular Indo-Chinese dish featuring fried chicken pieces tossed in a tangy and spicy sauce.", "price": 13.99},
|
| 21 |
]
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
{"name": "Extra Salan 8oz", "price": "$2.00"},
|
| 28 |
-
{"name": "Extra Onion", "price": "$1.00"},
|
| 29 |
-
{"name": "Extra Onion & Lemon", "price": "$2.00"},
|
| 30 |
-
{"name": "Extra Fried Onion 4oz", "price": "$2.00"},
|
| 31 |
-
]
|
| 32 |
|
| 33 |
# Filter dishes by category
|
| 34 |
def filter_dishes(category):
|
|
@@ -36,100 +33,90 @@ def filter_dishes(category):
|
|
| 36 |
return menu_data
|
| 37 |
return [dish for dish in menu_data if dish["category"] == category]
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
def
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
<
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
|
| 58 |
-
border-radius: 5px; cursor: pointer;">Add</button>
|
| 59 |
-
</div>
|
| 60 |
</div>
|
| 61 |
"""
|
| 62 |
-
|
| 63 |
-
return
|
| 64 |
|
| 65 |
# Main Gradio App
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
-
gr.HTML("<h1 style='text-align: center;
|
| 68 |
|
| 69 |
-
#
|
| 70 |
with gr.Row():
|
| 71 |
btn_all = gr.Button("ALL")
|
| 72 |
btn_vegan = gr.Button("VEGAN")
|
| 73 |
btn_halal = gr.Button("HALAL")
|
| 74 |
|
| 75 |
-
# Dish display
|
| 76 |
-
dish_display = gr.HTML(
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
<
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
<h4 style="text-align: left; margin-top: 20px;">Choose Extras</h4>
|
| 91 |
-
<div style="display: flex; flex-wrap: wrap; justify-content: space-between; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
|
| 92 |
-
""" + "".join([f'<label style="margin-right: 10px; display: block;"><input type="checkbox" name="extras" value="{extra["name"]}" style="margin-right: 5px;">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """
|
| 93 |
</div>
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
border-radius: 5px; cursor: pointer;">Add to Cart</button>
|
| 98 |
-
<button onclick="closePopup()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745;
|
| 99 |
-
color: white; border: none; border-radius: 5px; cursor: pointer;">Close</button>
|
| 100 |
-
</div>
|
| 101 |
<script>
|
| 102 |
-
function showPopup(
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
function closePopup() {
|
| 112 |
-
document.getElementById(
|
| 113 |
-
}
|
| 114 |
-
function addToCart(dishName, spiceLevel) {
|
| 115 |
-
const selectedExtras = Array.from(document.querySelectorAll('input[name=extras]:checked')).map(cb => cb.value).join(", ");
|
| 116 |
-
alert(dishName + " with " + spiceLevel + " spice level and extras: " + selectedExtras + " has been added to the cart!");
|
| 117 |
}
|
| 118 |
</script>
|
| 119 |
""")
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
btn_all.click(
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from cart import Cart # Import Cart from cart.py
|
| 3 |
+
|
| 4 |
|
| 5 |
# Menu data
|
| 6 |
menu_data = [
|
|
|
|
| 22 |
{"name": "Chicken Manchurian", "category": "HALAL", "image": "https://huggingface.co/spaces/Rammohan0504/First-Project/resolve/main/pictures/Chicken_manchurian.jpg", "description": "A popular Indo-Chinese dish featuring fried chicken pieces tossed in a tangy and spicy sauce.", "price": 13.99},
|
| 23 |
]
|
| 24 |
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Initialize the cart
|
| 28 |
+
cart = Cart()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Filter dishes by category
|
| 31 |
def filter_dishes(category):
|
|
|
|
| 33 |
return menu_data
|
| 34 |
return [dish for dish in menu_data if dish["category"] == category]
|
| 35 |
|
| 36 |
+
# Add item to the cart
|
| 37 |
+
def add_to_cart(item_name):
|
| 38 |
+
item = next((dish for dish in menu_data if dish["name"] == item_name), None)
|
| 39 |
+
if item:
|
| 40 |
+
message = cart.add_item(item)
|
| 41 |
+
return message, cart.view_cart(), f"Total: ${cart.total_price():.2f}"
|
| 42 |
+
return "Item not found!", cart.view_cart(), f"Total: ${cart.total_price():.2f}"
|
| 43 |
+
|
| 44 |
+
# Generate pop-up content for a selected dish
|
| 45 |
+
def generate_popup(dish_name):
|
| 46 |
+
item = next((dish for dish in menu_data if dish["name"] == dish_name), None)
|
| 47 |
+
if item:
|
| 48 |
+
popup_html = f"""
|
| 49 |
+
<div style="text-align: center;">
|
| 50 |
+
<img src="{item['image']}" alt="{item['name']}" style="width: 300px; height: 200px; object-fit: cover; border-radius: 10px;">
|
| 51 |
+
<h2 style="color: #444;">{item['name']}</h2>
|
| 52 |
+
<p style="font-size: 16px; color: #666;">{item['description']}</p>
|
| 53 |
+
<p style="font-size: 18px; color: #444;">Price: ${item['price']}</p>
|
|
|
|
|
|
|
|
|
|
| 54 |
</div>
|
| 55 |
"""
|
| 56 |
+
return popup_html
|
| 57 |
+
return "Dish not found!"
|
| 58 |
|
| 59 |
# Main Gradio App
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
+
gr.HTML("<h1 style='text-align: center;'>π Biryani Hub Menu π</h1>")
|
| 62 |
|
| 63 |
+
# Category buttons
|
| 64 |
with gr.Row():
|
| 65 |
btn_all = gr.Button("ALL")
|
| 66 |
btn_vegan = gr.Button("VEGAN")
|
| 67 |
btn_halal = gr.Button("HALAL")
|
| 68 |
|
| 69 |
+
# Dish display and cart
|
| 70 |
+
dish_display = gr.HTML()
|
| 71 |
+
popup_display = gr.HTML()
|
| 72 |
+
cart_display = gr.HTML("Cart is empty.")
|
| 73 |
+
cart_total = gr.Textbox(value="Total: $0.00", interactive=False)
|
| 74 |
|
| 75 |
+
# Event to update dishes based on category
|
| 76 |
+
def display_dishes(category):
|
| 77 |
+
filtered_dishes = filter_dishes(category)
|
| 78 |
+
html_content = "<div style='display: flex; flex-wrap: wrap; gap: 20px; justify-content: center;'>"
|
| 79 |
+
for dish in filtered_dishes:
|
| 80 |
+
html_content += f"""
|
| 81 |
+
<div style="width: 200px; border: 1px solid #ddd; border-radius: 10px; padding: 10px; text-align: center; background-color: #f9f9f9;">
|
| 82 |
+
<img src="{dish['image']}" alt="{dish['name']}" style="width: 100%; height: 150px; object-fit: cover; border-radius: 10px;">
|
| 83 |
+
<h4 style="margin: 10px 0; color: #444;">{dish['name']}</h4>
|
| 84 |
+
<button onclick="showPopup('{dish['name']}')" style="padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">View</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
</div>
|
| 86 |
+
"""
|
| 87 |
+
html_content += "</div>"
|
| 88 |
+
return html_content
|
| 89 |
|
| 90 |
+
# JavaScript for managing the pop-up
|
| 91 |
+
gr.HTML("""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
<script>
|
| 93 |
+
function showPopup(dishName) {
|
| 94 |
+
fetch("/generate_popup", {
|
| 95 |
+
method: "POST",
|
| 96 |
+
headers: { "Content-Type": "application/json" },
|
| 97 |
+
body: JSON.stringify({ name: dishName })
|
| 98 |
+
})
|
| 99 |
+
.then(response => response.json())
|
| 100 |
+
.then(data => {
|
| 101 |
+
document.getElementById("popup").innerHTML = data.html;
|
| 102 |
+
document.getElementById("popup").style.display = "block";
|
| 103 |
+
});
|
| 104 |
}
|
| 105 |
function closePopup() {
|
| 106 |
+
document.getElementById("popup").style.display = "none";
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
}
|
| 108 |
</script>
|
| 109 |
""")
|
| 110 |
|
| 111 |
+
# Dish display logic
|
| 112 |
+
btn_all.click(lambda: display_dishes("ALL"), outputs=dish_display)
|
| 113 |
+
btn_vegan.click(lambda: display_dishes("VEGAN"), outputs=dish_display)
|
| 114 |
+
btn_halal.click(lambda: display_dishes("HALAL"), outputs=dish_display)
|
| 115 |
+
|
| 116 |
+
# Cart display
|
| 117 |
+
gr.Button("View Cart").click(lambda: cart.view_cart(), outputs=cart_display)
|
| 118 |
+
|
| 119 |
+
# Placeholder for pop-up
|
| 120 |
+
gr.HTML(id="popup", value="", visible=False)
|
| 121 |
+
|
| 122 |
+
demo.launch()
|
|
|
|
|
|
|
|
|