nagasurendra commited on
Commit
b0b4830
·
verified ·
1 Parent(s): be2cb4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -135
app.py CHANGED
@@ -11,6 +11,7 @@ def load_users():
11
  df = pd.read_excel(USER_FILE)
12
  return {row['Email']: row for _, row in df.iterrows()}
13
  except FileNotFoundError:
 
14
  df = pd.DataFrame(columns=["Name", "Phone", "Email", "Password"])
15
  df.to_excel(USER_FILE, index=False)
16
  return {}
@@ -24,37 +25,19 @@ def save_users(users):
24
  def signup_user(name, phone, email, password):
25
  users = load_users()
26
  if email in users:
27
- return "Email already exists. Please use a different email."
28
  hashed_password = hashpw(password.encode(), gensalt()).decode()
29
  users[email] = {"Name": name, "Phone": phone, "Email": email, "Password": hashed_password}
30
  save_users(users)
31
- return "Signup successful! Please log in."
32
 
33
  # Validate login
34
  def validate_login(email, password):
35
  users = load_users()
36
  if email in users and checkpw(password.encode(), users[email]["Password"].encode()):
37
- return True, "Login successful!"
38
- return False, "Invalid email or password."
39
 
40
- # Session management
41
- active_sessions = {}
42
-
43
- def login_user(email, password):
44
- success, message = validate_login(email, password)
45
- if success:
46
- active_sessions[email] = True
47
- return message
48
- return message
49
-
50
- def is_logged_in(email):
51
- return active_sessions.get(email, False)
52
-
53
- def logout_user(email):
54
- if email in active_sessions:
55
- del active_sessions[email]
56
- return "Logged out successfully!"
57
- return "No active session found for this email."
58
  # Function to load the menu data
59
  def load_menu():
60
  menu_file = "menu.xlsx" # Ensure this file exists in the same directory
@@ -273,125 +256,113 @@ modal_and_cart_js = """
273
  }
274
  </script>
275
  """
276
-
277
-
278
- # Gradio app
279
  def app():
280
  with gr.Blocks() as demo:
281
- gr.Markdown("## Secure Food Ordering System")
282
-
283
- # Login and Signup Tabs
284
- with gr.Tabs():
285
- # Signup Tab
286
- with gr.Tab("Signup"):
287
- gr.Markdown("### Signup Page")
288
- name = gr.Textbox(label="Name")
289
- phone = gr.Textbox(label="Phone Number")
290
- email = gr.Textbox(label="Email")
291
- password = gr.Textbox(label="Password", type="password")
292
- signup_btn = gr.Button("Signup")
293
- signup_message = gr.Label()
294
-
295
- signup_btn.click(
296
- signup_user,
297
- inputs=[name, phone, email, password],
298
- outputs=[signup_message],
299
- )
300
-
301
- # Login Tab
302
- with gr.Tab("Login"):
303
- gr.Markdown("### Login Page")
304
- login_email = gr.Textbox(label="Email")
305
- login_password = gr.Textbox(label="Password", type="password")
306
- login_btn = gr.Button("Login")
307
- login_message = gr.Label()
308
-
309
- login_btn.click(
310
- login_user,
311
- inputs=[login_email, login_password],
312
- outputs=[login_message],
313
- )
314
-
315
- # Menu Page
316
- with gr.Tab("Menu Page"):
317
- gr.Markdown("### Menu Page (Accessible Only After Login)")
318
-
319
- # Input for user email to verify login
320
- email_input = gr.Textbox(label="Enter Email to Verify Access")
321
- verify_btn = gr.Button("Load Menu")
322
- menu_output = gr.HTML(value="") # Output for menu items
323
-
324
- def verify_and_load_menu(email):
325
- if is_logged_in(email):
326
- return filter_menu("All") # Filtered menu based on preference
327
- return "Access Denied! Please log in."
328
-
329
- verify_btn.click(
330
- verify_and_load_menu,
331
- inputs=[email_input],
332
- outputs=[menu_output],
333
- )
334
-
335
- # Radio button for selecting preference
336
- selected_preference = gr.Radio(
337
- choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
338
- value="All",
339
- label="Choose a Preference",
340
- )
341
-
342
- # Output area for menu items
343
- menu_output = gr.HTML(value=filter_menu("All"))
344
-
345
- # Floating cart display
346
- cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
347
-
348
- # Final order display
349
- final_order_output = gr.HTML(value="", elem_id="final-order")
350
-
351
- # Modal window
352
- modal_window = gr.HTML("""
353
- <div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
354
- <div style="text-align: right;">
355
- <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
356
- </div>
357
- <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
358
- <h2 id="modal-name"></h2>
359
- <p id="modal-description"></p>
360
- <p id="modal-price"></p>
361
- <!-- Biryani Extras -->
362
- <label for="biryani-extras">Biryani Extras :</label>
363
- <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
364
- <label><input type="checkbox" name="biryani-extra" value="Thums up" /> Thums up + $2.00</label>
365
- <label><input type="checkbox" name="biryani-extra" value="Sprite" /> Sprite + $2.00</label>
366
- <label><input type="checkbox" name="biryani-extra" value="Extra Raitha" /> Extra Raitha + $1.00</label>
367
- <label><input type="checkbox" name="biryani-extra" value="Extra Salan" /> Extra Salan + $2.00</label>
368
- <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
369
- <label><input type="checkbox" name="biryani-extra" value="Chilli Chicken" /> Chilli Chicken + $14.00</label>
370
- <label><input type="checkbox" name="biryani-extra" value="Veg Manchurian" /> Veg Manchurian + $12.00</label>
371
- </div>
372
- <!-- Quantity and Special Instructions -->
373
- <label for="quantity">Quantity:</label>
374
- <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
375
- <br><br>
376
- <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
377
- <br><br>
378
- <!-- Add to Cart Button -->
379
- <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>
380
  </div>
381
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
382
 
383
- # Update menu dynamically based on preference
384
- selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
385
 
386
- # Layout
387
- gr.Row([selected_preference])
388
- gr.Row(menu_output)
389
- gr.Row(cart_output)
390
- gr.Row(modal_window)
391
- gr.Row(final_order_output)
392
- gr.HTML(modal_and_cart_js)
393
 
394
- return demo
395
 
396
 
397
  if __name__ == "__main__":
 
11
  df = pd.read_excel(USER_FILE)
12
  return {row['Email']: row for _, row in df.iterrows()}
13
  except FileNotFoundError:
14
+ # Create the file if it doesn't exist
15
  df = pd.DataFrame(columns=["Name", "Phone", "Email", "Password"])
16
  df.to_excel(USER_FILE, index=False)
17
  return {}
 
25
  def signup_user(name, phone, email, password):
26
  users = load_users()
27
  if email in users:
28
+ return "Email already exists. Please use a different email.", "signup" # Redirect to signup
29
  hashed_password = hashpw(password.encode(), gensalt()).decode()
30
  users[email] = {"Name": name, "Phone": phone, "Email": email, "Password": hashed_password}
31
  save_users(users)
32
+ return "Signup successful! Redirecting to login page...", "login" # Redirect to login
33
 
34
  # Validate login
35
  def validate_login(email, password):
36
  users = load_users()
37
  if email in users and checkpw(password.encode(), users[email]["Password"].encode()):
38
+ return "Login successful! Redirecting to menu page...", "menu" # Redirect to menu
39
+ return "Invalid email or password. Please try again.", "login" # Redirect to login
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # Function to load the menu data
42
  def load_menu():
43
  menu_file = "menu.xlsx" # Ensure this file exists in the same directory
 
256
  }
257
  </script>
258
  """
 
 
 
259
  def app():
260
  with gr.Blocks() as demo:
261
+ # State to track the current page
262
+ current_page = gr.State("login") # Default page is the login page
263
+
264
+ # === LOGIN PAGE ===
265
+ with gr.Row(visible_if=lambda x: x == "login", inputs=[current_page]):
266
+ gr.Markdown("### Login Page")
267
+ login_email = gr.Textbox(label="Email")
268
+ login_password = gr.Textbox(label="Password", type="password")
269
+ login_btn = gr.Button("Login")
270
+ login_message = gr.Label()
271
+
272
+ def login(email, password):
273
+ message, page = validate_login(email, password)
274
+ return message, page # Redirect based on login status
275
+
276
+ login_btn.click(
277
+ login,
278
+ inputs=[login_email, login_password],
279
+ outputs=[login_message, current_page], # Update message and page
280
+ )
281
+
282
+ # === SIGNUP PAGE ===
283
+ with gr.Row(visible_if=lambda x: x == "signup", inputs=[current_page]):
284
+ gr.Markdown("### Signup Page")
285
+ name = gr.Textbox(label="Name")
286
+ phone = gr.Textbox(label="Phone Number")
287
+ email = gr.Textbox(label="Email")
288
+ password = gr.Textbox(label="Password", type="password")
289
+ signup_btn = gr.Button("Signup")
290
+ signup_message = gr.Label()
291
+
292
+ def signup(name, phone, email, password):
293
+ message, page = signup_user(name, phone, email, password)
294
+ return message, page # Redirect to login after signup
295
+
296
+ signup_btn.click(
297
+ signup,
298
+ inputs=[name, phone, email, password],
299
+ outputs=[signup_message, current_page], # Update message and page
300
+ )
301
+
302
+ # === MENU PAGE ===
303
+ with gr.Row(visible_if=lambda x: x == "menu", inputs=[current_page]):
304
+ gr.Markdown("### Menu Page")
305
+
306
+ # Radio button for selecting preference
307
+ selected_preference = gr.Radio(
308
+ choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
309
+ value="All",
310
+ label="Choose a Preference",
311
+ )
312
+
313
+ # Output area for menu items
314
+ menu_output = gr.HTML(value=filter_menu("All"))
315
+
316
+ # Floating cart display
317
+ cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
318
+
319
+ # Final order display
320
+ final_order_output = gr.HTML(value="", elem_id="final-order")
321
+
322
+ # Modal window
323
+ modal_window = gr.HTML("""
324
+ <div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
325
+ <div style="text-align: right;">
326
+ <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  </div>
328
+ <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
329
+ <h2 id="modal-name"></h2>
330
+ <p id="modal-description"></p>
331
+ <p id="modal-price"></p>
332
+ <!-- Biryani Extras -->
333
+ <label for="biryani-extras">Biryani Extras :</label>
334
+ <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
335
+ <label><input type="checkbox" name="biryani-extra" value="Thums up" /> Thums up + $2.00</label>
336
+ <label><input type="checkbox" name="biryani-extra" value="Sprite" /> Sprite + $2.00</label>
337
+ <label><input type="checkbox" name="biryani-extra" value="Extra Raitha" /> Extra Raitha + $1.00</label>
338
+ <label><input type="checkbox" name="biryani-extra" value="Extra Salan" /> Extra Salan + $2.00</label>
339
+ <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
340
+ <label><input type="checkbox" name="biryani-extra" value="Chilli Chicken" /> Chilli Chicken + $14.00</label>
341
+ <label><input type="checkbox" name="biryani-extra" value="Veg Manchurian" /> Veg Manchurian + $12.00</label>
342
+ </div>
343
+ <!-- Quantity and Special Instructions -->
344
+ <label for="quantity">Quantity:</label>
345
+ <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
346
+ <br><br>
347
+ <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
348
+ <br><br>
349
+ <!-- Add to Cart Button -->
350
+ <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>
351
+ </div>
352
+ """)
353
 
354
+ # Update menu dynamically based on preference
355
+ selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
356
 
357
+ # Layout
358
+ gr.Row([selected_preference])
359
+ gr.Row(menu_output)
360
+ gr.Row(cart_output)
361
+ gr.Row(modal_window)
362
+ gr.Row(final_order_output)
363
+ gr.HTML(modal_and_cart_js)
364
 
365
+ return demo
366
 
367
 
368
  if __name__ == "__main__":