geethareddy commited on
Commit
b77a0a8
·
verified ·
1 Parent(s): 53ee573

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -61
app.py CHANGED
@@ -73,22 +73,25 @@ def filter_menu(preference):
73
  filtered_data.append(item)
74
  return filtered_data
75
 
76
- # Render Menu as HTML
77
  def render_menu_html(menu_data):
78
- html_content = '<div style="display: flex; flex-wrap: wrap; gap: 20px; justify-content: center;">'
79
  for item in menu_data:
80
  html_content += f"""
81
- <div style="border: 1px solid #ddd; border-radius: 10px; padding: 15px; width: 200px; text-align: center; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);">
82
- <img src="{item.get('Image2__c', '')}" alt="{item['Name']}" style="width: 150px; height: 150px; object-fit: cover; border-radius: 10px;">
83
- <h3>{item['Name']}</h3>
84
- <p>${item['Price__c']}</p>
85
- <button style="background-color: green; color: white; border: none; padding: 10px; border-radius: 5px; cursor: pointer;" onclick="showPopup('{item['Name']}', '{item['Price__c']}', '{item['Description__c']}', '{item.get('Image2__c', '')}')">Add</button>
 
 
 
86
  </div>
87
  """
88
  html_content += '</div>'
89
  return html_content
90
 
91
- # Add to Cart
92
  def add_to_cart(name, price, quantity, addons, instructions):
93
  global cart
94
  item = {
@@ -101,25 +104,25 @@ def add_to_cart(name, price, quantity, addons, instructions):
101
  cart.append(item)
102
  return render_cart_html()
103
 
104
- # Render Cart
105
  def render_cart_html():
106
  global cart
107
  if not cart:
108
  return "<p>Your cart is empty.</p>"
109
- html = '<div>'
110
- for i, item in enumerate(cart):
111
- html += f"""
112
- <div style="border: 1px solid #ddd; padding: 10px; margin: 10px;">
113
- <h4>{item['name']}</h4>
114
  <p>Price: ${item['price']}</p>
115
  <p>Quantity: {item['quantity']}</p>
116
- <p>Addons: {', '.join(item['addons']) if item['addons'] else 'None'}</p>
117
  <p>Instructions: {item['instructions']}</p>
118
- <button style="background-color: red; color: white; border: none; padding: 5px 10px; border-radius: 5px; cursor: pointer;" onclick="removeFromCart({i})">Remove</button>
119
  </div>
120
  """
121
- html += '</div>'
122
- return html
123
 
124
  # Remove Item from Cart
125
  def remove_from_cart(index):
@@ -128,63 +131,178 @@ def remove_from_cart(index):
128
  cart.pop(index)
129
  return render_cart_html()
130
 
131
- # Popup Functionality (JavaScript)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  menu_script = """
133
  <script>
134
- function showPopup(name, price, description, image) {
135
- const popup = document.getElementById('popup');
136
- const overlay = document.getElementById('popup-overlay');
137
- document.getElementById('popup-title').innerText = name;
138
- document.getElementById('popup-price').innerText = `$${price}`;
139
- document.getElementById('popup-description').innerText = description;
140
- document.getElementById('popup-image').src = image;
141
- popup.style.display = 'block';
142
- overlay.style.display = 'block';
143
- }
144
-
145
- function closePopup() {
146
- const popup = document.getElementById('popup');
147
- const overlay = document.getElementById('popup-overlay');
148
- popup.style.display = 'none';
149
- overlay.style.display = 'none';
150
- }
151
-
152
- function removeFromCart(index) {
153
- GradioAPI.call("remove_from_cart", index).then(function(html) {
154
- document.getElementById('cart-content').innerHTML = html;
155
- });
156
- }
157
  </script>
158
  """
159
 
160
  # Gradio App
161
  with gr.Blocks() as app:
162
- gr.Markdown("# Welcome to the Dynamic Food Ordering System")
163
-
164
- # Include JavaScript for Popup
165
- gr.HTML(menu_script)
166
 
167
  # Popup HTML
168
  gr.HTML("""
169
- <div id='popup-overlay' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 1000;' onclick='closePopup()'></div>
170
- <div id='popup' style='display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: white; padding: 20px; border-radius: 10px; z-index: 1001; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); width: 400px;'>
171
- <button style='float: right; background: none; border: none; font-size: 1.5em; cursor: pointer;' onclick='closePopup()'>&times;</button>
172
- <img id='popup-image' src='' alt='Item Image' style='width: 100%; border-radius: 10px;'>
173
- <h3 id='popup-title'></h3>
174
- <p id='popup-price' style='color: green; font-size: 1.2em;'></p>
175
- <p id='popup-description'></p>
176
- <label>Quantity: <input type='number' id='popup-quantity' min='1' value='1'></label>
177
- <textarea id='popup-instructions' placeholder='Special instructions...' rows='3' style='width: 100%; margin-top: 10px;'></textarea>
178
- <button style='background-color: green; color: white; padding: 10px; border: none; border-radius: 5px; margin-top: 10px; cursor: pointer;' onclick='addToCart()'>Add to Cart</button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  </div>
180
  """)
181
 
182
  with gr.Row():
183
- preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], value="All", label="Menu Filter")
184
- menu_display = gr.HTML()
185
- preference.change(fn=lambda x: render_menu_html(filter_menu(x)), inputs=preference, outputs=menu_display)
186
 
187
- with gr.Row():
188
- cart_display = gr.HTML()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
 
190
  app.launch()
 
73
  filtered_data.append(item)
74
  return filtered_data
75
 
76
+ # Render Menu as HTML with Add Button
77
  def render_menu_html(menu_data):
78
+ html_content = '<div style="display: flex; flex-direction: column; gap: 20px; align-items: center;">'
79
  for item in menu_data:
80
  html_content += f"""
81
+ <div class=\"menu-card\" style=\"display: flex; flex-direction: row; gap: 20px; border: 1px solid #ddd; border-radius: 10px; padding: 15px; width: 80%; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); align-items: center;\">
82
+ <img src="{item.get('Image2__c', '')}" alt="{item['Name']}" style=\"width: 150px; height: 150px; object-fit: cover; border-radius: 10px;\">
83
+ <div style=\"flex-grow: 1;\">
84
+ <h3 style=\"margin: 0; font-size: 1.5em;\">{item['Name']}</h3>
85
+ <p style=\"margin: 5px 0; font-size: 1.2em; color: green;\">${item['Price__c']}</p>
86
+ <p style=\"margin: 5px 0; font-size: 1em; color: #555;\">{item['Description__c']}</p>
87
+ </div>
88
+ <button onclick=\"showPopup('{item['Name']}', '{item['Description__c']}', '{item['Price__c']}', '{item.get('Image2__c', '')}')\" style=\"background-color: green; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer;\">Add</button>
89
  </div>
90
  """
91
  html_content += '</div>'
92
  return html_content
93
 
94
+ # Add Item to Cart
95
  def add_to_cart(name, price, quantity, addons, instructions):
96
  global cart
97
  item = {
 
104
  cart.append(item)
105
  return render_cart_html()
106
 
107
+ # Render Cart as HTML
108
  def render_cart_html():
109
  global cart
110
  if not cart:
111
  return "<p>Your cart is empty.</p>"
112
+ html_content = '<div style="display: flex; flex-direction: column; gap: 20px; align-items: center;">'
113
+ for index, item in enumerate(cart):
114
+ html_content += f"""
115
+ <div class=\"cart-item\" style=\"display: flex; flex-direction: column; gap: 10px; border: 1px solid #ddd; border-radius: 10px; padding: 15px; width: 80%; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);\">
116
+ <h3>{item['name']}</h3>
117
  <p>Price: ${item['price']}</p>
118
  <p>Quantity: {item['quantity']}</p>
119
+ <p>Add-ons: {', '.join(item['addons']) if item['addons'] else 'None'}</p>
120
  <p>Instructions: {item['instructions']}</p>
121
+ <button onclick=\"removeFromCart({index})\" style=\"background-color: red; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer;\">Remove</button>
122
  </div>
123
  """
124
+ html_content += '</div>'
125
+ return html_content
126
 
127
  # Remove Item from Cart
128
  def remove_from_cart(index):
 
131
  cart.pop(index)
132
  return render_cart_html()
133
 
134
+ # Add CSS for Styling
135
+ menu_styles = """
136
+ <style>
137
+ .menu-card:hover {
138
+ box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
139
+ }
140
+ .popup {
141
+ position: fixed;
142
+ top: 50%;
143
+ left: 50%;
144
+ transform: translate(-50%, -50%);
145
+ background-color: white;
146
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
147
+ border-radius: 10px;
148
+ width: 400px;
149
+ padding: 20px;
150
+ display: none;
151
+ z-index: 1000;
152
+ }
153
+ .popup img {
154
+ width: 100%;
155
+ border-radius: 10px;
156
+ }
157
+ .popup-content {
158
+ margin-top: 15px;
159
+ }
160
+ .popup-close {
161
+ position: absolute;
162
+ top: 10px;
163
+ right: 10px;
164
+ background: none;
165
+ border: none;
166
+ font-size: 1.5em;
167
+ cursor: pointer;
168
+ }
169
+ .popup-overlay {
170
+ position: fixed;
171
+ top: 0;
172
+ left: 0;
173
+ width: 100%;
174
+ height: 100%;
175
+ background: rgba(0, 0, 0, 0.5);
176
+ display: none;
177
+ z-index: 999;
178
+ }
179
+ .addon-container {
180
+ margin-top: 15px;
181
+ }
182
+ .addon-container label {
183
+ margin-right: 10px;
184
+ }
185
+ .addon-container input {
186
+ margin-right: 5px;
187
+ }
188
+ .quantity-container {
189
+ margin-top: 15px;
190
+ }
191
+ .instruction-container {
192
+ margin-top: 15px;
193
+ }
194
+ </style>
195
+ """
196
+
197
+ # JavaScript for Popup Functionality
198
  menu_script = """
199
  <script>
200
+ function showPopup(name, description, price, image) {
201
+ const popup = document.getElementById('popup');
202
+ const overlay = document.getElementById('popup-overlay');
203
+ document.getElementById('popup-title').innerText = name;
204
+ document.getElementById('popup-description').innerText = description;
205
+ document.getElementById('popup-price').innerText = `$${price}`;
206
+ document.getElementById('popup-image').src = image;
207
+ popup.style.display = 'block';
208
+ overlay.style.display = 'block';
209
+ }
210
+
211
+ function closePopup() {
212
+ const popup = document.getElementById('popup');
213
+ const overlay = document.getElementById('popup-overlay');
214
+ popup.style.display = 'none';
215
+ overlay.style.display = 'none';
216
+ }
217
+
218
+ function removeFromCart(index) {
219
+ GradioAPI.call("remove_from_cart", index).then(function(html) {
220
+ document.getElementById('cart-content').innerHTML = html;
221
+ });
222
+ }
223
  </script>
224
  """
225
 
226
  # Gradio App
227
  with gr.Blocks() as app:
228
+ gr.HTML(menu_styles) # Add the CSS
229
+ gr.HTML(menu_script) # Add the JavaScript
 
 
230
 
231
  # Popup HTML
232
  gr.HTML("""
233
+ <div id='popup-overlay' class='popup-overlay' onclick='closePopup()'></div>
234
+ <div id='popup' class='popup'>
235
+ <button class='popup-close' onclick='closePopup()'>&times;</button>
236
+ <img id='popup-image' src='' alt='Item Image'>
237
+ <div class='popup-content'>
238
+ <h3 id='popup-title'></h3>
239
+ <p id='popup-description'></p>
240
+ <p id='popup-price' style='color: green; font-size: 1.2em;'></p>
241
+
242
+ <h4>Add-ons:</h4>
243
+ <div class='addon-container'>
244
+ <label><input type='checkbox' value='Thums Up + $2'> Thums Up + $2</label>
245
+ <label><input type='checkbox' value='Sprite + $2'> Sprite + $2</label>
246
+ <label><input type='checkbox' value='Extra Raitha + $1'> Extra Raitha + $1</label>
247
+ <label><input type='checkbox' value='Extra Salan + $2'> Extra Salan + $2</label>
248
+ <label><input type='checkbox' value='Extra Onion & Lemon + $2'> Extra Onion & Lemon + $2</label>
249
+ <label><input type='checkbox' value='Chilli Chicken + $14'> Chilli Chicken + $14</label>
250
+ <label><input type='checkbox' value='Veg Manchurian + $12'> Veg Manchurian + $12</label>
251
+ </div>
252
+
253
+ <div class='quantity-container'>
254
+ <label>Quantity: <input type='number' id='popup-quantity' min='1' value='1'></label>
255
+ </div>
256
+
257
+ <div class='instruction-container'>
258
+ <label>Special Instructions:</label>
259
+ <textarea id='popup-instructions' placeholder='Add your special instructions here...' rows='3' style='width: 100%;'></textarea>
260
+ </div>
261
+
262
+ <button onclick="addToCart()" style='margin-top: 15px; background-color: green; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer;'>Add to Cart</button>
263
+ </div>
264
  </div>
265
  """)
266
 
267
  with gr.Row():
268
+ gr.HTML("<h1 style='text-align: center;'>Welcome to Biryani Hub</h1>")
 
 
269
 
270
+ with gr.Row(visible=True) as login_page:
271
+ login_email = gr.Textbox(label="Email")
272
+ login_password = gr.Textbox(label="Password", type="password")
273
+ login_button = gr.Button("Login")
274
+ login_status = gr.Textbox(label="Status", interactive=False)
275
+
276
+ with gr.Row(visible=False) as signup_page:
277
+ signup_name = gr.Textbox(label="Name")
278
+ signup_email = gr.Textbox(label="Email")
279
+ signup_phone = gr.Textbox(label="Phone")
280
+ signup_password = gr.Textbox(label="Password", type="password")
281
+ submit_signup = gr.Button("Signup")
282
+ signup_status = gr.Textbox(label="Status", interactive=False)
283
+ to_login_button = gr.Button("Go to Login")
284
+
285
+ with gr.Row(visible=False) as menu_page:
286
+ preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Choose a Preference", value="All")
287
+ menu_output = gr.HTML()
288
+ preference.change(lambda p: render_menu_html(filter_menu(p)), inputs=preference, outputs=menu_output)
289
+
290
+ with gr.Row(visible=False) as cart_page:
291
+ cart_content = gr.HTML()
292
+
293
+ def handle_login(email, password):
294
+ status, user = login(email, password)
295
+ if status == "Login successful!":
296
+ return gr.update(visible=False), gr.update(visible=True), gr.update(value=""), status
297
+ else:
298
+ return gr.update(), gr.update(), gr.update(), status
299
+
300
+ def handle_signup(name, email, phone, password):
301
+ status = signup(name, email, phone, password)
302
+ return status
303
+
304
+ login_button.click(handle_login, [login_email, login_password], [login_page, menu_page, login_status])
305
+ submit_signup.click(handle_signup, [signup_name, signup_email, signup_phone, signup_password], signup_status)
306
+ to_login_button.click(lambda: (gr.update(visible=True), gr.update(visible=False)), None, [login_page, signup_page])
307
 
308
  app.launch()