nagasurendra commited on
Commit
2653a35
·
verified ·
1 Parent(s): b718f7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -27
app.py CHANGED
@@ -9,22 +9,21 @@ def load_menu():
9
  except Exception as e:
10
  raise ValueError(f"Error loading menu file: {e}")
11
 
 
 
 
12
  # Function to filter menu items based on preference
13
  def filter_menu(preference):
14
- # Load menu data
15
  menu_data = load_menu()
16
-
17
- # Define filter conditions
18
  if preference == "Halal/Non-Veg":
19
  filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
20
  elif preference == "Vegetarian":
21
  filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
22
  elif preference == "Guilt-Free":
23
  filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
24
- else: # Default to "All"
25
  filtered_data = menu_data
26
 
27
- # Generate HTML for the menu
28
  html_content = ""
29
  for _, item in filtered_data.iterrows():
30
  html_content += f"""
@@ -42,6 +41,19 @@ def filter_menu(preference):
42
  """
43
  return html_content
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Gradio app definition
46
  def app():
47
  with gr.Blocks() as demo:
@@ -57,6 +69,9 @@ def app():
57
  # Output area for menu items
58
  menu_output = gr.HTML(value=filter_menu("All"))
59
 
 
 
 
60
  # Modal window
61
  modal_window = gr.HTML("""
62
  <div id="modal" style="display: none; position: fixed; top: 40%; left: 50%; transform: translate(-50%, -40%); width: 50%; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
@@ -102,7 +117,6 @@ def app():
102
  <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>
103
  </div>
104
  <script>
105
- // A placeholder for the cart items
106
  let cart = [];
107
 
108
  function openModal(name, image, description, price) {
@@ -118,36 +132,23 @@ def app():
118
  }
119
 
120
  function addToCart() {
121
- // Get dish details
122
  const name = document.getElementById('modal-name').innerText;
123
  const price = document.getElementById('modal-price').innerText;
124
  const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
125
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
126
  const instructions = document.getElementById('special-instructions').value;
127
-
128
- // Get selected extras
129
  const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
130
 
131
- // Create a cart item object
132
- const cartItem = {
133
- name,
134
- price,
135
- spiceLevel,
136
- quantity,
137
- instructions,
138
- extras
139
- };
140
-
141
- // Add to cart
142
  cart.push(cartItem);
143
 
144
- // Update the cart display (for demo purposes, we'll log it in the console)
145
- console.log("Cart:", cart);
 
 
 
146
 
147
- // Optionally, show a confirmation message
148
  alert(`${name} added to cart!`);
149
-
150
- // Close the modal
151
  closeModal();
152
  }
153
  </script>
@@ -156,14 +157,14 @@ def app():
156
  # Interactivity
157
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
158
 
159
- # Layout
160
  gr.Row([selected_preference])
161
  gr.Row(menu_output)
 
162
  gr.Row(modal_window)
163
 
164
  return demo
165
 
166
- # Run the app
167
  if __name__ == "__main__":
168
  demo = app()
169
  demo.launch()
 
9
  except Exception as e:
10
  raise ValueError(f"Error loading menu file: {e}")
11
 
12
+ # Initialize cart globally
13
+ cart_items = []
14
+
15
  # Function to filter menu items based on preference
16
  def filter_menu(preference):
 
17
  menu_data = load_menu()
 
 
18
  if preference == "Halal/Non-Veg":
19
  filtered_data = menu_data[menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
20
  elif preference == "Vegetarian":
21
  filtered_data = menu_data[~menu_data["Ingredients"].str.contains("Chicken|Mutton|Fish|Prawns|Goat", case=False, na=False)]
22
  elif preference == "Guilt-Free":
23
  filtered_data = menu_data[menu_data["Description"].str.contains(r"Fat: ([0-9]|10)g", case=False, na=False)]
24
+ else:
25
  filtered_data = menu_data
26
 
 
27
  html_content = ""
28
  for _, item in filtered_data.iterrows():
29
  html_content += f"""
 
41
  """
42
  return html_content
43
 
44
+ # Function to update the cart display
45
+ def update_cart(cart):
46
+ global cart_items
47
+ cart_items = cart # Update global cart items
48
+ if len(cart_items) == 0:
49
+ return "Your cart is empty."
50
+ cart_html = "<h3>Your Cart:</h3><ul>"
51
+ for item in cart_items:
52
+ extras = ", ".join(item["extras"])
53
+ cart_html += f"<li>{item['name']} (x{item['quantity']}, Spice: {item['spiceLevel']}, Extras: {extras}) - {item['price']}</li>"
54
+ cart_html += "</ul>"
55
+ return cart_html
56
+
57
  # Gradio app definition
58
  def app():
59
  with gr.Blocks() as demo:
 
69
  # Output area for menu items
70
  menu_output = gr.HTML(value=filter_menu("All"))
71
 
72
+ # Cart display
73
+ cart_output = gr.HTML(value="Your cart is empty.")
74
+
75
  # Modal window
76
  modal_window = gr.HTML("""
77
  <div id="modal" style="display: none; position: fixed; top: 40%; left: 50%; transform: translate(-50%, -40%); width: 50%; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
 
117
  <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>
118
  </div>
119
  <script>
 
120
  let cart = [];
121
 
122
  function openModal(name, image, description, price) {
 
132
  }
133
 
134
  function addToCart() {
 
135
  const name = document.getElementById('modal-name').innerText;
136
  const price = document.getElementById('modal-price').innerText;
137
  const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
138
  const quantity = parseInt(document.getElementById('quantity').value) || 1;
139
  const instructions = document.getElementById('special-instructions').value;
 
 
140
  const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
141
 
142
+ const cartItem = { name, price, spiceLevel, quantity, instructions, extras };
 
 
 
 
 
 
 
 
 
 
143
  cart.push(cartItem);
144
 
145
+ fetch("/update_cart", {
146
+ method: "POST",
147
+ headers: { "Content-Type": "application/json" },
148
+ body: JSON.stringify(cart)
149
+ });
150
 
 
151
  alert(`${name} added to cart!`);
 
 
152
  closeModal();
153
  }
154
  </script>
 
157
  # Interactivity
158
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
159
 
160
+ # Add the cart output
161
  gr.Row([selected_preference])
162
  gr.Row(menu_output)
163
+ gr.Row(cart_output)
164
  gr.Row(modal_window)
165
 
166
  return demo
167
 
 
168
  if __name__ == "__main__":
169
  demo = app()
170
  demo.launch()