Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -152,7 +152,6 @@ def modal_js():
|
|
152 |
<script>
|
153 |
let cart = [];
|
154 |
let totalCartCost = 0;
|
155 |
-
|
156 |
function openModal(name, image2, description, price) {
|
157 |
const modal = document.getElementById('modal');
|
158 |
modal.style.display = 'block';
|
@@ -167,68 +166,57 @@ def modal_js():
|
|
167 |
document.getElementById('modal-price').innerText = price;
|
168 |
document.getElementById('quantity').value = 1;
|
169 |
document.getElementById('special-instructions').value = '';
|
170 |
-
|
171 |
resetAddOns(); // Reset add-ons when opening the modal
|
172 |
}
|
173 |
-
|
174 |
function closeModal() {
|
175 |
document.getElementById('modal').style.display = 'none';
|
176 |
}
|
177 |
-
|
178 |
function addToCart() {
|
179 |
const name = document.getElementById('modal-name').innerText;
|
180 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
181 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
182 |
const instructions = document.getElementById('special-instructions').value;
|
183 |
-
|
184 |
const selectedAddOns = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'));
|
185 |
const extras = selectedAddOns.map(extra => ({
|
186 |
name: extra.value,
|
187 |
price: parseFloat(extra.getAttribute('data-price')),
|
188 |
quantity: 1 // Default quantity for add-ons is 1
|
189 |
}));
|
190 |
-
|
191 |
const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
192 |
const totalCost = (price * quantity) + extrasCost;
|
193 |
-
|
194 |
// Add the item to the cart with its specific add-ons
|
195 |
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
196 |
totalCartCost += totalCost; // Update the total cost of the cart
|
197 |
-
|
198 |
updateCartButton();
|
199 |
updateCartTotalCost(); // Update total cost displayed
|
200 |
closeModal();
|
201 |
}
|
202 |
-
|
203 |
function updateCartButton() {
|
204 |
const cartButton = document.getElementById('cart-button');
|
205 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
206 |
}
|
207 |
-
|
208 |
function openCartModal() {
|
209 |
const cartModal = document.getElementById('cart-modal');
|
210 |
const cartItemsContainer = document.getElementById('cart-items');
|
211 |
cartItemsContainer.innerHTML = "";
|
212 |
cart.forEach((item, index) => {
|
213 |
-
const extrasList = item.extras.map(extra => `${extra.name} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
|
214 |
cartItemsContainer.innerHTML += `
|
215 |
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
|
216 |
<h3>${item.name}</h3>
|
217 |
-
<p>Quantity: ${item.quantity}
|
218 |
<p>Extras: ${extrasList || 'None'}</p>
|
219 |
<p>Special Instructions: ${item.instructions || 'None'}</p>
|
220 |
-
<p>Total Cost:
|
221 |
<button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
|
222 |
</div>
|
223 |
`;
|
224 |
});
|
225 |
cartModal.style.display = 'block';
|
226 |
}
|
227 |
-
|
228 |
function closeCartModal() {
|
229 |
document.getElementById('cart-modal').style.display = 'none';
|
230 |
}
|
231 |
-
|
232 |
function removeFromCart(index) {
|
233 |
totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
|
234 |
cart.splice(index, 1);
|
@@ -236,32 +224,40 @@ def modal_js():
|
|
236 |
updateCartTotalCost(); // Update total cost displayed
|
237 |
openCartModal();
|
238 |
}
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
function updateCartTotalCost() {
|
241 |
const totalCostElement = document.getElementById('cart-total-cost');
|
242 |
totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
|
243 |
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
244 |
}
|
245 |
-
|
246 |
function proceedToCheckout() {
|
247 |
-
const
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
<
|
255 |
-
<p>Quantity: ${item.quantity}</p>
|
256 |
-
<p>Extras: ${extrasList || 'None'}</p>
|
257 |
-
<p>Special Instructions: ${item.instructions || 'None'}</p>
|
258 |
-
<p>Total Cost: $${item.totalCost.toFixed(2)}</p>
|
259 |
</div>
|
260 |
`;
|
261 |
});
|
262 |
-
|
263 |
-
|
264 |
-
|
|
|
265 |
}
|
266 |
</script>
|
267 |
"""
|
@@ -272,7 +268,6 @@ with gr.Blocks() as app:
|
|
272 |
with gr.Row():
|
273 |
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
274 |
|
275 |
-
# Login Page
|
276 |
with gr.Row(visible=True) as login_page:
|
277 |
with gr.Column():
|
278 |
login_email = gr.Textbox(label="Email")
|
@@ -281,7 +276,6 @@ with gr.Blocks() as app:
|
|
281 |
signup_button = gr.Button("Go to Signup")
|
282 |
login_output = gr.Textbox(label="Status")
|
283 |
|
284 |
-
# Signup Page
|
285 |
with gr.Row(visible=False) as signup_page:
|
286 |
with gr.Column():
|
287 |
signup_name = gr.Textbox(label="Name")
|
@@ -292,7 +286,6 @@ with gr.Blocks() as app:
|
|
292 |
login_redirect = gr.Button("Go to Login")
|
293 |
signup_output = gr.Textbox(label="Status")
|
294 |
|
295 |
-
# Menu Page
|
296 |
with gr.Row(visible=False) as menu_page:
|
297 |
with gr.Column():
|
298 |
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
@@ -302,19 +295,11 @@ with gr.Blocks() as app:
|
|
302 |
gr.HTML(create_modal_window())
|
303 |
gr.HTML(modal_js())
|
304 |
|
305 |
-
# Checkout Page
|
306 |
-
with gr.Row(visible=False) as checkout_page:
|
307 |
-
with gr.Column():
|
308 |
-
gr.HTML("<h2 style='text-align: center;'>Final Order</h2>")
|
309 |
-
cart_summary = gr.HTML()
|
310 |
-
gr.Button("Confirm Order")
|
311 |
-
|
312 |
login_button.click(
|
313 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
314 |
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
|
315 |
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
|
316 |
)
|
317 |
-
|
318 |
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
|
319 |
|
320 |
app.launch()
|
|
|
152 |
<script>
|
153 |
let cart = [];
|
154 |
let totalCartCost = 0;
|
|
|
155 |
function openModal(name, image2, description, price) {
|
156 |
const modal = document.getElementById('modal');
|
157 |
modal.style.display = 'block';
|
|
|
166 |
document.getElementById('modal-price').innerText = price;
|
167 |
document.getElementById('quantity').value = 1;
|
168 |
document.getElementById('special-instructions').value = '';
|
|
|
169 |
resetAddOns(); // Reset add-ons when opening the modal
|
170 |
}
|
|
|
171 |
function closeModal() {
|
172 |
document.getElementById('modal').style.display = 'none';
|
173 |
}
|
|
|
174 |
function addToCart() {
|
175 |
const name = document.getElementById('modal-name').innerText;
|
176 |
const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
|
177 |
const quantity = parseInt(document.getElementById('quantity').value) || 1;
|
178 |
const instructions = document.getElementById('special-instructions').value;
|
|
|
179 |
const selectedAddOns = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked'));
|
180 |
const extras = selectedAddOns.map(extra => ({
|
181 |
name: extra.value,
|
182 |
price: parseFloat(extra.getAttribute('data-price')),
|
183 |
quantity: 1 // Default quantity for add-ons is 1
|
184 |
}));
|
|
|
185 |
const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
186 |
const totalCost = (price * quantity) + extrasCost;
|
|
|
187 |
// Add the item to the cart with its specific add-ons
|
188 |
cart.push({ name, price, quantity, extras, instructions, totalCost });
|
189 |
totalCartCost += totalCost; // Update the total cost of the cart
|
|
|
190 |
updateCartButton();
|
191 |
updateCartTotalCost(); // Update total cost displayed
|
192 |
closeModal();
|
193 |
}
|
|
|
194 |
function updateCartButton() {
|
195 |
const cartButton = document.getElementById('cart-button');
|
196 |
cartButton.innerText = `View Cart (${cart.length} items)`;
|
197 |
}
|
|
|
198 |
function openCartModal() {
|
199 |
const cartModal = document.getElementById('cart-modal');
|
200 |
const cartItemsContainer = document.getElementById('cart-items');
|
201 |
cartItemsContainer.innerHTML = "";
|
202 |
cart.forEach((item, index) => {
|
203 |
+
const extrasList = item.extras.map(extra => `${extra.name} x<input type="number" value="${extra.quantity}" min="1" style="width: 50px;" onchange="updateCartItem(${index}, 'extra', this.value)" /> (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ');
|
204 |
cartItemsContainer.innerHTML += `
|
205 |
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
|
206 |
<h3>${item.name}</h3>
|
207 |
+
<p>Quantity: <input type="number" value="${item.quantity}" min="1" style="width: 50px;" onchange="updateCartItem(${index}, 'item', this.value)" /></p>
|
208 |
<p>Extras: ${extrasList || 'None'}</p>
|
209 |
<p>Special Instructions: ${item.instructions || 'None'}</p>
|
210 |
+
<p>Total Cost: $<span id="item-${index}-total">${item.totalCost.toFixed(2)}</span></p>
|
211 |
<button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
|
212 |
</div>
|
213 |
`;
|
214 |
});
|
215 |
cartModal.style.display = 'block';
|
216 |
}
|
|
|
217 |
function closeCartModal() {
|
218 |
document.getElementById('cart-modal').style.display = 'none';
|
219 |
}
|
|
|
220 |
function removeFromCart(index) {
|
221 |
totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
|
222 |
cart.splice(index, 1);
|
|
|
224 |
updateCartTotalCost(); // Update total cost displayed
|
225 |
openCartModal();
|
226 |
}
|
227 |
+
function updateCartItem(index, type, value) {
|
228 |
+
if (type === 'item') {
|
229 |
+
cart[index].quantity = parseInt(value);
|
230 |
+
} else if (type === 'extra') {
|
231 |
+
cart[index].extras[0].quantity = parseInt(value); // Assuming one add-on for simplicity
|
232 |
+
}
|
233 |
+
const item = cart[index];
|
234 |
+
const price = item.price;
|
235 |
+
const extrasCost = item.extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
|
236 |
+
item.totalCost = (price * item.quantity) + extrasCost;
|
237 |
+
document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
|
238 |
+
updateCartTotalCost(); // Update total cost displayed
|
239 |
+
}
|
240 |
function updateCartTotalCost() {
|
241 |
const totalCostElement = document.getElementById('cart-total-cost');
|
242 |
totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
|
243 |
totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
|
244 |
}
|
|
|
245 |
function proceedToCheckout() {
|
246 |
+
const checkoutModal = document.getElementById('final-order-section');
|
247 |
+
let finalOrderHTML = "<h1>Final Order</h1>";
|
248 |
+
cart.forEach((item) => {
|
249 |
+
finalOrderHTML += `
|
250 |
+
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;">
|
251 |
+
<h3>${item.name} (x${item.quantity})</h3>
|
252 |
+
<p>Extras: ${item.extras.map(extra => `${extra.name} (+$${(extra.price * extra.quantity).toFixed(2)})`).join(', ') || 'None'}</p>
|
253 |
+
<p>Total: $${item.totalCost.toFixed(2)}</p>
|
|
|
|
|
|
|
|
|
254 |
</div>
|
255 |
`;
|
256 |
});
|
257 |
+
finalOrderHTML += `<h3>Total Bill: $${totalCartCost.toFixed(2)}</h3>`;
|
258 |
+
checkoutModal.innerHTML = finalOrderHTML;
|
259 |
+
checkoutModal.style.display = 'block';
|
260 |
+
closeCartModal(); // Close cart modal
|
261 |
}
|
262 |
</script>
|
263 |
"""
|
|
|
268 |
with gr.Row():
|
269 |
gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
|
270 |
|
|
|
271 |
with gr.Row(visible=True) as login_page:
|
272 |
with gr.Column():
|
273 |
login_email = gr.Textbox(label="Email")
|
|
|
276 |
signup_button = gr.Button("Go to Signup")
|
277 |
login_output = gr.Textbox(label="Status")
|
278 |
|
|
|
279 |
with gr.Row(visible=False) as signup_page:
|
280 |
with gr.Column():
|
281 |
signup_name = gr.Textbox(label="Name")
|
|
|
286 |
login_redirect = gr.Button("Go to Login")
|
287 |
signup_output = gr.Textbox(label="Status")
|
288 |
|
|
|
289 |
with gr.Row(visible=False) as menu_page:
|
290 |
with gr.Column():
|
291 |
preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
|
|
|
295 |
gr.HTML(create_modal_window())
|
296 |
gr.HTML(modal_js())
|
297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
login_button.click(
|
299 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
|
300 |
if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
|
301 |
[login_email, login_password], [login_page, menu_page, menu_output, login_output]
|
302 |
)
|
|
|
303 |
preference.change(lambda pref: filter_menu(pref), [preference], menu_output)
|
304 |
|
305 |
app.launch()
|