Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -303,12 +303,10 @@ def create_account(name, phone, email, password):
|
|
| 303 |
|
| 304 |
def navigate_to_login():
|
| 305 |
return gr.update(visible=True), gr.update(visible=False)
|
| 306 |
-
|
|
|
|
| 307 |
def app():
|
| 308 |
with gr.Blocks() as demo:
|
| 309 |
-
# Shared state for cart
|
| 310 |
-
cart_state = gr.State([]) # Initialize an empty list for cart items
|
| 311 |
-
|
| 312 |
# Login Page
|
| 313 |
with gr.Column(visible=True) as login_section:
|
| 314 |
gr.Markdown("# Login Page")
|
|
@@ -343,12 +341,9 @@ def app():
|
|
| 343 |
# Output area for menu items
|
| 344 |
menu_output = gr.HTML(value=filter_menu("All"))
|
| 345 |
|
| 346 |
-
#
|
| 347 |
-
add_item_button = gr.Button("Add Item to Cart")
|
| 348 |
-
|
| 349 |
-
# "View Cart" button
|
| 350 |
view_cart_button = gr.Button("View Cart")
|
| 351 |
-
|
| 352 |
# Modal window
|
| 353 |
modal_window = gr.HTML("""
|
| 354 |
<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;">
|
|
@@ -380,74 +375,60 @@ def app():
|
|
| 380 |
<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>
|
| 381 |
</div>
|
| 382 |
""")
|
| 383 |
-
|
| 384 |
-
gr.HTML(modal_and_cart_js)
|
| 385 |
# Update menu dynamically based on preference
|
| 386 |
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
|
| 387 |
|
| 388 |
# Layout
|
| 389 |
gr.Row([selected_preference])
|
| 390 |
gr.Row(menu_output)
|
| 391 |
-
gr.Row(view_cart_button)
|
| 392 |
gr.Row(modal_window)
|
|
|
|
| 393 |
|
|
|
|
| 394 |
with gr.Column(visible=False) as cart_section:
|
| 395 |
-
gr.Markdown("### Cart Page")
|
| 396 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
final_order_output = gr.HTML(value="", elem_id="final-order")
|
|
|
|
|
|
|
| 398 |
back_to_menu_button = gr.Button("Back to Menu")
|
| 399 |
|
| 400 |
gr.Row(cart_output)
|
| 401 |
gr.Row(final_order_output)
|
| 402 |
-
gr.Row(back_to_menu_button)
|
| 403 |
|
| 404 |
# Button Bindings
|
| 405 |
-
# Button
|
| 406 |
login_button.click(
|
| 407 |
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."),
|
| 408 |
inputs=[login_email, login_password],
|
| 409 |
outputs=[login_section, menu_section, login_error],
|
| 410 |
)
|
| 411 |
-
|
| 412 |
-
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
| 413 |
-
outputs=[login_section, signup_section],
|
| 414 |
-
)
|
| 415 |
signup_button.click(
|
| 416 |
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)),
|
| 417 |
inputs=[signup_name, signup_phone, signup_email, signup_password],
|
| 418 |
outputs=[signup_message, login_section, signup_section],
|
| 419 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
go_to_login.click(
|
| 421 |
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
| 422 |
outputs=[login_section, signup_section],
|
| 423 |
)
|
| 424 |
-
#
|
| 425 |
-
def add_item_to_cart(cart):
|
| 426 |
-
item = "Sample Item" # Simulate an item being added
|
| 427 |
-
cart.append(item)
|
| 428 |
-
return cart
|
| 429 |
-
|
| 430 |
-
add_item_button.click(
|
| 431 |
-
add_item_to_cart,
|
| 432 |
-
inputs=[cart_state],
|
| 433 |
-
outputs=[cart_state],
|
| 434 |
-
)
|
| 435 |
-
|
| 436 |
-
# Navigation from "View Cart" button to Cart Page
|
| 437 |
-
def display_cart(cart):
|
| 438 |
-
if not cart:
|
| 439 |
-
return "Your cart is empty.", ""
|
| 440 |
-
cart_html = "<ul>" + "".join(f"<li>{item}</li>" for item in cart) + "</ul>"
|
| 441 |
-
final_html = f"<p>Total items: {len(cart)}</p>"
|
| 442 |
-
return cart_html, final_html
|
| 443 |
-
|
| 444 |
view_cart_button.click(
|
| 445 |
-
lambda
|
| 446 |
-
|
| 447 |
-
outputs=[menu_section, cart_section, cart_output, final_order_output],
|
| 448 |
)
|
| 449 |
-
|
| 450 |
-
# Navigation from Cart Page back to Menu Page
|
| 451 |
back_to_menu_button.click(
|
| 452 |
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
| 453 |
outputs=[menu_section, cart_section],
|
|
@@ -455,6 +436,5 @@ def app():
|
|
| 455 |
|
| 456 |
return demo
|
| 457 |
|
| 458 |
-
|
| 459 |
if __name__ == "__main__":
|
| 460 |
-
app().launch()
|
|
|
|
| 303 |
|
| 304 |
def navigate_to_login():
|
| 305 |
return gr.update(visible=True), gr.update(visible=False)
|
| 306 |
+
|
| 307 |
+
# Gradio App
|
| 308 |
def app():
|
| 309 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
| 310 |
# Login Page
|
| 311 |
with gr.Column(visible=True) as login_section:
|
| 312 |
gr.Markdown("# Login Page")
|
|
|
|
| 341 |
# Output area for menu items
|
| 342 |
menu_output = gr.HTML(value=filter_menu("All"))
|
| 343 |
|
| 344 |
+
# Button to navigate to Cart Page
|
|
|
|
|
|
|
|
|
|
| 345 |
view_cart_button = gr.Button("View Cart")
|
| 346 |
+
|
| 347 |
# Modal window
|
| 348 |
modal_window = gr.HTML("""
|
| 349 |
<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;">
|
|
|
|
| 375 |
<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>
|
| 376 |
</div>
|
| 377 |
""")
|
|
|
|
|
|
|
| 378 |
# Update menu dynamically based on preference
|
| 379 |
selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
|
| 380 |
|
| 381 |
# Layout
|
| 382 |
gr.Row([selected_preference])
|
| 383 |
gr.Row(menu_output)
|
|
|
|
| 384 |
gr.Row(modal_window)
|
| 385 |
+
gr.HTML(modal_and_cart_js)
|
| 386 |
|
| 387 |
+
# Cart & Final Order Page
|
| 388 |
with gr.Column(visible=False) as cart_section:
|
| 389 |
+
gr.Markdown("### Cart & Final Order Page")
|
| 390 |
+
|
| 391 |
+
# Floating cart display
|
| 392 |
+
cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
|
| 393 |
+
|
| 394 |
+
# Final order display
|
| 395 |
final_order_output = gr.HTML(value="", elem_id="final-order")
|
| 396 |
+
|
| 397 |
+
# Button to navigate back to Menu Page
|
| 398 |
back_to_menu_button = gr.Button("Back to Menu")
|
| 399 |
|
| 400 |
gr.Row(cart_output)
|
| 401 |
gr.Row(final_order_output)
|
|
|
|
| 402 |
|
| 403 |
# Button Bindings
|
| 404 |
+
# Login Button
|
| 405 |
login_button.click(
|
| 406 |
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."),
|
| 407 |
inputs=[login_email, login_password],
|
| 408 |
outputs=[login_section, menu_section, login_error],
|
| 409 |
)
|
| 410 |
+
# Signup Button
|
|
|
|
|
|
|
|
|
|
| 411 |
signup_button.click(
|
| 412 |
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)),
|
| 413 |
inputs=[signup_name, signup_phone, signup_email, signup_password],
|
| 414 |
outputs=[signup_message, login_section, signup_section],
|
| 415 |
)
|
| 416 |
+
# Navigate to Signup Page
|
| 417 |
+
go_to_signup.click(
|
| 418 |
+
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
| 419 |
+
outputs=[login_section, signup_section],
|
| 420 |
+
)
|
| 421 |
+
# Navigate Back to Login Page
|
| 422 |
go_to_login.click(
|
| 423 |
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
| 424 |
outputs=[login_section, signup_section],
|
| 425 |
)
|
| 426 |
+
# Navigate to Cart Page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
view_cart_button.click(
|
| 428 |
+
lambda: (gr.update(visible=False), gr.update(visible=True)),
|
| 429 |
+
outputs=[menu_section, cart_section],
|
|
|
|
| 430 |
)
|
| 431 |
+
# Navigate Back to Menu Page
|
|
|
|
| 432 |
back_to_menu_button.click(
|
| 433 |
lambda: (gr.update(visible=True), gr.update(visible=False)),
|
| 434 |
outputs=[menu_section, cart_section],
|
|
|
|
| 436 |
|
| 437 |
return demo
|
| 438 |
|
|
|
|
| 439 |
if __name__ == "__main__":
|
| 440 |
+
app().launch()
|