Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -318,16 +318,16 @@ def app():
|
|
318 |
value="All",
|
319 |
label="Choose a Preference",
|
320 |
)
|
|
|
|
|
|
|
321 |
menu_output = gr.HTML(value=filter_menu("All"))
|
322 |
|
323 |
-
# Button to
|
324 |
-
view_cart_button = gr.
|
325 |
-
|
326 |
-
<button id="view-cart-button" style="background-color: #007bff; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;">View Cart</button>
|
327 |
-
"""
|
328 |
-
)
|
329 |
|
330 |
-
#
|
331 |
modal_window = gr.HTML("""
|
332 |
<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;">
|
333 |
<div style="text-align: right;">
|
@@ -358,62 +358,16 @@ def app():
|
|
358 |
<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>
|
359 |
</div>
|
360 |
""")
|
|
|
|
|
361 |
|
362 |
-
|
363 |
-
# Modal window for cart
|
364 |
-
modal_cart = gr.HTML(
|
365 |
-
"""
|
366 |
-
<div id="cart-modal" style="display: none; position: fixed; bottom: 0; left: 0; width: 100%; background: white; z-index: 1000; border-top: 2px solid #ccc; box-shadow: 0 -2px 5px rgba(0,0,0,0.1); transition: height 0.3s ease;">
|
367 |
-
<div style="padding: 10px; display: flex; justify-content: space-between; align-items: center;">
|
368 |
-
<span style="font-size: 18px; font-weight: bold;">Your Cart</span>
|
369 |
-
<button id="close-cart" onclick="toggleCartModal(false)" style="background: none; border: none; font-size: 20px; cursor: pointer;">×</button>
|
370 |
-
</div>
|
371 |
-
<div id="cart-details" style="padding: 10px; max-height: 300px; overflow-y: auto;">
|
372 |
-
<div id="cart-content">Your cart is empty.</div>
|
373 |
-
<div id="final-order" style="margin-top: 20px;"></div>
|
374 |
-
</div>
|
375 |
-
</div>
|
376 |
-
""",
|
377 |
-
visible=True,
|
378 |
-
)
|
379 |
-
|
380 |
-
# JavaScript for toggling the modal window
|
381 |
-
modal_js = gr.HTML(
|
382 |
-
"""
|
383 |
-
<script>
|
384 |
-
function toggleCartModal(show) {
|
385 |
-
const cartModal = document.getElementById('cart-modal');
|
386 |
-
if (show) {
|
387 |
-
const screenHeight = window.innerHeight;
|
388 |
-
cartModal.style.display = 'block';
|
389 |
-
cartModal.style.height = Math.min(screenHeight * 0.7, 500) + 'px'; // Max 70% of screen or 500px
|
390 |
-
cartModal.style.position = 'fixed';
|
391 |
-
} else {
|
392 |
-
cartModal.style.display = 'none';
|
393 |
-
}
|
394 |
-
}
|
395 |
-
|
396 |
-
document.getElementById('view-cart-button').onclick = function() {
|
397 |
-
toggleCartModal(true);
|
398 |
-
};
|
399 |
-
|
400 |
-
document.getElementById('close-cart').onclick = function() {
|
401 |
-
toggleCartModal(false);
|
402 |
-
};
|
403 |
-
</script>
|
404 |
-
|
405 |
-
"""
|
406 |
-
)
|
407 |
-
|
408 |
gr.Row([selected_preference])
|
409 |
gr.Row(menu_output)
|
410 |
-
gr.Row(view_cart_button)
|
411 |
gr.Row(modal_window)
|
412 |
gr.HTML(modal_and_cart_js)
|
413 |
-
gr.Row(modal_cart)
|
414 |
-
gr.Row(modal_js)
|
415 |
|
416 |
-
|
417 |
with gr.Column(visible=False) as cart_section:
|
418 |
gr.Markdown("### Cart & Final Order Page")
|
419 |
|
@@ -428,30 +382,47 @@ def app():
|
|
428 |
|
429 |
gr.Row(cart_output)
|
430 |
gr.Row(final_order_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
431 |
|
432 |
# Button Bindings
|
|
|
433 |
login_button.click(
|
434 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), "") if check_credentials(email, password) else (gr.update(), gr.update(), "Invalid email or password."),
|
435 |
inputs=[login_email, login_password],
|
436 |
outputs=[login_section, menu_section, login_error],
|
437 |
)
|
438 |
-
|
439 |
signup_button.click(
|
440 |
lambda name, phone, email, password: ("Signup successful! Please login.", gr.update(visible=True), gr.update(visible=False)) if save_user(name, phone, email, password) else ("Email already exists.", gr.update(visible=False), gr.update(visible=True)),
|
441 |
inputs=[signup_name, signup_phone, signup_email, signup_password],
|
442 |
outputs=[signup_message, login_section, signup_section],
|
443 |
)
|
444 |
-
|
445 |
go_to_signup.click(
|
446 |
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
447 |
outputs=[login_section, signup_section],
|
448 |
)
|
449 |
-
|
450 |
go_to_login.click(
|
451 |
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
452 |
outputs=[login_section, signup_section],
|
453 |
)
|
454 |
-
|
|
|
|
|
|
|
|
|
|
|
455 |
back_to_menu_button.click(
|
456 |
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
457 |
outputs=[menu_section, cart_section],
|
|
|
318 |
value="All",
|
319 |
label="Choose a Preference",
|
320 |
)
|
321 |
+
empty_div = gr.HTML('<div style="height: 20px;"></div>')
|
322 |
+
|
323 |
+
# Output area for menu items
|
324 |
menu_output = gr.HTML(value=filter_menu("All"))
|
325 |
|
326 |
+
# Button to navigate to Cart Page
|
327 |
+
view_cart_button = gr.Button("View Cart")
|
328 |
+
empty_div = gr.HTML('<div style="height: 300px;"></div>')
|
|
|
|
|
|
|
329 |
|
330 |
+
# Modal window
|
331 |
modal_window = gr.HTML("""
|
332 |
<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;">
|
333 |
<div style="text-align: right;">
|
|
|
358 |
<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>
|
359 |
</div>
|
360 |
""")
|
361 |
+
# Update menu dynamically based on preference
|
362 |
+
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
|
363 |
|
364 |
+
# Layout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
gr.Row([selected_preference])
|
366 |
gr.Row(menu_output)
|
|
|
367 |
gr.Row(modal_window)
|
368 |
gr.HTML(modal_and_cart_js)
|
|
|
|
|
369 |
|
370 |
+
# Cart & Final Order Page
|
371 |
with gr.Column(visible=False) as cart_section:
|
372 |
gr.Markdown("### Cart & Final Order Page")
|
373 |
|
|
|
382 |
|
383 |
gr.Row(cart_output)
|
384 |
gr.Row(final_order_output)
|
385 |
+
# JavaScript to Scroll to Top on Page Change
|
386 |
+
scroll_to_top_js = gr.HTML("""
|
387 |
+
<script>
|
388 |
+
function scrollToTop() {
|
389 |
+
window.scrollTo({ top: 0, behavior: 'smooth' });
|
390 |
+
}
|
391 |
+
</script>
|
392 |
+
""")
|
393 |
+
|
394 |
+
# Include JavaScript in the App
|
395 |
+
demo.add_component(scroll_to_top_js)
|
396 |
|
397 |
# Button Bindings
|
398 |
+
# Login Button
|
399 |
login_button.click(
|
400 |
lambda email, password: (gr.update(visible=False), gr.update(visible=True), "") if check_credentials(email, password) else (gr.update(), gr.update(), "Invalid email or password."),
|
401 |
inputs=[login_email, login_password],
|
402 |
outputs=[login_section, menu_section, login_error],
|
403 |
)
|
404 |
+
# Signup Button
|
405 |
signup_button.click(
|
406 |
lambda name, phone, email, password: ("Signup successful! Please login.", gr.update(visible=True), gr.update(visible=False)) if save_user(name, phone, email, password) else ("Email already exists.", gr.update(visible=False), gr.update(visible=True)),
|
407 |
inputs=[signup_name, signup_phone, signup_email, signup_password],
|
408 |
outputs=[signup_message, login_section, signup_section],
|
409 |
)
|
410 |
+
# Navigate to Signup Page
|
411 |
go_to_signup.click(
|
412 |
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
413 |
outputs=[login_section, signup_section],
|
414 |
)
|
415 |
+
# Navigate Back to Login Page
|
416 |
go_to_login.click(
|
417 |
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
418 |
outputs=[login_section, signup_section],
|
419 |
)
|
420 |
+
# Navigate to Cart Page
|
421 |
+
view_cart_button.click(
|
422 |
+
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
423 |
+
outputs=[menu_section, cart_section],
|
424 |
+
)
|
425 |
+
# Navigate Back to Menu Page
|
426 |
back_to_menu_button.click(
|
427 |
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
428 |
outputs=[menu_section, cart_section],
|