Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,147 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from components.menu import render_menu
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
gr.Markdown("# Welcome to Our Restaurant!")
|
| 7 |
-
gr.Markdown("### Explore the best Indian and Chinese food!")
|
| 8 |
-
render_menu(app)
|
| 9 |
-
app.launch()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
# Global variables for cart and menu data
|
| 4 |
+
cart = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
menu_data = [
|
| 7 |
+
{"name": "Veg Burger", "category": "VEGAN",
|
| 8 |
+
"image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veg_burger.jpg",
|
| 9 |
+
"description": "A delicious vegan burger with plant-based patty, lettuce, and tomato.",
|
| 10 |
+
"price": "$8.99"},
|
| 11 |
+
{"name": "Chicken Biryani", "category": "HALAL",
|
| 12 |
+
"image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/chicken_biryani.jpg",
|
| 13 |
+
"description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.",
|
| 14 |
+
"price": "$12.99"},
|
| 15 |
+
{"name": "Paneer Butter Masala", "category": "VEGAN",
|
| 16 |
+
"image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/paneer_butter_masala.jpg",
|
| 17 |
+
"description": "Paneer cooked in a rich and creamy tomato-based gravy.",
|
| 18 |
+
"price": "$10.99"}
|
| 19 |
+
]
|
| 20 |
+
|
| 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 {"data": f"{item_name} added to cart!"}
|
| 25 |
+
|
| 26 |
+
# Function to display the current cart
|
| 27 |
+
def display_cart():
|
| 28 |
+
if not cart:
|
| 29 |
+
return "Your cart is empty."
|
| 30 |
+
cart_content = "<h3>Your Cart:</h3><ul>"
|
| 31 |
+
for item in cart:
|
| 32 |
+
cart_content += f"<li>{item['item_name']}</li>"
|
| 33 |
+
cart_content += "</ul>"
|
| 34 |
+
return cart_content
|
| 35 |
+
|
| 36 |
+
# Function to generate dish cards
|
| 37 |
+
def display_dishes(category):
|
| 38 |
+
html_content = "<div style='display: flex; flex-wrap: wrap; gap: 20px; justify-content: center;'>"
|
| 39 |
+
for dish in menu_data:
|
| 40 |
+
if category == "ALL" or dish["category"] == category:
|
| 41 |
+
html_content += f"""
|
| 42 |
+
<div style='flex: 1 1 calc(30% - 20px); display: flex; flex-direction: column; align-items: center;
|
| 43 |
+
justify-content: space-between; padding: 20px; border: 1px solid #ddd; border-radius: 10px;
|
| 44 |
+
background-color: #f9f9f9; box-shadow: 0 4px 8px rgba(0,0,0,0.1);'>
|
| 45 |
+
<img src='{dish['image']}' alt='{dish['name']}' style='width: 100%; height: 150px; object-fit: cover; border-radius: 10px;'>
|
| 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 style='padding: 10px 20px; background-color: #28a745; color: white; border: none;
|
| 50 |
+
border-radius: 5px; cursor: pointer;'
|
| 51 |
+
onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}', '{dish['image']}')">
|
| 52 |
+
Add to Cart
|
| 53 |
+
</button>
|
| 54 |
+
</div>
|
| 55 |
+
"""
|
| 56 |
+
html_content += "</div>"
|
| 57 |
+
return html_content
|
| 58 |
+
|
| 59 |
+
# Popup JavaScript for handling the modal
|
| 60 |
+
popup_js = """
|
| 61 |
+
<script>
|
| 62 |
+
function showPopup(name, description, price, image) {
|
| 63 |
+
const popup = document.getElementById("custom-popup");
|
| 64 |
+
const overlay = document.getElementById("overlay");
|
| 65 |
+
popup.innerHTML = `
|
| 66 |
+
<button onclick="closePopup()" style="position: absolute; top: 10px; right: 10px; background: none; border: none; font-size: 20px; cursor: pointer;">×</button>
|
| 67 |
+
<img src="${image}" alt="${name}" style="width: 100%; height: 200px; object-fit: cover; border-radius: 10px; margin-bottom: 10px;">
|
| 68 |
+
<h3>${name}</h3>
|
| 69 |
+
<p>${description}</p>
|
| 70 |
+
<p style="font-weight: bold;">Price: ${price}</p>
|
| 71 |
+
<button id="add-to-cart-button" style="padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">
|
| 72 |
+
Add to Cart
|
| 73 |
+
</button>
|
| 74 |
+
`;
|
| 75 |
+
popup.style.display = "block";
|
| 76 |
+
overlay.style.display = "block";
|
| 77 |
+
|
| 78 |
+
document.getElementById("add-to-cart-button").onclick = async function() {
|
| 79 |
+
const response = await fetch("/run/add_to_cart", {
|
| 80 |
+
method: "POST",
|
| 81 |
+
body: JSON.stringify({ name }),
|
| 82 |
+
headers: { "Content-Type": "application/json" }
|
| 83 |
+
});
|
| 84 |
+
const result = await response.json();
|
| 85 |
+
alert(result.data); // Show success message
|
| 86 |
+
closePopup(); // Automatically close popup
|
| 87 |
+
};
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
function closePopup() {
|
| 91 |
+
document.getElementById("custom-popup").style.display = "none";
|
| 92 |
+
document.getElementById("overlay").style.display = "none";
|
| 93 |
+
}
|
| 94 |
+
</script>
|
| 95 |
+
"""
|
| 96 |
+
|
| 97 |
+
# Popup HTML structure
|
| 98 |
+
popup_html = """
|
| 99 |
+
<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>
|
| 100 |
+
<div id="custom-popup"
|
| 101 |
+
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;">
|
| 102 |
+
</div>
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
# Main Gradio App
|
| 106 |
+
with gr.Blocks() as demo:
|
| 107 |
+
gr.HTML(popup_html) # Add popup container
|
| 108 |
+
gr.HTML(popup_js) # Add popup JavaScript
|
| 109 |
+
|
| 110 |
+
gr.HTML("<h1 style='text-align: center;'>π Welcome to the Biryani Hub π</h1>")
|
| 111 |
+
|
| 112 |
+
with gr.Tabs():
|
| 113 |
+
# Menu Tab
|
| 114 |
+
with gr.Tab("Menu"):
|
| 115 |
+
with gr.Row():
|
| 116 |
+
btn_all = gr.Button("ALL")
|
| 117 |
+
btn_vegan = gr.Button("VEGAN")
|
| 118 |
+
btn_halal = gr.Button("HALAL")
|
| 119 |
+
|
| 120 |
+
dish_display = gr.HTML(value=display_dishes("ALL"))
|
| 121 |
+
|
| 122 |
+
btn_all.click(
|
| 123 |
+
lambda: display_dishes("ALL"),
|
| 124 |
+
outputs=dish_display
|
| 125 |
+
)
|
| 126 |
+
btn_vegan.click(
|
| 127 |
+
lambda: display_dishes("VEGAN"),
|
| 128 |
+
outputs=dish_display
|
| 129 |
+
)
|
| 130 |
+
btn_halal.click(
|
| 131 |
+
lambda: display_dishes("HALAL"),
|
| 132 |
+
outputs=dish_display
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Cart Tab
|
| 136 |
+
with gr.Tab("Cart"):
|
| 137 |
+
cart_display = gr.HTML(value=display_cart())
|
| 138 |
+
refresh_cart_btn = gr.Button("Refresh Cart")
|
| 139 |
+
refresh_cart_btn.click(
|
| 140 |
+
lambda: display_cart(),
|
| 141 |
+
outputs=cart_display
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Register the add_to_cart function
|
| 145 |
+
demo.add_to_cart = add_to_cart
|
| 146 |
+
|
| 147 |
+
demo.launch()
|