Rammohan0504 commited on
Commit
add0ac3
·
verified ·
1 Parent(s): 502df1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -47,7 +47,7 @@ def display_dishes(category="ALL"):
47
 
48
  # Main Gradio App
49
  with gr.Blocks() as demo:
50
- cart = [] # Initialize cart as a plain list
51
 
52
  gr.HTML("<h1 style='text-align: center;'>Dynamic Menu with Cart</h1>")
53
 
@@ -59,19 +59,22 @@ with gr.Blocks() as demo:
59
  dish_display = gr.HTML(value=display_dishes())
60
  cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
61
 
62
- btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
63
- btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
64
- btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
65
 
66
  def handle_add_to_cart(cart, dish_name):
67
  cart.append(dish_name)
68
  return cart, update_cart(cart)
69
 
70
- gr.Interface(
71
- fn=add_to_cart,
72
- inputs=[gr.Textbox(label="Cart", lines=2, value="[]"), gr.Textbox(label="Dish Name")],
73
- outputs=[gr.Textbox(label="Updated Cart"), cart_display]
74
- ).launch()
 
 
 
75
 
76
  gr.HTML("""
77
  <div id='cart-icon' style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
@@ -114,14 +117,9 @@ with gr.Blocks() as demo:
114
 
115
  function addItemToCart() {
116
  closeMenuPopup();
117
- fetch('/run/predict', {
118
- method: 'POST',
119
- headers: { 'Content-Type': 'application/json' },
120
- body: JSON.stringify({ data: [selectedItem] })
121
- }).then(response => response.json()).then(data => {
122
- document.getElementById('cart-content').innerHTML = data.data[1];
123
- alert(selectedItem + ' has been added to the cart!');
124
- });
125
  }
126
  </script>
127
  """)
 
47
 
48
  # Main Gradio App
49
  with gr.Blocks() as demo:
50
+ cart = gr.State([]) # Use gr.State to manage cart
51
 
52
  gr.HTML("<h1 style='text-align: center;'>Dynamic Menu with Cart</h1>")
53
 
 
59
  dish_display = gr.HTML(value=display_dishes())
60
  cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
61
 
62
+ btn_all.click(lambda: display_dishes("ALL"), outputs=dish_display)
63
+ btn_vegan.click(lambda: display_dishes("VEGAN"), outputs=dish_display)
64
+ btn_halal.click(lambda: display_dishes("HALAL"), outputs=dish_display)
65
 
66
  def handle_add_to_cart(cart, dish_name):
67
  cart.append(dish_name)
68
  return cart, update_cart(cart)
69
 
70
+ add_to_cart_button = gr.Button("Add to Cart")
71
+ selected_item = gr.Textbox(label="Selected Item", visible=False)
72
+
73
+ add_to_cart_button.click(
74
+ handle_add_to_cart,
75
+ inputs=[cart, selected_item],
76
+ outputs=[cart, cart_display]
77
+ )
78
 
79
  gr.HTML("""
80
  <div id='cart-icon' style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
 
117
 
118
  function addItemToCart() {
119
  closeMenuPopup();
120
+ const inputBox = gradioApp().querySelector("#component-0 input");
121
+ inputBox.value = selectedItem;
122
+ gradioApp().querySelector("#component-1 button").click();
 
 
 
 
 
123
  }
124
  </script>
125
  """)