Rammohan0504 commited on
Commit
3a6f9b5
·
verified ·
1 Parent(s): fa75506

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- import json
3
 
4
  # Global variables for cart and menu data
5
  cart = []
@@ -20,7 +19,7 @@ menu_data = [
20
  ]
21
 
22
  # Function to add an item to the cart
23
- def add_to_cart(item_name: str):
24
  cart.append({"item_name": item_name})
25
  return f"'{item_name}' has been added to your cart!"
26
 
@@ -57,7 +56,7 @@ def display_dishes(category):
57
  html_content += "</div>"
58
  return html_content
59
 
60
- # Popup JavaScript for handling the modal
61
  popup_js = """
62
  <script>
63
  function showPopup(name, description, price, image) {
@@ -75,16 +74,28 @@ popup_js = """
75
  `;
76
  popup.style.display = "block";
77
  overlay.style.display = "block";
78
-
79
- document.getElementById("add-to-cart-button").onclick = async function() {
80
- const response = await fetch("/run/add_to_cart", {
81
- method: "POST",
82
- body: JSON.stringify({ data: name }),
83
- headers: { "Content-Type": "application/json" }
84
- });
85
- const result = await response.json();
86
- alert(result.message); // Show success message
87
- closePopup(); // Automatically close popup
 
 
 
 
 
 
 
 
 
 
 
 
88
  };
89
  }
90
 
@@ -95,7 +106,7 @@ popup_js = """
95
  </script>
96
  """
97
 
98
- # Popup HTML structure
99
  popup_html = """
100
  <div id="overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; z-index: 999;"></div>
101
  <div id="custom-popup"
@@ -103,7 +114,7 @@ popup_html = """
103
  </div>
104
  """
105
 
106
- # Main Gradio App
107
  with gr.Blocks() as demo:
108
  gr.HTML(popup_html) # Add popup container
109
  gr.HTML(popup_js) # Add popup JavaScript
@@ -142,4 +153,8 @@ with gr.Blocks() as demo:
142
  outputs=cart_display
143
  )
144
 
 
 
 
145
  demo.launch()
 
 
1
  import gradio as gr
 
2
 
3
  # Global variables for cart and menu data
4
  cart = []
 
19
  ]
20
 
21
  # Function to add an item to the cart
22
+ def add_to_cart(item_name):
23
  cart.append({"item_name": item_name})
24
  return f"'{item_name}' has been added to your cart!"
25
 
 
56
  html_content += "</div>"
57
  return html_content
58
 
59
+ # JavaScript for handling the popup
60
  popup_js = """
61
  <script>
62
  function showPopup(name, description, price, image) {
 
74
  `;
75
  popup.style.display = "block";
76
  overlay.style.display = "block";
77
+
78
+ document.getElementById("add-to-cart-button").onclick = async function () {
79
+ try {
80
+ const response = await fetch("/run/add_to_cart", {
81
+ method: "POST",
82
+ body: JSON.stringify({ name }),
83
+ headers: { "Content-Type": "application/json" },
84
+ });
85
+
86
+ const result = await response.json();
87
+
88
+ if (response.ok) {
89
+ alert(result); // Show success message
90
+ } else {
91
+ alert("Error adding item to cart: " + (result.error || "Unknown error"));
92
+ }
93
+ } catch (error) {
94
+ console.error("Error:", error);
95
+ alert("Something went wrong while adding the item.");
96
+ }
97
+
98
+ closePopup();
99
  };
100
  }
101
 
 
106
  </script>
107
  """
108
 
109
+ # HTML structure for the popup
110
  popup_html = """
111
  <div id="overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; z-index: 999;"></div>
112
  <div id="custom-popup"
 
114
  </div>
115
  """
116
 
117
+ # Main Gradio app
118
  with gr.Blocks() as demo:
119
  gr.HTML(popup_html) # Add popup container
120
  gr.HTML(popup_js) # Add popup JavaScript
 
153
  outputs=cart_display
154
  )
155
 
156
+ # Register backend functions
157
+ demo.load(add_to_cart, inputs="text", outputs="text")
158
+
159
  demo.launch()
160
+