Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -68,17 +68,8 @@ menu_data = [
|
|
| 68 |
"price": "$6.99"}
|
| 69 |
]
|
| 70 |
|
| 71 |
-
# Cart data (persistent across sessions)
|
| 72 |
cart_items = []
|
| 73 |
|
| 74 |
-
# Extras
|
| 75 |
-
extras = [
|
| 76 |
-
{"name": "Coke 500ml", "price": "$2.00"},
|
| 77 |
-
{"name": "Sprite 500ml", "price": "$2.00"},
|
| 78 |
-
{"name": "Extra Raitha 4oz", "price": "$1.00"},
|
| 79 |
-
{"name": "Extra Salan 8oz", "price": "$2.00"},
|
| 80 |
-
]
|
| 81 |
-
|
| 82 |
# Filter dishes by category
|
| 83 |
def filter_dishes(category):
|
| 84 |
if category == "ALL":
|
|
@@ -88,167 +79,63 @@ def filter_dishes(category):
|
|
| 88 |
# Generate dish cards with "Add" button
|
| 89 |
def display_dishes(category):
|
| 90 |
filtered_dishes = filter_dishes(category)
|
| 91 |
-
html_content = "<div style='display: flex; flex-
|
| 92 |
for dish in filtered_dishes:
|
| 93 |
html_content += f"""
|
| 94 |
-
<div style='margin: 10px;
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
<
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
</div>
|
| 102 |
-
<div style='text-align: center;'>
|
| 103 |
-
<img src='{dish['image']}' alt='{dish['name']}'
|
| 104 |
-
style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'>
|
| 105 |
-
<button onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}')"
|
| 106 |
-
style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
|
| 107 |
-
border-radius: 5px; cursor: pointer;">Add</button>
|
| 108 |
-
</div>
|
| 109 |
</div>
|
| 110 |
"""
|
| 111 |
html_content += "</div>"
|
| 112 |
return html_content
|
| 113 |
|
| 114 |
-
# Function to handle adding items to the cart
|
| 115 |
-
def add_to_cart(dish_name, selected_extras, quantity, custom_description):
|
| 116 |
-
cart_items.append({
|
| 117 |
-
"name": dish_name,
|
| 118 |
-
"extras": selected_extras,
|
| 119 |
-
"quantity": quantity,
|
| 120 |
-
"custom_description": custom_description
|
| 121 |
-
})
|
| 122 |
-
return update_cart()
|
| 123 |
-
|
| 124 |
-
# Update cart display
|
| 125 |
-
def update_cart():
|
| 126 |
-
if not cart_items:
|
| 127 |
-
return "<p>Your cart is empty.</p>"
|
| 128 |
-
html_content = "<h3>Cart Items</h3><ul style='list-style-type: none; padding: 0;'>"
|
| 129 |
-
for item in cart_items:
|
| 130 |
-
extras = ", ".join(item["extras"])
|
| 131 |
-
html_content += f"<li style='margin-bottom: 10px;'><strong>{item['name']}</strong> (x{item['quantity']})<br>"
|
| 132 |
-
html_content += f"Extras: {extras if extras else 'None'}<br>"
|
| 133 |
-
html_content += f"Note: {item['custom_description']}</li>"
|
| 134 |
-
html_content += "</ul>"
|
| 135 |
-
return html_content
|
| 136 |
-
|
| 137 |
# Main Gradio App
|
| 138 |
with gr.Blocks() as demo:
|
| 139 |
-
gr.HTML("<h1 style='text-align: center;
|
| 140 |
|
| 141 |
-
with gr.Tabs()
|
| 142 |
with gr.Tab("Menu"):
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
btn_vegan = gr.Button("VEGAN")
|
| 146 |
-
btn_halal = gr.Button("HALAL")
|
| 147 |
-
dish_display = gr.HTML(value=display_dishes("ALL"))
|
| 148 |
|
| 149 |
with gr.Tab("Cart"):
|
| 150 |
-
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
-
btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
|
| 155 |
-
btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
|
| 156 |
-
|
| 157 |
-
# Add to cart button with input fields
|
| 158 |
-
add_to_cart_inputs = [
|
| 159 |
-
gr.Textbox(label="Dish Name", visible=False),
|
| 160 |
-
gr.Textbox(label="Selected Extras", visible=False),
|
| 161 |
-
gr.Number(label="Quantity", value=1, visible=False),
|
| 162 |
-
gr.Textbox(label="Custom Description", visible=False)
|
| 163 |
-
]
|
| 164 |
-
|
| 165 |
-
add_to_cart_button = gr.Button("Add to Cart", visible=False)
|
| 166 |
-
add_to_cart_button.click(
|
| 167 |
-
add_to_cart,
|
| 168 |
-
inputs=add_to_cart_inputs,
|
| 169 |
-
outputs=[cart_display]
|
| 170 |
-
)
|
| 171 |
|
| 172 |
gr.HTML(f"""
|
| 173 |
-
<div id="popup" style="display: none; position:
|
| 174 |
background-color: white; padding: 20px; border-radius: 10px;
|
| 175 |
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
|
| 176 |
<div style="position: absolute; top: 10px; right: 10px; cursor: pointer; font-size: 20px; font-weight: bold;" onclick="closePopup()">×</div>
|
| 177 |
<h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
|
| 178 |
<p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
|
| 179 |
<p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
|
| 180 |
-
<
|
| 181 |
-
<div style="display: flex; flex-wrap: wrap;">
|
| 182 |
-
""" + "".join([f'<label><input type="checkbox" name="extras" value="{extra["name"]}">{extra["name"]} + {extra["price"]}</label>' for extra in extras]) + """
|
| 183 |
-
</div>
|
| 184 |
-
<h4 style="text-align: left; margin-top: 20px;">Quantity</h4>
|
| 185 |
-
<div style="display: flex; align-items: center;">
|
| 186 |
-
<button onclick="decreaseQuantity()" style="padding: 5px 10px; margin-right: 5px;">-</button>
|
| 187 |
-
<input type="number" id="quantity" min="1" value="1" style="width: 50px; text-align: center;" readonly>
|
| 188 |
-
<button onclick="increaseQuantity()" style="padding: 5px 10px; margin-left: 5px;">+</button>
|
| 189 |
-
</div>
|
| 190 |
-
<h4 style="text-align: left; margin-top: 20px;">Add Custom Notes</h4>
|
| 191 |
-
<textarea id="custom-description" rows="2" style="width: 100%; padding: 5px; border-radius: 5px; border: 1px solid #ddd;"></textarea>
|
| 192 |
-
<button onclick="addItemToCart()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Add to Cart</button>
|
| 193 |
</div>
|
| 194 |
<script>
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
quantityInput.value = parseInt(quantityInput.value) + 1;
|
| 213 |
-
}
|
| 214 |
-
|
| 215 |
-
function decreaseQuantity() {
|
| 216 |
-
const quantityInput = document.getElementById('quantity');
|
| 217 |
-
if (parseInt(quantityInput.value) > 1) {
|
| 218 |
-
quantityInput.value = parseInt(quantityInput.value) - 1;
|
| 219 |
-
}
|
| 220 |
-
}
|
| 221 |
-
|
| 222 |
-
function addItemToCart() {
|
| 223 |
-
const extrasInputs = document.querySelectorAll('input[name="extras"]:checked');
|
| 224 |
-
selectedExtras = Array.from(extrasInputs).map(input => input.value);
|
| 225 |
-
const quantity = document.getElementById('quantity').value || 1;
|
| 226 |
-
const customDescription = document.getElementById('custom-description').value || "None";
|
| 227 |
-
|
| 228 |
-
// Pass data to Gradio backend
|
| 229 |
-
const gradioApp = document.querySelector("gradio-app").shadowRoot;
|
| 230 |
-
const dishNameInput = gradioApp.querySelector("textarea[aria-label='Dish Name']");
|
| 231 |
-
const extrasInput = gradioApp.querySelector("textarea[aria-label='Selected Extras']");
|
| 232 |
-
const quantityInput = gradioApp.querySelector("input[aria-label='Quantity']");
|
| 233 |
-
const customDescriptionInput = gradioApp.querySelector("textarea[aria-label='Custom Description']");
|
| 234 |
-
const addToCartButton = gradioApp.querySelector("button[aria-label='Add to Cart']");
|
| 235 |
-
|
| 236 |
-
if (dishNameInput && extrasInput && quantityInput && customDescriptionInput && addToCartButton) {
|
| 237 |
-
dishNameInput.value = selectedDish;
|
| 238 |
-
extrasInput.value = JSON.stringify(selectedExtras);
|
| 239 |
-
quantityInput.value = quantity;
|
| 240 |
-
customDescriptionInput.value = customDescription;
|
| 241 |
-
|
| 242 |
-
// Trigger Gradio's add-to-cart function
|
| 243 |
-
addToCartButton.click();
|
| 244 |
-
closePopup();
|
| 245 |
-
} else {
|
| 246 |
-
console.error("Failed to locate Gradio inputs or button.");
|
| 247 |
-
}
|
| 248 |
-
}
|
| 249 |
-
</script>
|
| 250 |
-
|
| 251 |
-
""")
|
| 252 |
|
| 253 |
demo.launch()
|
| 254 |
-
|
|
|
|
| 68 |
"price": "$6.99"}
|
| 69 |
]
|
| 70 |
|
|
|
|
| 71 |
cart_items = []
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# Filter dishes by category
|
| 74 |
def filter_dishes(category):
|
| 75 |
if category == "ALL":
|
|
|
|
| 79 |
# Generate dish cards with "Add" button
|
| 80 |
def display_dishes(category):
|
| 81 |
filtered_dishes = filter_dishes(category)
|
| 82 |
+
html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
|
| 83 |
for dish in filtered_dishes:
|
| 84 |
html_content += f"""
|
| 85 |
+
<div style='margin: 10px; width: 300px; border: 1px solid #ddd; border-radius: 10px; padding: 10px; text-align: center;'>
|
| 86 |
+
<img src='{dish['image']}' style='width: 100%; height: 150px; object-fit: cover; border-radius: 10px;'>
|
| 87 |
+
<h4>{dish['name']}</h4>
|
| 88 |
+
<p>{dish['description']}</p>
|
| 89 |
+
<p><strong>{dish['price']}</strong></p>
|
| 90 |
+
<button onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}')"
|
| 91 |
+
style='padding: 10px 15px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;'>Add</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
</div>
|
| 93 |
"""
|
| 94 |
html_content += "</div>"
|
| 95 |
return html_content
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
# Main Gradio App
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
+
gr.HTML("<h1 style='text-align: center;'>Dynamic Menu</h1>")
|
| 100 |
|
| 101 |
+
with gr.Tabs():
|
| 102 |
with gr.Tab("Menu"):
|
| 103 |
+
category_buttons = gr.ButtonGroup(["ALL", "VEGAN", "HALAL"])
|
| 104 |
+
menu_html = gr.HTML(value=display_dishes("ALL"))
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
with gr.Tab("Cart"):
|
| 107 |
+
cart_html = gr.HTML(value="<p>Your cart is empty.</p>")
|
| 108 |
|
| 109 |
+
# Update dish display on button click
|
| 110 |
+
category_buttons.change(display_dishes, inputs=[category_buttons], outputs=[menu_html])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
gr.HTML(f"""
|
| 113 |
+
<div id="popup" style="display: none; position: absolute; left: 50%; transform: translateX(-50%);
|
| 114 |
background-color: white; padding: 20px; border-radius: 10px;
|
| 115 |
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000; text-align: center; width: 500px;">
|
| 116 |
<div style="position: absolute; top: 10px; right: 10px; cursor: pointer; font-size: 20px; font-weight: bold;" onclick="closePopup()">×</div>
|
| 117 |
<h2 id="popup-title" style="margin-top: 20px; color: #444;"></h2>
|
| 118 |
<p id="popup-description" style="margin-top: 10px; font-size: 16px; color: #666;"></p>
|
| 119 |
<p id="popup-price" style="margin-top: 10px; font-size: 16px; color: #444;"></p>
|
| 120 |
+
<button onclick="closePopup()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Close</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
</div>
|
| 122 |
<script>
|
| 123 |
+
function showPopup(title, description, price) {
|
| 124 |
+
const popup = document.getElementById('popup');
|
| 125 |
+
document.getElementById('popup-title').textContent = title;
|
| 126 |
+
document.getElementById('popup-description').textContent = description;
|
| 127 |
+
document.getElementById('popup-price').textContent = price;
|
| 128 |
+
|
| 129 |
+
// Get scroll position and adjust popup position
|
| 130 |
+
const scrollY = window.scrollY;
|
| 131 |
+
popup.style.top = `${scrollY + 100}px`; // Offset for visibility
|
| 132 |
+
popup.style.display = 'block';
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
function closePopup() {
|
| 136 |
+
document.getElementById('popup').style.display = 'none';
|
| 137 |
+
}
|
| 138 |
+
</script>
|
| 139 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
demo.launch()
|
|
|