nagasurendra commited on
Commit
0557a16
·
verified ·
1 Parent(s): 3b6879b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -105
app.py CHANGED
@@ -11,7 +11,6 @@ def load_users():
11
  df = pd.read_excel(USER_FILE)
12
  return {row['Username']: row['Password'] for _, row in df.iterrows()}
13
  except FileNotFoundError:
14
- # Create the file if it doesn't exist
15
  df = pd.DataFrame(columns=["Username", "Password"])
16
  df.to_excel(USER_FILE, index=False)
17
  return {}
@@ -26,7 +25,6 @@ def signup_user(username, password):
26
  users = load_users()
27
  if username in users:
28
  return "Username already exists. Please choose a different username."
29
- # Hash the password before saving
30
  hashed_password = hashpw(password.encode(), gensalt()).decode()
31
  users[username] = hashed_password
32
  save_users(users)
@@ -45,7 +43,7 @@ active_sessions = {}
45
  def login_user(username, password):
46
  success, message = validate_login(username, password)
47
  if success:
48
- active_sessions[username] = True # Add to active sessions
49
  return message
50
  return message
51
 
@@ -58,10 +56,11 @@ def logout_user(username):
58
  return f"{username} has been logged out successfully."
59
  return "No active session found for this user."
60
 
61
-
62
- # Function to load menu data
 
63
  def load_menu():
64
- menu_file = "menu.xlsx" # Ensure this file exists
65
  try:
66
  return pd.read_excel(menu_file)
67
  except Exception as e:
@@ -96,7 +95,7 @@ def filter_menu(preference):
96
  """
97
  return html_content
98
 
99
- # JavaScript for modal and cart behavior (unchanged)
100
  modal_and_cart_js = """
101
  <style>
102
  .cart-container {
@@ -277,122 +276,94 @@ modal_and_cart_js = """
277
  </script>
278
  """
279
 
280
- # Gradio app definition
281
  def app():
282
  with gr.Blocks() as demo:
283
- gr.Markdown("## Secure Food Ordering System with Login")
284
 
 
285
  with gr.Tabs():
286
- # Login Tab
287
  with gr.Tab("Login"):
288
- gr.Markdown("### Login Page")
289
  username = gr.Textbox(label="Username")
290
  password = gr.Textbox(label="Password", type="password")
291
  login_btn = gr.Button("Login")
292
  login_message = gr.Label()
293
 
294
- login_btn.click(
295
- login_user,
296
- inputs=[username, password],
297
- outputs=[login_message],
298
- )
299
 
300
- # Signup Tab
301
  with gr.Tab("Signup"):
302
- gr.Markdown("### Signup Page")
303
  signup_username = gr.Textbox(label="Username")
304
  signup_password = gr.Textbox(label="Password", type="password")
305
  signup_btn = gr.Button("Signup")
306
  signup_message = gr.Label()
307
 
308
- signup_btn.click(
309
- signup_user,
310
- inputs=[signup_username, signup_password],
311
- outputs=[signup_message],
 
 
 
 
 
 
 
 
 
312
  )
313
 
314
- # Menu Tab
315
- with gr.Tab("Menu Page"):
316
- gr.Markdown("### Menu Page (Only for Logged-In Users)")
317
-
318
- # Input for username to verify login
319
- menu_username = gr.Textbox(label="Enter Username to Verify Login", placeholder="Enter your username")
320
- menu_check = gr.Button("Check Login Status")
321
- login_status = gr.Label()
322
- menu_content = gr.HTML(value="")
323
-
324
- # Function to check login status
325
- def check_login_status(username):
326
- if is_logged_in(username):
327
- return "Welcome to the menu page!", filter_menu("All")
328
- return "You are not logged in!", ""
329
-
330
- # Bind login check button
331
- menu_check.click(
332
- check_login_status,
333
- inputs=[menu_username],
334
- outputs=[login_status, menu_content],
335
- )
336
-
337
- # Define Radio button for selecting preference
338
- selected_preference = gr.Radio(
339
- choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
340
- value="All",
341
- label="Choose a Preference",
342
- )
343
-
344
- # Output area for menu items
345
- menu_output = gr.HTML(value=filter_menu("All"))
346
-
347
- # Dynamically update the menu based on preference
348
- selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
349
-
350
- # Floating cart display
351
- cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
352
-
353
- # Final order display
354
- final_order_output = gr.HTML(value="", elem_id="final-order")
355
-
356
- # Modal window
357
- modal_window = gr.HTML("""
358
- <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;">
359
- <div style="text-align: right;">
360
- <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
361
- </div>
362
- <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
363
- <h2 id="modal-name"></h2>
364
- <p id="modal-description"></p>
365
- <p id="modal-price"></p>
366
- <label for="biryani-extras">Biryani Extras:</label>
367
- <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
368
- <label><input type="checkbox" name="biryani-extra" value="Thums up" /> Thums up + $2.00</label>
369
- <label><input type="checkbox" name="biryani-extra" value="Sprite" /> Sprite + $2.00</label>
370
- <label><input type="checkbox" name="biryani-extra" value="Extra Raitha" /> Extra Raitha + $1.00</label>
371
- <label><input type="checkbox" name="biryani-extra" value="Extra Salan" /> Extra Salan + $2.00</label>
372
- <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
373
- <label><input type="checkbox" name="biryani-extra" value="Chilli Chicken" /> Chilli Chicken + $14.00</label>
374
- <label><input type="checkbox" name="biryani-extra" value="Veg Manchurian" /> Veg Manchurian + $12.00</label>
375
- </div>
376
- <label for="quantity">Quantity:</label>
377
- <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
378
- <br><br>
379
- <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
380
- <br><br>
381
- <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>
382
- </div>
383
- """)
384
-
385
- # Layout for the menu page
386
- gr.Row([selected_preference])
387
- gr.Row(menu_output)
388
- gr.Row(cart_output)
389
- gr.Row(modal_window)
390
- gr.Row(final_order_output)
391
- gr.HTML(modal_and_cart_js)
392
-
393
-
394
- return demo
395
 
396
  if __name__ == "__main__":
397
- demo = app()
398
- demo.launch()
 
11
  df = pd.read_excel(USER_FILE)
12
  return {row['Username']: row['Password'] for _, row in df.iterrows()}
13
  except FileNotFoundError:
 
14
  df = pd.DataFrame(columns=["Username", "Password"])
15
  df.to_excel(USER_FILE, index=False)
16
  return {}
 
25
  users = load_users()
26
  if username in users:
27
  return "Username already exists. Please choose a different username."
 
28
  hashed_password = hashpw(password.encode(), gensalt()).decode()
29
  users[username] = hashed_password
30
  save_users(users)
 
43
  def login_user(username, password):
44
  success, message = validate_login(username, password)
45
  if success:
46
+ active_sessions[username] = True
47
  return message
48
  return message
49
 
 
56
  return f"{username} has been logged out successfully."
57
  return "No active session found for this user."
58
 
59
+ # Menu functionality: Your original code will remain here as-is
60
+ # Keeping all your existing menu, cart, modal logic intact
61
+ # Function to load the menu data
62
  def load_menu():
63
+ menu_file = "menu.xlsx" # Ensure this file exists in the same directory
64
  try:
65
  return pd.read_excel(menu_file)
66
  except Exception as e:
 
95
  """
96
  return html_content
97
 
98
+ # JavaScript for modal and cart behavior
99
  modal_and_cart_js = """
100
  <style>
101
  .cart-container {
 
276
  </script>
277
  """
278
 
279
+ # Gradio app
280
  def app():
281
  with gr.Blocks() as demo:
282
+ gr.Markdown("## Secure Food Ordering System")
283
 
284
+ # Login and Signup Tabs
285
  with gr.Tabs():
 
286
  with gr.Tab("Login"):
 
287
  username = gr.Textbox(label="Username")
288
  password = gr.Textbox(label="Password", type="password")
289
  login_btn = gr.Button("Login")
290
  login_message = gr.Label()
291
 
292
+ login_btn.click(login_user, [username, password], [login_message])
 
 
 
 
293
 
 
294
  with gr.Tab("Signup"):
 
295
  signup_username = gr.Textbox(label="Username")
296
  signup_password = gr.Textbox(label="Password", type="password")
297
  signup_btn = gr.Button("Signup")
298
  signup_message = gr.Label()
299
 
300
+ signup_btn.click(signup_user, [signup_username, signup_password], [signup_message])
301
+
302
+ # Menu Page Tab
303
+ with gr.Tab("Menu Page"):
304
+ gr.Markdown("### Menu Page")
305
+
306
+ # Preserve your menu-related code exactly as it was
307
+ # Add your exact original menu, cart, and modal code here
308
+
309
+ selected_preference = gr.Radio(
310
+ choices=["All", "Vegetarian", "Halal/Non-Veg", "Guilt-Free"],
311
+ value="All",
312
+ label="Choose a Preference",
313
  )
314
 
315
+ # Output area for menu items
316
+ menu_output = gr.HTML(value=filter_menu("All"))
317
+
318
+ # Floating cart display
319
+ cart_output = gr.HTML(value="Your cart is empty.", elem_id="floating-cart")
320
+
321
+ # Final order display
322
+ final_order_output = gr.HTML(value="", elem_id="final-order")
323
+
324
+ # Modal window
325
+ modal_window = gr.HTML("""
326
+ <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;">
327
+ <div style="text-align: right;">
328
+ <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
329
+ </div>
330
+ <img id="modal-image" style="width: 100%; height: auto; border-radius: 8px; margin-bottom: 20px;" />
331
+ <h2 id="modal-name"></h2>
332
+ <p id="modal-description"></p>
333
+ <p id="modal-price"></p>
334
+ <!-- Biryani Extras -->
335
+ <label for="biryani-extras">Biryani Extras :</label>
336
+ <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
337
+ <label><input type="checkbox" name="biryani-extra" value="Thums up" /> Thums up + $2.00</label>
338
+ <label><input type="checkbox" name="biryani-extra" value="Sprite" /> Sprite + $2.00</label>
339
+ <label><input type="checkbox" name="biryani-extra" value="Extra Raitha" /> Extra Raitha + $1.00</label>
340
+ <label><input type="checkbox" name="biryani-extra" value="Extra Salan" /> Extra Salan + $2.00</label>
341
+ <label><input type="checkbox" name="biryani-extra" value="Extra Onion & Lemon" /> Extra Onion & Lemon + $2.00</label>
342
+ <label><input type="checkbox" name="biryani-extra" value="Chilli Chicken" /> Chilli Chicken + $14.00</label>
343
+ <label><input type="checkbox" name="biryani-extra" value="Veg Manchurian" /> Veg Manchurian + $12.00</label>
344
+ </div>
345
+ <!-- Quantity and Special Instructions -->
346
+ <label for="quantity">Quantity:</label>
347
+ <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
348
+ <br><br>
349
+ <textarea id="special-instructions" placeholder="Add special instructions here..." style="width: 100%; height: 60px;"></textarea>
350
+ <br><br>
351
+ <!-- Add to Cart Button -->
352
+ <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>
353
+ </div>
354
+ """)
355
+
356
+ # Update menu dynamically based on preference
357
+ selected_preference.change(filter_menu, inputs=[selected_preference], outputs=[menu_output])
358
+
359
+ # Layout
360
+ gr.Row([selected_preference])
361
+ gr.Row(menu_output)
362
+ gr.Row(cart_output)
363
+ gr.Row(modal_window)
364
+ gr.Row(final_order_output)
365
+ gr.HTML(modal_and_cart_js)
366
+ return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
 
368
  if __name__ == "__main__":
369
+ app().launch()