Rammohan0504 commited on
Commit
9949293
·
verified ·
1 Parent(s): 1f66497

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -34
app.py CHANGED
@@ -66,7 +66,7 @@ def add_to_cart(dish_name, spice_level, selected_extras, quantity, custom_descri
66
  "quantity": quantity,
67
  "custom_description": custom_description
68
  })
69
- return update_cart()
70
 
71
  # Update cart display
72
  def update_cart():
@@ -96,7 +96,20 @@ with gr.Blocks() as demo:
96
  with gr.Tab("Cart"):
97
  cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
98
 
99
- # JavaScript for managing the pop-up
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  gr.HTML(f"""
101
  <div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
102
  background-color: white; padding: 20px; border-radius: 10px;
@@ -123,9 +136,6 @@ with gr.Blocks() as demo:
123
  <textarea id="custom-description" rows="2" style="width: 100%; padding: 5px; border-radius: 5px; border: 1px solid #ddd;"></textarea>
124
  <button onclick="addItemToCart()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Add to Cart</button>
125
  </div>
126
- <div id="notification" style="display: none; position: fixed; bottom: 20px; right: 20px; background-color: #4caf50; color: white; padding: 15px; border-radius: 5px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1000;">
127
- Your item has been added to the cart!
128
- </div>
129
  <script>
130
  let selectedDish = "";
131
  let selectedExtras = [];
@@ -160,37 +170,15 @@ with gr.Blocks() as demo:
160
  const customDescription = document.getElementById('custom-description').value || "None";
161
 
162
  // Pass to Gradio backend
163
- fetch("/run/add_to_cart", {
164
- method: "POST",
165
- headers: {"Content-Type": "application/json"},
166
- body: JSON.stringify({
167
- dish_name: selectedDish,
168
- spice_level: spiceLevel,
169
- selected_extras: selectedExtras,
170
- quantity: quantity,
171
- custom_description: customDescription
172
- })
173
- }).then(response => response.json()).then(data => {
174
- document.querySelector("#cart-display").innerHTML = data.html;
175
- showNotification();
176
- closePopup();
177
- });
178
- }
179
-
180
- function showNotification() {
181
- const notification = document.getElementById('notification');
182
- notification.style.display = 'block';
183
- setTimeout(() => {
184
- notification.style.display = 'none';
185
- }, 3000);
186
  }
187
  </script>
188
  """)
189
 
190
- # Button click events
191
- btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
192
- btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
193
- btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
194
-
195
-
196
  demo.launch()
 
66
  "quantity": quantity,
67
  "custom_description": custom_description
68
  })
69
+ return update_cart(), "Your item has been added to the cart!"
70
 
71
  # Update cart display
72
  def update_cart():
 
96
  with gr.Tab("Cart"):
97
  cart_display = gr.HTML(value="<p>Your cart is empty.</p>")
98
 
99
+ notification = gr.Textbox(visible=False)
100
+
101
+ # Button click events
102
+ btn_all.click(lambda: gr.update(value=display_dishes("ALL")), outputs=dish_display)
103
+ btn_vegan.click(lambda: gr.update(value=display_dishes("VEGAN")), outputs=dish_display)
104
+ btn_halal.click(lambda: gr.update(value=display_dishes("HALAL")), outputs=dish_display)
105
+
106
+ add_to_cart_button = gr.Button("Add to Cart", visible=False)
107
+ add_to_cart_button.click(
108
+ add_to_cart,
109
+ inputs=[gr.Textbox(), gr.Textbox(), gr.Textbox(), gr.Number(), gr.Textbox()],
110
+ outputs=[cart_display, notification]
111
+ )
112
+
113
  gr.HTML(f"""
114
  <div id="popup" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
115
  background-color: white; padding: 20px; border-radius: 10px;
 
136
  <textarea id="custom-description" rows="2" style="width: 100%; padding: 5px; border-radius: 5px; border: 1px solid #ddd;"></textarea>
137
  <button onclick="addItemToCart()" style="margin-top: 20px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Add to Cart</button>
138
  </div>
 
 
 
139
  <script>
140
  let selectedDish = "";
141
  let selectedExtras = [];
 
170
  const customDescription = document.getElementById('custom-description').value || "None";
171
 
172
  // Pass to Gradio backend
173
+ gradioApp().querySelector("#component-0 input").value = selectedDish;
174
+ gradioApp().querySelector("#component-1 input").value = spiceLevel;
175
+ gradioApp().querySelector("#component-2 input").value = JSON.stringify(selectedExtras);
176
+ gradioApp().querySelector("#component-3 input").value = quantity;
177
+ gradioApp().querySelector("#component-4 input").value = customDescription;
178
+ gradioApp().querySelector("#component-5 button").click();
179
+ closePopup();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  }
181
  </script>
182
  """)
183
 
 
 
 
 
 
 
184
  demo.launch()