Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,22 +25,62 @@ def filter_menu(preference):
|
|
| 25 |
for _, item in filtered_data.iterrows():
|
| 26 |
html_content += f"""
|
| 27 |
<div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
</
|
|
|
|
|
|
|
|
|
|
| 39 |
"""
|
| 40 |
return html_content
|
| 41 |
|
| 42 |
# JavaScript for modal and cart behavior
|
| 43 |
modal_and_cart_js = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
<script>
|
| 45 |
let cart = [];
|
| 46 |
const extrasPrices = {
|
|
@@ -52,47 +92,48 @@ modal_and_cart_js = """
|
|
| 52 |
"Chilli Chicken": 14,
|
| 53 |
"Veg Manchurian": 12
|
| 54 |
};
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
alert("You cannot add more items after finalizing your order.");
|
| 60 |
-
return;
|
| 61 |
-
}
|
| 62 |
-
const modal = document.getElementById('modal');
|
| 63 |
-
modal.style.display = 'block';
|
| 64 |
-
modal.style.position = 'absolute';
|
| 65 |
-
|
| 66 |
-
// Get button position and adjust modal location
|
| 67 |
-
const buttonRect = event.target.getBoundingClientRect();
|
| 68 |
-
const isSmallScreen = window.innerWidth <= 768;
|
| 69 |
-
|
| 70 |
-
modal.style.top = `${window.scrollY + buttonRect.top + (isSmallScreen ? 20 : 0)}px`;
|
| 71 |
-
modal.style.left = isSmallScreen
|
| 72 |
-
? "50%"
|
| 73 |
-
: `${window.scrollX + buttonRect.left}px`;
|
| 74 |
-
modal.style.transform = isSmallScreen ? "translateX(-50%)" : "none";
|
| 75 |
-
modal.style.width = isSmallScreen ? "90%" : "30%";
|
| 76 |
-
|
| 77 |
-
// Update modal content
|
| 78 |
-
document.getElementById('modal-image').src = image;
|
| 79 |
-
document.getElementById('modal-name').innerText = name;
|
| 80 |
-
document.getElementById('modal-description').innerText = description;
|
| 81 |
-
document.getElementById('modal-price').innerText = price;
|
| 82 |
-
|
| 83 |
-
// Reset extras and quantity
|
| 84 |
-
const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
|
| 85 |
-
extrasInputs.forEach(input => input.checked = false);
|
| 86 |
-
document.getElementById('quantity').value = 1;
|
| 87 |
-
document.getElementById('special-instructions').value = '';
|
| 88 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
function closeModal() {
|
| 91 |
document.getElementById('modal').style.display = 'none';
|
| 92 |
}
|
| 93 |
-
|
| 94 |
-
// Functions for cart management (retained)
|
| 95 |
function addToCart() {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
const name = document.getElementById('modal-name').innerText;
|
| 97 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
| 98 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
|
@@ -100,29 +141,93 @@ modal_and_cart_js = """
|
|
| 100 |
const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
|
| 101 |
const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
|
| 102 |
const itemTotal = (price + extrasCost) * quantity;
|
| 103 |
-
const cartItem = { name, price, quantity, instructions, extras, itemTotal };
|
| 104 |
cart.push(cartItem);
|
| 105 |
alert(`${name} added to cart!`);
|
| 106 |
updateCartDisplay();
|
| 107 |
closeModal();
|
| 108 |
}
|
| 109 |
-
|
| 110 |
function updateCartDisplay() {
|
| 111 |
let totalBill = 0;
|
| 112 |
let cartHTML = "<div class='cart-container'>";
|
| 113 |
cart.forEach((item, index) => {
|
| 114 |
totalBill += item.itemTotal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
cartHTML += `<div class='cart-item'>
|
| 116 |
<span>${item.name}</span>
|
| 117 |
<span>Item Price: $${item.price.toFixed(2)}</span>
|
| 118 |
-
<
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
});
|
| 123 |
cartHTML += `</div><p class='cart-total'>Total Bill: $${totalBill.toFixed(2)}</p>`;
|
|
|
|
| 124 |
document.getElementById('floating-cart').innerHTML = cartHTML;
|
| 125 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
</script>
|
| 127 |
"""
|
| 128 |
|
|
@@ -131,17 +236,23 @@ def app():
|
|
| 131 |
with gr.Blocks() as demo:
|
| 132 |
gr.Markdown("## Dynamic Menu with Preferences")
|
| 133 |
|
|
|
|
| 134 |
selected_preference = gr.Radio(
|
| 135 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
| 136 |
value="All",
|
| 137 |
label="Choose a Preference",
|
| 138 |
)
|
| 139 |
|
|
|
|
| 140 |
menu_output = gr.HTML(value=filter_menu("All"))
|
|
|
|
|
|
|
| 141 |
cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
|
|
|
|
|
|
|
| 142 |
final_order_output = gr.HTML(value="", elem_id="final-order")
|
| 143 |
|
| 144 |
-
# Modal window
|
| 145 |
modal_window = gr.HTML("""
|
| 146 |
<div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
|
| 147 |
<div style="text-align: right;">
|
|
@@ -173,8 +284,10 @@ def app():
|
|
| 173 |
</div>
|
| 174 |
""")
|
| 175 |
|
|
|
|
| 176 |
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
|
| 177 |
|
|
|
|
| 178 |
gr.Row([selected_preference])
|
| 179 |
gr.Row(menu_output)
|
| 180 |
gr.Row(cart_output)
|
|
@@ -185,5 +298,4 @@ def app():
|
|
| 185 |
return demo
|
| 186 |
|
| 187 |
if __name__ == "__main__":
|
| 188 |
-
demo = app()
|
| 189 |
-
demo.launch()
|
|
|
|
| 25 |
for _, item in filtered_data.iterrows():
|
| 26 |
html_content += f"""
|
| 27 |
<div style="display: flex; align-items: center; border: 1px solid #ddd; border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);">
|
| 28 |
+
<div style="flex: 1; margin-right: 15px;">
|
| 29 |
+
<h3 style="margin: 0; font-size: 18px;">{item['Dish Name']}</h3>
|
| 30 |
+
<p style="margin: 5px 0; font-size: 16px; color: #888;">${item['Price ($)']}</p>
|
| 31 |
+
<p style="margin: 5px 0; font-size: 14px; color: #555;">{item['Description']}</p>
|
| 32 |
+
</div>
|
| 33 |
+
<div style="flex-shrink: 0; text-align: center;">
|
| 34 |
+
<img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
|
| 35 |
+
<button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;"
|
| 36 |
+
onclick="openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}', event)">
|
| 37 |
+
Add
|
| 38 |
+
</button>
|
| 39 |
+
</div>
|
| 40 |
+
</div>
|
| 41 |
+
|
| 42 |
"""
|
| 43 |
return html_content
|
| 44 |
|
| 45 |
# JavaScript for modal and cart behavior
|
| 46 |
modal_and_cart_js = """
|
| 47 |
+
<style>
|
| 48 |
+
.cart-container {
|
| 49 |
+
border: 1px solid #ddd;
|
| 50 |
+
border-radius: 8px;
|
| 51 |
+
padding: 15px;
|
| 52 |
+
margin-bottom: 15px;
|
| 53 |
+
background-color: #f9f9f9;
|
| 54 |
+
display: flex;
|
| 55 |
+
flex-direction: column;
|
| 56 |
+
gap: 10px;
|
| 57 |
+
}
|
| 58 |
+
.cart-item {
|
| 59 |
+
display: flex;
|
| 60 |
+
justify-content: space-between;
|
| 61 |
+
align-items: center;
|
| 62 |
+
border-bottom: 1px solid #ddd;
|
| 63 |
+
padding: 10px 0;
|
| 64 |
+
}
|
| 65 |
+
.cart-item:last-child {
|
| 66 |
+
border-bottom: none;
|
| 67 |
+
}
|
| 68 |
+
.cart-item span {
|
| 69 |
+
font-size: 16px;
|
| 70 |
+
margin-right: 10px;
|
| 71 |
+
}
|
| 72 |
+
.cart-item .quantity-container {
|
| 73 |
+
display: flex;
|
| 74 |
+
align-items: center;
|
| 75 |
+
gap: 5px;
|
| 76 |
+
}
|
| 77 |
+
.cart-total {
|
| 78 |
+
font-size: 18px;
|
| 79 |
+
font-weight: bold;
|
| 80 |
+
text-align: right;
|
| 81 |
+
margin-top: 10px;
|
| 82 |
+
}
|
| 83 |
+
</style>
|
| 84 |
<script>
|
| 85 |
let cart = [];
|
| 86 |
const extrasPrices = {
|
|
|
|
| 92 |
"Chilli Chicken": 14,
|
| 93 |
"Veg Manchurian": 12
|
| 94 |
};
|
| 95 |
+
function openModal(name, image, description, price, event) {
|
| 96 |
+
if (finalized) {
|
| 97 |
+
alert("You cannot add more items after finalizing your order.");
|
| 98 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
+
const modal = document.getElementById('modal');
|
| 101 |
+
modal.style.display = 'block';
|
| 102 |
+
modal.style.position = 'absolute';
|
| 103 |
+
|
| 104 |
+
// Get the button position and calculate modal position
|
| 105 |
+
const buttonRect = event.target.getBoundingClientRect();
|
| 106 |
+
const isSmallScreen = window.innerWidth <= 768;
|
| 107 |
+
|
| 108 |
+
modal.style.top = `${window.scrollY + buttonRect.top + (isSmallScreen ? 20 : 0)}px`;
|
| 109 |
+
modal.style.left = isSmallScreen
|
| 110 |
+
? "50%"
|
| 111 |
+
: `${window.scrollX + buttonRect.left}px`;
|
| 112 |
+
|
| 113 |
+
modal.style.transform = isSmallScreen ? "translateX(-50%)" : "none";
|
| 114 |
+
modal.style.width = isSmallScreen ? "90%" : "30%";
|
| 115 |
+
|
| 116 |
+
// Update modal content
|
| 117 |
+
document.getElementById('modal-image').src = image;
|
| 118 |
+
document.getElementById('modal-name').innerText = name;
|
| 119 |
+
document.getElementById('modal-description').innerText = description;
|
| 120 |
+
document.getElementById('modal-price').innerText = price;
|
| 121 |
+
|
| 122 |
+
// Reset extras and quantity
|
| 123 |
+
const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
|
| 124 |
+
extrasInputs.forEach(input => input.checked = false);
|
| 125 |
+
document.getElementById('quantity').value = 1;
|
| 126 |
+
document.getElementById('special-instructions').value = '';
|
| 127 |
+
}
|
| 128 |
|
| 129 |
function closeModal() {
|
| 130 |
document.getElementById('modal').style.display = 'none';
|
| 131 |
}
|
|
|
|
|
|
|
| 132 |
function addToCart() {
|
| 133 |
+
if (finalized) {
|
| 134 |
+
alert("You cannot add more items after finalizing your order.");
|
| 135 |
+
return;
|
| 136 |
+
}
|
| 137 |
const name = document.getElementById('modal-name').innerText;
|
| 138 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
| 139 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
|
|
|
| 141 |
const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
|
| 142 |
const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
|
| 143 |
const itemTotal = (price + extrasCost) * quantity;
|
| 144 |
+
const cartItem = { name, price, quantity, instructions, extras, itemTotal, extrasQuantities: extras.map(() => 1) };
|
| 145 |
cart.push(cartItem);
|
| 146 |
alert(`${name} added to cart!`);
|
| 147 |
updateCartDisplay();
|
| 148 |
closeModal();
|
| 149 |
}
|
|
|
|
| 150 |
function updateCartDisplay() {
|
| 151 |
let totalBill = 0;
|
| 152 |
let cartHTML = "<div class='cart-container'>";
|
| 153 |
cart.forEach((item, index) => {
|
| 154 |
totalBill += item.itemTotal;
|
| 155 |
+
const extras = item.extras.map((extra, i) => {
|
| 156 |
+
const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
|
| 157 |
+
const extraTotal = extrasPrices[extra] * extraQuantity;
|
| 158 |
+
totalBill += extraTotal;
|
| 159 |
+
return `<div class='cart-item'>
|
| 160 |
+
<span>${extra}</span>
|
| 161 |
+
<span>Price: $${extrasPrices[extra].toFixed(2)}</span>
|
| 162 |
+
<div class='quantity-container'>
|
| 163 |
+
<label for='extra-quantity-${index}-${i}'>Quantity:</label>
|
| 164 |
+
<input type='number' id='extra-quantity-${index}-${i}' value='${extraQuantity}' min='1' style='width: 50px;' onchange='updateExtraQuantity(${index}, ${i}, this.value)'>
|
| 165 |
+
</div>
|
| 166 |
+
<span>Total: $${extraTotal.toFixed(2)}</span>
|
| 167 |
+
<input type='checkbox' id='extra-remove-${index}-${i}' onclick='removeExtra(${index}, ${i})'> Remove
|
| 168 |
+
</div>`;
|
| 169 |
+
}).join('');
|
| 170 |
cartHTML += `<div class='cart-item'>
|
| 171 |
<span>${item.name}</span>
|
| 172 |
<span>Item Price: $${item.price.toFixed(2)}</span>
|
| 173 |
+
<div class='quantity-container'>
|
| 174 |
+
<label for='item-quantity-${index}'>Quantity:</label>
|
| 175 |
+
<input type='number' id='item-quantity-${index}' value='${item.quantity}' min='1' style='width: 50px;' onchange='updateItemQuantity(${index}, this.value)'>
|
| 176 |
+
</div>
|
| 177 |
+
<span>Total: $${(item.price * item.quantity).toFixed(2)}</span>
|
| 178 |
+
<input type='checkbox' id='item-remove-${index}' onclick='removeItem(${index})'> Remove
|
| 179 |
+
</div>
|
| 180 |
+
${extras}
|
| 181 |
+
<div class='cart-item'><strong>Instructions:</strong> ${item.instructions || "None"}</div>`;
|
| 182 |
});
|
| 183 |
cartHTML += `</div><p class='cart-total'>Total Bill: $${totalBill.toFixed(2)}</p>`;
|
| 184 |
+
cartHTML += `<button style='margin-top: 10px; background-color: #007bff; color: white; border: none; padding: 10px; border-radius: 5px; width: 100%; cursor: pointer;' onclick='submitCart()'>Submit</button>`;
|
| 185 |
document.getElementById('floating-cart').innerHTML = cartHTML;
|
| 186 |
}
|
| 187 |
+
function updateItemQuantity(index, newQuantity) {
|
| 188 |
+
const quantity = parseInt(newQuantity) || 1;
|
| 189 |
+
cart[index].quantity = quantity;
|
| 190 |
+
cart[index].itemTotal = cart[index].price * quantity;
|
| 191 |
+
updateCartDisplay();
|
| 192 |
+
}
|
| 193 |
+
function updateExtraQuantity(cartIndex, extraIndex, newQuantity) {
|
| 194 |
+
const quantity = parseInt(newQuantity) || 1;
|
| 195 |
+
cart[cartIndex].extrasQuantities = cart[cartIndex].extrasQuantities || [];
|
| 196 |
+
cart[cartIndex].extrasQuantities[extraIndex] = quantity;
|
| 197 |
+
updateCartDisplay();
|
| 198 |
+
}
|
| 199 |
+
function removeExtra(cartIndex, extraIndex) {
|
| 200 |
+
cart[cartIndex].extras.splice(extraIndex, 1);
|
| 201 |
+
if (cart[cartIndex].extrasQuantities) {
|
| 202 |
+
cart[cartIndex].extrasQuantities.splice(extraIndex, 1);
|
| 203 |
+
}
|
| 204 |
+
updateCartDisplay();
|
| 205 |
+
}
|
| 206 |
+
function removeItem(index) {
|
| 207 |
+
cart.splice(index, 1);
|
| 208 |
+
updateCartDisplay();
|
| 209 |
+
}
|
| 210 |
+
function submitCart() {
|
| 211 |
+
let finalOrderHTML = "<h3>Final Order:</h3><ul>";
|
| 212 |
+
let totalBill = 0;
|
| 213 |
+
cart.forEach(item => {
|
| 214 |
+
totalBill += item.itemTotal;
|
| 215 |
+
const extras = item.extras.map((extra, i) => {
|
| 216 |
+
const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
|
| 217 |
+
const extraTotal = extrasPrices[extra] * extraQuantity;
|
| 218 |
+
totalBill += extraTotal;
|
| 219 |
+
return `${extra} (x${extraQuantity}) - $${extraTotal.toFixed(2)}`;
|
| 220 |
+
}).join(', ');
|
| 221 |
+
finalOrderHTML += `<li>
|
| 222 |
+
${item.name} (x${item.quantity}) - $${item.itemTotal.toFixed(2)}
|
| 223 |
+
<br>Extras: ${extras}
|
| 224 |
+
<br>Instructions: ${item.instructions || "None"}
|
| 225 |
+
</li>`;
|
| 226 |
+
});
|
| 227 |
+
finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
|
| 228 |
+
document.getElementById('final-order').innerHTML = finalOrderHTML;
|
| 229 |
+
alert("Your final order has been submitted!");
|
| 230 |
+
}
|
| 231 |
</script>
|
| 232 |
"""
|
| 233 |
|
|
|
|
| 236 |
with gr.Blocks() as demo:
|
| 237 |
gr.Markdown("## Dynamic Menu with Preferences")
|
| 238 |
|
| 239 |
+
# Radio button for selecting preference
|
| 240 |
selected_preference = gr.Radio(
|
| 241 |
choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
|
| 242 |
value="All",
|
| 243 |
label="Choose a Preference",
|
| 244 |
)
|
| 245 |
|
| 246 |
+
# Output area for menu items
|
| 247 |
menu_output = gr.HTML(value=filter_menu("All"))
|
| 248 |
+
|
| 249 |
+
# Floating cart display
|
| 250 |
cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
|
| 251 |
+
|
| 252 |
+
# Final order display
|
| 253 |
final_order_output = gr.HTML(value="", elem_id="final-order")
|
| 254 |
|
| 255 |
+
# Modal window
|
| 256 |
modal_window = gr.HTML("""
|
| 257 |
<div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
|
| 258 |
<div style="text-align: right;">
|
|
|
|
| 284 |
</div>
|
| 285 |
""")
|
| 286 |
|
| 287 |
+
# Update menu dynamically based on preference
|
| 288 |
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
|
| 289 |
|
| 290 |
+
# Layout
|
| 291 |
gr.Row([selected_preference])
|
| 292 |
gr.Row(menu_output)
|
| 293 |
gr.Row(cart_output)
|
|
|
|
| 298 |
return demo
|
| 299 |
|
| 300 |
if __name__ == "__main__":
|
| 301 |
+
demo = app()
|
|
|