nagasurendra commited on
Commit
702d48f
·
verified ·
1 Parent(s): 93820c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -306,6 +306,9 @@ def navigate_to_login():
306
 
307
  def app():
308
  with gr.Blocks() as demo:
 
 
 
309
  # Login Page
310
  with gr.Column(visible=True) as login_section:
311
  gr.Markdown("# Login Page")
@@ -342,7 +345,6 @@ def app():
342
 
343
  # "View Cart" button
344
  view_cart_button = gr.Button("View Cart")
345
-
346
 
347
  # Modal window
348
  modal_window = gr.HTML("""
@@ -375,6 +377,8 @@ def app():
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
 
@@ -383,7 +387,6 @@ def app():
383
  gr.Row(menu_output)
384
  gr.Row(view_cart_button)
385
  gr.Row(modal_window)
386
- gr.HTML(modal_and_cart_js)
387
 
388
  with gr.Column(visible=False) as cart_section:
389
  gr.Markdown("### Cart Page")
@@ -415,10 +418,23 @@ def app():
415
  lambda: (gr.update(visible=True), gr.update(visible=False)),
416
  outputs=[login_section, signup_section],
417
  )
 
 
 
 
 
418
  # Navigation from "View Cart" button to Cart Page
 
 
 
 
 
 
 
419
  view_cart_button.click(
420
- lambda: (gr.update(visible=False), gr.update(visible=True)),
421
- outputs=[menu_section, cart_section],
 
422
  )
423
 
424
  # Navigation from Cart Page back to Menu Page
@@ -426,7 +442,6 @@ def app():
426
  lambda: (gr.update(visible=True), gr.update(visible=False)),
427
  outputs=[menu_section, cart_section],
428
  )
429
-
430
  return demo
431
 
432
 
 
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")
 
345
 
346
  # "View Cart" button
347
  view_cart_button = gr.Button("View Cart")
 
348
 
349
  # Modal window
350
  modal_window = gr.HTML("""
 
377
  <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>
378
  </div>
379
  """)
380
+
381
+ gr.HTML(modal_and_cart_js)
382
  # Update menu dynamically based on preference
383
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
384
 
 
387
  gr.Row(menu_output)
388
  gr.Row(view_cart_button)
389
  gr.Row(modal_window)
 
390
 
391
  with gr.Column(visible=False) as cart_section:
392
  gr.Markdown("### Cart Page")
 
418
  lambda: (gr.update(visible=True), gr.update(visible=False)),
419
  outputs=[login_section, signup_section],
420
  )
421
+ # Add item to cart logic
422
+ def add_item_to_cart(item, cart):
423
+ cart.append(item) # Add the item to the cart
424
+ return cart
425
+
426
  # Navigation from "View Cart" button to Cart Page
427
+ def display_cart(cart):
428
+ if not cart:
429
+ return "Your cart is empty.", ""
430
+ cart_html = "<ul>" + "".join(f"<li>{item}</li>" for item in cart) + "</ul>"
431
+ final_html = f"<p>Items in cart: {len(cart)}</p>"
432
+ return cart_html, final_html
433
+
434
  view_cart_button.click(
435
+ lambda cart: (gr.update(visible=False), gr.update(visible=True), *display_cart(cart)),
436
+ inputs=[cart_state],
437
+ outputs=[menu_section, cart_section, cart_output, final_order_output],
438
  )
439
 
440
  # Navigation from Cart Page back to Menu Page
 
442
  lambda: (gr.update(visible=True), gr.update(visible=False)),
443
  outputs=[menu_section, cart_section],
444
  )
 
445
  return demo
446
 
447