DSatishchandra commited on
Commit
53b01b6
·
verified ·
1 Parent(s): 7b771cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -12
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import pandas as pd
3
 
4
- # Function to load the menu data from Excel
5
  def load_menu():
6
  menu_file = "menu.xlsx" # Ensure this file exists in the same directory
7
  try:
@@ -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"""
@@ -36,15 +35,28 @@ def filter_menu(preference):
36
  </div>
37
  <div style="flex-shrink: 0; text-align: center;">
38
  <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
39
- <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;">Add</button>
40
  </div>
41
  </div>
42
  """
43
  return html_content
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Gradio app definition
46
  def app():
47
- with gr.Blocks(title="Dynamic Menu with Filters") as demo:
48
  gr.Markdown("## Dynamic Menu with Preferences")
49
 
50
  # Radio button for selecting preference
@@ -57,16 +69,92 @@ def app():
57
  # Output area for menu items
58
  menu_output = gr.HTML(value=filter_menu("All"))
59
 
60
- # Define interactivity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
62
 
63
- # Layout
64
  gr.Row([selected_preference])
65
  gr.Row(menu_output)
 
 
66
 
67
  return demo
68
 
69
- # Run the app
70
  if __name__ == "__main__":
71
  demo = app()
72
- demo.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
 
4
+ # Function to load the menu data
5
  def load_menu():
6
  menu_file = "menu.xlsx" # Ensure this file exists in the same directory
7
  try:
 
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"""
 
35
  </div>
36
  <div style="flex-shrink: 0; text-align: center;">
37
  <img src="{item['Image URL']}" alt="{item['Dish Name']}" style="width: 100px; height: 100px; border-radius: 8px; object-fit: cover; margin-bottom: 10px;">
38
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 15px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="openModal('{item['Dish Name']}', '{item['Image URL']}', '{item['Description']}', '{item['Price ($)']}')">Add</button>
39
  </div>
40
  </div>
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:
60
  gr.Markdown("## Dynamic Menu with Preferences")
61
 
62
  # Radio button for selecting preference
 
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;">
78
+ <div style="text-align: right;">
79
+ <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
80
+ </div>
81
+ <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
82
+ <h2 id="modal-name"></h2>
83
+ <p id="modal-description"></p>
84
+ <p id="modal-price"></p>
85
+ <!-- Spice Levels -->
86
+ <label for="spice-level">Choose a Spice Level (Required):</label>
87
+ <div id="spice-level-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
88
+ <label><input type="radio" name="spice-level" value="American Mild" required /> American Mild</label>
89
+ <label><input type="radio" name="spice-level" value="American Medium" required /> American Medium</label>
90
+ <label><input type="radio" name="spice-level" value="American Spicy" required /> American Spicy</label>
91
+ <label><input type="radio" name="spice-level" value="Indian Mild" required /> Indian Mild</label>
92
+ <label><input type="radio" name="spice-level" value="Indian Medium" required /> Indian Medium</label>
93
+ <label><input type="radio" name="spice-level" value="Indian Very Spicy" required /> Indian Very Spicy</label>
94
+ </div>
95
+ <!-- Biryani Extras -->
96
+ <label for="biryani-extras">Biryani Extras (Optional - Choose as many as you like):</label>
97
+ <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
98
+ <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 4oz" /> Extra Raitha 4oz + $1.00</label>
99
+ <label><input type="checkbox" name="biryani-extra" value="Extra Raitha 8oz" /> Extra Raitha 8oz + $2.00</label>
100
+ <label><input type="checkbox" name="biryani-extra" value="Extra Salan 4oz" /> Extra Salan 4oz + $1.00</label>
101
+ <label><input type="checkbox" name="biryani-extra" value="Extra Salan 8oz" /> Extra Salan 8oz + $2.00</label>
102
+ <label><input type="checkbox" name="biryani-extra" value="Extra Onion" /> Extra Onion + $1.00</label>
103
+ <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
104
+ <label><input type="checkbox" name="biryani-extra" value="Extra Fried Onion" /> Extra Fried Onion 4oz + $2.00</label>
105
+ </div>
106
+ <!-- Quantity and Special Instructions -->
107
+ <label for="quantity">Quantity:</label>
108
+ <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
109
+ <br><br>
110
+ <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
111
+ <br><br>
112
+ <!-- Add to Cart Button -->
113
+ <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>
114
+ </div>
115
+ <script>
116
+ let cart = [];
117
+ function openModal(name, image, description, price) {
118
+ document.getElementById('modal').style.display = 'block';
119
+ document.getElementById('modal-image').src = image;
120
+ document.getElementById('modal-name').innerText = name;
121
+ document.getElementById('modal-description').innerText = description;
122
+ document.getElementById('modal-price').innerText = price;
123
+ }
124
+ function closeModal() {
125
+ document.getElementById('modal').style.display = 'none';
126
+ }
127
+ function addToCart() {
128
+ const name = document.getElementById('modal-name').innerText;
129
+ const price = document.getElementById('modal-price').innerText;
130
+ const spiceLevel = document.querySelector('input[name="spice-level"]:checked')?.value || "Not Selected";
131
+ const quantity = parseInt(document.getElementById('quantity').value) || 1;
132
+ const instructions = document.getElementById('special-instructions').value;
133
+ const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
134
+ const cartItem = { name, price, spiceLevel, quantity, instructions, extras };
135
+ cart.push(cartItem);
136
+ fetch("/update_cart", {
137
+ method: "POST",
138
+ headers: { "Content-Type": "application/json" },
139
+ body: JSON.stringify(cart)
140
+ });
141
+ alert(`${name} added to cart!`);
142
+ closeModal();
143
+ }
144
+ </script>
145
+ """)
146
+
147
+ # Interactivity
148
  selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
149
 
150
+ # Add the cart output
151
  gr.Row([selected_preference])
152
  gr.Row(menu_output)
153
+ gr.Row(cart_output)
154
+ gr.Row(modal_window)
155
 
156
  return demo
157
 
 
158
  if __name__ == "__main__":
159
  demo = app()
160
+ demo.launch()