Rammohan0504 commited on
Commit
0ffa58b
·
verified ·
1 Parent(s): d569433

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -32
app.py CHANGED
@@ -7,20 +7,17 @@ menu_data = [
7
  # Add other items as needed...
8
  ]
9
 
10
- # Cart data (persistent across sessions)
11
- cart_items = []
12
-
13
  # Function to handle adding items to the cart
14
- def add_to_cart(dish_name):
15
- cart_items.append(dish_name)
16
- return update_cart()
17
 
18
  # Function to update the cart display
19
- def update_cart():
20
- if not cart_items:
21
  return "<p>Your cart is empty.</p>"
22
  html_content = "<h3>Cart Items</h3><ul>"
23
- for item in cart_items:
24
  html_content += f"<li>{item}</li>"
25
  html_content += "</ul><button onclick='alert(\'Order placed!\')' style='padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px;'>Place Order</button>"
26
  return html_content
@@ -50,6 +47,8 @@ def display_dishes(category="ALL"):
50
 
51
  # Main Gradio App
52
  with gr.Blocks() as demo:
 
 
53
  gr.HTML("<h1 style='text-align: center;'>Dynamic Menu with Cart</h1>")
54
 
55
  with gr.Row():
@@ -58,34 +57,28 @@ with gr.Blocks() as demo:
58
  btn_halal = gr.Button("HALAL")
59
 
60
  dish_display = gr.HTML(value=display_dishes())
 
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
- cart_content = gr.HTML(value=update_cart())
67
-
68
  gr.HTML("""
69
  <div id='cart-icon' style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
70
  <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/Shopping_cart_icon.svg/512px-Shopping_cart_icon.svg.png' alt='Cart' style='width: 40px;' onclick='toggleCartPopup()'>
71
  </div>
72
  <div id='cart-popup' style='display: none; position: fixed; top: 50px; right: 10px; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);'>
73
- <div id='cart-content'></div>
 
 
 
 
74
  <button onclick='toggleCartPopup()' style='margin-top: 10px; padding: 5px 10px; background-color: #007bff; color: white; border: none; border-radius: 3px;'>Close</button>
75
  </div>
76
  <script>
77
  function toggleCartPopup() {
78
  const popup = document.getElementById('cart-popup');
79
  popup.style.display = popup.style.display === 'block' ? 'none' : 'block';
80
- fetch('/run/predict', {
81
- method: 'POST',
82
- headers: { 'Content-Type': 'application/json' },
83
- body: JSON.stringify({ data: [] })
84
- })
85
- .then(response => response.json())
86
- .then(data => {
87
- document.getElementById('cart-content').innerHTML = data.data[0];
88
- });
89
  }
90
  </script>
91
  """)
@@ -118,19 +111,9 @@ with gr.Blocks() as demo:
118
  function addItemToCart() {
119
  closeMenuPopup();
120
  alert('Item added to cart!');
121
- fetch('/run/predict', {
122
- method: 'POST',
123
- headers: { 'Content-Type': 'application/json' },
124
- body: JSON.stringify({ data: [selectedItem] })
125
- })
126
- .then(response => response.json())
127
- .then(data => {
128
- document.getElementById('cart-content').innerHTML = data.data[0];
129
- });
130
  }
131
  </script>
132
  """)
133
 
134
- cart_content.update(lambda: update_cart())
135
-
136
  demo.launch()
 
7
  # Add other items as needed...
8
  ]
9
 
 
 
 
10
  # Function to handle adding items to the cart
11
+ def add_to_cart(cart, dish_name):
12
+ cart.append(dish_name)
13
+ return cart, update_cart(cart)
14
 
15
  # Function to update the cart display
16
+ def update_cart(cart):
17
+ if not cart:
18
  return "<p>Your cart is empty.</p>"
19
  html_content = "<h3>Cart Items</h3><ul>"
20
+ for item in cart:
21
  html_content += f"<li>{item}</li>"
22
  html_content += "</ul><button onclick='alert(\'Order placed!\')' style='padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px;'>Place Order</button>"
23
  return html_content
 
47
 
48
  # Main Gradio App
49
  with gr.Blocks() as demo:
50
+ cart = gr.State([])
51
+
52
  gr.HTML("<h1 style='text-align: center;'>Dynamic Menu with Cart</h1>")
53
 
54
  with gr.Row():
 
57
  btn_halal = gr.Button("HALAL")
58
 
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
  gr.HTML("""
67
  <div id='cart-icon' style='position: fixed; top: 10px; right: 10px; cursor: pointer;'>
68
  <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/Shopping_cart_icon.svg/512px-Shopping_cart_icon.svg.png' alt='Cart' style='width: 40px;' onclick='toggleCartPopup()'>
69
  </div>
70
  <div id='cart-popup' style='display: none; position: fixed; top: 50px; right: 10px; background: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);'>
71
+ <div id='cart-content'>
72
+ <div style='overflow-y: auto;'>
73
+ <div>{cart_display}</div>
74
+ </div>
75
+ </div>
76
  <button onclick='toggleCartPopup()' style='margin-top: 10px; padding: 5px 10px; background-color: #007bff; color: white; border: none; border-radius: 3px;'>Close</button>
77
  </div>
78
  <script>
79
  function toggleCartPopup() {
80
  const popup = document.getElementById('cart-popup');
81
  popup.style.display = popup.style.display === 'block' ? 'none' : 'block';
 
 
 
 
 
 
 
 
 
82
  }
83
  </script>
84
  """)
 
111
  function addItemToCart() {
112
  closeMenuPopup();
113
  alert('Item added to cart!');
114
+ gradioApp().querySelector("#component-0 button").click();
 
 
 
 
 
 
 
 
115
  }
116
  </script>
117
  """)
118
 
 
 
119
  demo.launch()