Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,37 +38,89 @@ def filter_menu(preference):
|
|
| 38 |
"""
|
| 39 |
return html_content
|
| 40 |
|
| 41 |
-
# Function to display final
|
| 42 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
total_bill = 0
|
| 44 |
final_order_html = "<h3>Your Final Order:</h3><ul>"
|
| 45 |
for item in cart:
|
| 46 |
total_bill += item['itemTotal']
|
| 47 |
-
extras =
|
| 48 |
final_order_html += f"<li>{item['name']} (x{item['quantity']}, Spice: {item['spiceLevel']}, Extras: {extras}, Instructions: {item['instructions']}) - ${item['itemTotal']:.2f}</li>"
|
| 49 |
final_order_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
|
| 50 |
final_order_html += "<p style='color: green; font-size: 18px;'>Your final order has been placed. Thank you!</p>"
|
| 51 |
return final_order_html
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Gradio app definition
|
| 71 |
def app():
|
|
|
|
|
|
|
|
|
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
gr.Markdown("## Dynamic Menu with Preferences")
|
| 74 |
|
|
@@ -83,26 +135,70 @@ def app():
|
|
| 83 |
menu_output = gr.HTML(value=filter_menu("All"))
|
| 84 |
|
| 85 |
# Floating cart display
|
| 86 |
-
cart_output = gr.HTML(value="Your cart is empty.")
|
| 87 |
|
| 88 |
# Final order display
|
| 89 |
-
final_order_output = gr.HTML(value="")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
#
|
| 92 |
finalize_button = gr.Button("Finalize Order")
|
| 93 |
|
| 94 |
-
# Finalize button click
|
| 95 |
-
finalize_button.click(
|
| 96 |
|
| 97 |
# Update menu dynamically based on preference
|
| 98 |
-
selected_preference.change(
|
| 99 |
|
| 100 |
# Layout
|
| 101 |
gr.Row([selected_preference])
|
| 102 |
gr.Row(menu_output)
|
| 103 |
gr.Row(cart_output)
|
|
|
|
| 104 |
gr.Row(finalize_button)
|
| 105 |
gr.Row(final_order_output)
|
|
|
|
| 106 |
|
| 107 |
return demo
|
| 108 |
|
|
|
|
| 38 |
"""
|
| 39 |
return html_content
|
| 40 |
|
| 41 |
+
# Function to finalize the order and display the final page
|
| 42 |
+
def finalize_order():
|
| 43 |
+
global cart
|
| 44 |
+
if not cart:
|
| 45 |
+
return "<p style='color: red; font-size: 18px;'>Your cart is empty. Please add items before finalizing.</p>"
|
| 46 |
+
|
| 47 |
total_bill = 0
|
| 48 |
final_order_html = "<h3>Your Final Order:</h3><ul>"
|
| 49 |
for item in cart:
|
| 50 |
total_bill += item['itemTotal']
|
| 51 |
+
extras = ', '.join(item['extras']) if item['extras'] else 'None'
|
| 52 |
final_order_html += f"<li>{item['name']} (x{item['quantity']}, Spice: {item['spiceLevel']}, Extras: {extras}, Instructions: {item['instructions']}) - ${item['itemTotal']:.2f}</li>"
|
| 53 |
final_order_html += f"</ul><p><strong>Total Bill: ${total_bill:.2f}</strong></p>"
|
| 54 |
final_order_html += "<p style='color: green; font-size: 18px;'>Your final order has been placed. Thank you!</p>"
|
| 55 |
return final_order_html
|
| 56 |
|
| 57 |
+
# JavaScript for modal and cart behavior
|
| 58 |
+
modal_and_cart_js = """
|
| 59 |
+
<script>
|
| 60 |
+
let cart = [];
|
| 61 |
+
const extrasPrices = {
|
| 62 |
+
"Extra Raitha 4oz": 1,
|
| 63 |
+
"Extra Raitha 8oz": 2,
|
| 64 |
+
"Extra Salan 4oz": 1,
|
| 65 |
+
"Extra Salan 8oz": 2,
|
| 66 |
+
"Extra Onion": 1,
|
| 67 |
+
"Extra Onion & Lemon": 2,
|
| 68 |
+
"Extra Fried Onion 4oz": 2
|
| 69 |
+
};
|
| 70 |
+
function openModal(name, image, description, price) {
|
| 71 |
+
document.getElementById('modal').style.display = 'block';
|
| 72 |
+
document.getElementById('modal-image').src = image;
|
| 73 |
+
document.getElementById('modal-name').innerText = name;
|
| 74 |
+
document.getElementById('modal-description').innerText = description;
|
| 75 |
+
document.getElementById('modal-price').innerText = price;
|
| 76 |
+
const spiceLevelInputs = document.querySelectorAll('input[name="spice-level"]');
|
| 77 |
+
spiceLevelInputs.forEach(input => input.checked = false);
|
| 78 |
+
const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
|
| 79 |
+
extrasInputs.forEach(input => input.checked = false);
|
| 80 |
+
document.getElementById('quantity').value = 1;
|
| 81 |
+
document.getElementById('special-instructions').value = '';
|
| 82 |
+
}
|
| 83 |
+
function closeModal() {
|
| 84 |
+
document.getElementById('modal').style.display = 'none';
|
| 85 |
+
}
|
| 86 |
+
function addToCart() {
|
| 87 |
+
const name = document.getElementById('modal-name').innerText;
|
| 88 |
+
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
| 89 |
+
const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
|
| 90 |
+
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
| 91 |
+
const instructions = document.getElementById('special-instructions').value;
|
| 92 |
+
const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
|
| 93 |
+
const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
|
| 94 |
+
const itemTotal = (price + extrasCost) * quantity;
|
| 95 |
+
const cartItem = { name, price, spiceLevel, quantity, instructions, extras, itemTotal };
|
| 96 |
+
cart.push(cartItem);
|
| 97 |
+
alert(`${name} added to cart!`);
|
| 98 |
+
updateCartDisplay();
|
| 99 |
+
closeModal();
|
| 100 |
+
}
|
| 101 |
+
function updateCartDisplay() {
|
| 102 |
+
let totalBill = 0;
|
| 103 |
+
let cartHTML = "<h3>Your Cart:</h3><ul>";
|
| 104 |
+
cart.forEach((item, index) => {
|
| 105 |
+
totalBill += item.itemTotal;
|
| 106 |
+
const extras = item.extras.join(', ');
|
| 107 |
+
cartHTML += `<li><input type='checkbox' id='cart-item-${index}' value='${index}' checked onclick='removeItem(${index})'> ${item.name} (x${item.quantity}, Spice: ${item.spiceLevel}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
|
| 108 |
+
});
|
| 109 |
+
cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
|
| 110 |
+
document.getElementById('floating-cart').innerHTML = cartHTML;
|
| 111 |
+
}
|
| 112 |
+
function removeItem(index) {
|
| 113 |
+
cart.splice(index, 1);
|
| 114 |
+
updateCartDisplay();
|
| 115 |
+
}
|
| 116 |
+
</script>
|
| 117 |
+
"""
|
| 118 |
|
| 119 |
# Gradio app definition
|
| 120 |
def app():
|
| 121 |
+
global cart
|
| 122 |
+
cart = []
|
| 123 |
+
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
gr.Markdown("## Dynamic Menu with Preferences")
|
| 126 |
|
|
|
|
| 135 |
menu_output = gr.HTML(value=filter_menu("All"))
|
| 136 |
|
| 137 |
# Floating cart display
|
| 138 |
+
cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
|
| 139 |
|
| 140 |
# Final order display
|
| 141 |
+
final_order_output = gr.HTML(value="", elem_id="final-page")
|
| 142 |
+
|
| 143 |
+
# Modal window
|
| 144 |
+
modal_window = gr.HTML("""
|
| 145 |
+
<div id="modal" style="display: none; position: fixed; top: 40%; left: 50%; transform: translate(-50%, -40%); width: 50%; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
|
| 146 |
+
<div style="text-align: right;">
|
| 147 |
+
<button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">×</button>
|
| 148 |
+
</div>
|
| 149 |
+
<img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
|
| 150 |
+
<h2 id="modal-name"></h2>
|
| 151 |
+
<p id="modal-description"></p>
|
| 152 |
+
<p id="modal-price"></p>
|
| 153 |
+
<!-- Spice Levels -->
|
| 154 |
+
<label for="spice-level">Choose a Spice Level (Required):</label>
|
| 155 |
+
<div id="spice-level-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
|
| 156 |
+
<label><input type="radio" name="spice-level" value="American Mild" /> American Mild</label>
|
| 157 |
+
<label><input type="radio" name="spice-level" value="American Medium" /> American Medium</label>
|
| 158 |
+
<label><input type="radio" name="spice-level" value="American Spicy" /> American Spicy</label>
|
| 159 |
+
<label><input type="radio" name="spice-level" value="Indian Mild" /> Indian Mild</label>
|
| 160 |
+
<label><input type="radio" name="spice-level" value="Indian Medium" /> Indian Medium</label>
|
| 161 |
+
<label><input type="radio" name="spice-level" value="Indian Very Spicy" /> Indian Very Spicy</label>
|
| 162 |
+
</div>
|
| 163 |
+
<!-- Biryani Extras -->
|
| 164 |
+
<label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
|
| 165 |
+
<div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
|
| 166 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Raitha 4oz" /> Extra Raitha 4oz + $1.00</label>
|
| 167 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Raitha 8oz" /> Extra Raitha 8oz + $2.00</label>
|
| 168 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Salan 4oz" /> Extra Salan 4oz + $1.00</label>
|
| 169 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label>
|
| 170 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Onion" /> Extra Onion + $1.00</label>
|
| 171 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
|
| 172 |
+
<label><input type="checkbox" name="biryani-extra" value="Extra Fried Onion 4oz" /> Extra Fried Onion 4oz + $2.00</label>
|
| 173 |
+
</div>
|
| 174 |
+
<!-- Quantity and Special Instructions -->
|
| 175 |
+
<label for="quantity">Quantity:</label>
|
| 176 |
+
<input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
|
| 177 |
+
<br><br>
|
| 178 |
+
<textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
|
| 179 |
+
<br><br>
|
| 180 |
+
<!-- Add to Cart Button -->
|
| 181 |
+
<button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
|
| 182 |
+
</div>
|
| 183 |
+
""")
|
| 184 |
|
| 185 |
+
# Finalize Order Button
|
| 186 |
finalize_button = gr.Button("Finalize Order")
|
| 187 |
|
| 188 |
+
# Finalize order button click event
|
| 189 |
+
finalize_button.click(finalize_order, outputs=[final_order_output])
|
| 190 |
|
| 191 |
# Update menu dynamically based on preference
|
| 192 |
+
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
|
| 193 |
|
| 194 |
# Layout
|
| 195 |
gr.Row([selected_preference])
|
| 196 |
gr.Row(menu_output)
|
| 197 |
gr.Row(cart_output)
|
| 198 |
+
gr.Row(modal_window)
|
| 199 |
gr.Row(finalize_button)
|
| 200 |
gr.Row(final_order_output)
|
| 201 |
+
gr.HTML(modal_and_cart_js)
|
| 202 |
|
| 203 |
return demo
|
| 204 |
|