Jayabalambika commited on
Commit
3c8c6ec
·
verified ·
1 Parent(s): 33781d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -123
app.py CHANGED
@@ -1,138 +1,82 @@
1
  import gradio as gr
2
- import smtplib
3
- from email.mime.text import MIMEText
4
- from email.mime.multipart import MIMEMultipart
5
- import sqlite3
6
- from datetime import datetime
7
  import pandas as pd
8
- from werkzeug.security import generate_password_hash, check_password_hash
9
- import random
10
- import string
11
-
12
- # Database setup
13
- conn = sqlite3.connect("attendance.db", check_same_thread=False)
14
- cursor = conn.cursor()
15
-
16
- cursor.execute('''CREATE TABLE IF NOT EXISTS users (
17
- id INTEGER PRIMARY KEY,
18
- email TEXT UNIQUE,
19
- password TEXT,
20
- verified INTEGER DEFAULT 0
21
- )''')
22
-
23
- cursor.execute('''CREATE TABLE IF NOT EXISTS attendance (
24
- id INTEGER PRIMARY KEY,
25
- email TEXT,
26
- date TEXT,
27
- status TEXT
28
- )''')
29
 
30
  # Helper Functions
31
- def generate_random_password():
32
- return ''.join(random.choices(string.ascii_letters + string.digits, k=8))
33
-
34
- def send_email(to_email, subject, body):
35
- try:
36
- print(f"Sending email to {to_email}: {subject}\n{body}") # Mock email sending
37
- return "Email sent successfully!"
38
- except Exception as e:
39
- return f"Failed to send email: {e}"
40
-
41
- # User Registration
42
- user_verification_tokens = {}
43
-
44
- def register_user(email):
45
- random_password = generate_random_password()
46
- hashed_password = generate_password_hash(random_password)
47
- try:
48
- cursor.execute("INSERT INTO users (email, password) VALUES (?, ?)", (email, hashed_password))
49
- conn.commit()
50
-
51
- # Generate verification link
52
- verification_token = ''.join(random.choices(string.ascii_letters + string.digits, k=16))
53
- user_verification_tokens[email] = verification_token
54
- verification_link = f"http://localhost/verify/{verification_token}" # Replace with actual host
55
-
56
- send_email(email, "Verify Your Account", f"Click here to verify your account: {verification_link}\nYour temporary password: {random_password}")
57
-
58
- return "Registration successful! Check your email to verify your account."
59
- except sqlite3.IntegrityError:
60
- return "Email already registered."
61
-
62
- # Verify User
63
- def verify_user(token):
64
- for email, stored_token in user_verification_tokens.items():
65
- if stored_token == token:
66
- cursor.execute("UPDATE users SET verified = 1 WHERE email = ?", (email,))
67
- conn.commit()
68
- return "Account verified! Please log in and change your password."
69
- return "Invalid verification token."
70
-
71
- # Change Password
72
- def change_password(email, old_password, new_password):
73
- cursor.execute("SELECT password FROM users WHERE email = ?", (email,))
74
- result = cursor.fetchone()
75
- if result and check_password_hash(result[0], old_password):
76
- hashed_new_password = generate_password_hash(new_password)
77
- cursor.execute("UPDATE users SET password = ? WHERE email = ?", (hashed_new_password, email))
78
- conn.commit()
79
- return "Password changed successfully!"
80
- return "Incorrect old password."
81
-
82
- # Log Attendance
83
- def log_attendance(email, status):
84
- date = datetime.now().strftime("%Y-%m-%d")
85
- cursor.execute("INSERT INTO attendance (email, date, status) VALUES (?, ?, ?)", (email, date, status))
86
- conn.commit()
87
- return "Attendance logged!"
88
-
89
- # Generate Monthly Fees Report
90
- def calculate_fees(email):
91
- current_month = datetime.now().strftime("%Y-%m")
92
- cursor.execute("SELECT COUNT(*) FROM attendance WHERE email = ? AND status = 'Present' AND date LIKE ?", (email, f"{current_month}%"))
93
- count_present = cursor.fetchone()[0]
94
- total_fees = count_present * 66.67
95
-
96
- # Send email with fees details
97
- send_email(email, "Monthly Fees", f"You attended {count_present} classes. Your total fees for the month: ${total_fees:.2f}")
98
-
99
- return f"Fees calculated and email sent: ${total_fees:.2f}"
100
 
101
  # Gradio Interface
102
- def login(email, password):
103
- cursor.execute("SELECT password, verified FROM users WHERE email = ?", (email,))
104
- result = cursor.fetchone()
105
- if result and check_password_hash(result[0], password):
106
- if result[1] == 1:
107
- return f"Welcome {email}!", gr.update(visible=True), email
108
- else:
109
- return "Account not verified. Check your email.", gr.update(visible=False), None
110
- return "Invalid credentials.", gr.update(visible=False), None
111
 
112
- with gr.Blocks() as app:
113
- email_state = gr.State()
114
 
115
- # Login Page
116
- with gr.Row():
117
- gr.Markdown("# Attendance Tracker")
118
 
119
  with gr.Row():
120
- email = gr.Textbox(label="Email")
121
- password = gr.Textbox(label="Password", type="password")
122
- login_button = gr.Button("Login")
 
123
 
124
- login_message = gr.Textbox(label="Message", interactive=False)
125
- attendance_section = gr.Group(visible=False)
126
 
127
- with attendance_section:
128
- with gr.Row():
129
- gr.Markdown("### Log Attendance")
130
- email_display = gr.Textbox(label="Email", interactive=False)
131
- status = gr.Radio(["Present", "Absent"], label="Status")
132
- log_button = gr.Button("Log Attendance")
133
- attendance_message = gr.Textbox(label="Message", interactive=False)
134
 
135
- login_button.click(login, inputs=[email, password], outputs=[login_message, attendance_section, email_state])
136
- log_button.click(log_attendance, inputs=[email_state, status], outputs=[attendance_message])
137
 
138
  app.launch()
 
1
  import gradio as gr
 
 
 
 
 
2
  import pandas as pd
3
+ import os
4
+ from datetime import datetime
5
+
6
+ # Attendance Tracker Setup
7
+ if not os.path.exists("attendance_records"):
8
+ os.makedirs("attendance_records")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Helper Functions
11
+ def log_attendance(name, day, date, status):
12
+ month = datetime.strptime(date, "%Y-%m-%d").strftime("%Y-%m")
13
+ file_path = f"attendance_records/{month}.csv"
14
+
15
+ # Load or create attendance sheet for the month
16
+ if os.path.exists(file_path):
17
+ df = pd.read_csv(file_path)
18
+ else:
19
+ df = pd.DataFrame(columns=["Name", "Day", "Date", "Status"])
20
+
21
+ # Add new attendance record
22
+ new_entry = {"Name": name, "Day": day, "Date": date, "Status": status}
23
+ df = df.append(new_entry, ignore_index=True)
24
+
25
+ # Save back to the file
26
+ df.to_csv(file_path, index=False)
27
+ return "Attendance logged successfully!"
28
+
29
+ def calculate_fees():
30
+ attendance_summaries = []
31
+
32
+ for file in os.listdir("attendance_records"):
33
+ if file.endswith(".csv"):
34
+ file_path = os.path.join("attendance_records", file)
35
+ df = pd.read_csv(file_path)
36
+
37
+ # Calculate fees for each candidate
38
+ fees_summary = df[df["Status"] == "Present"].groupby("Name").size() * (1000 / 12) # Example: Monthly fees divided by 12
39
+ fees_summary.name = "Fees"
40
+
41
+ # Merge fees into the attendance sheet
42
+ df = df.merge(fees_summary, on="Name", how="left")
43
+
44
+ # Save updated file
45
+ df.to_csv(file_path, index=False)
46
+
47
+ # Summarize for all candidates
48
+ attendance_summaries.append(df[["Name", "Fees"]].drop_duplicates())
49
+
50
+ if attendance_summaries:
51
+ summary_df = pd.concat(attendance_summaries).drop_duplicates()
52
+ summary_df.to_csv("attendance_records/fees_summary.csv", index=False)
53
+ return "Fees calculated and updated successfully!"
54
+
55
+ return "No attendance records found for fees calculation."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Gradio Interface
58
+ def submit_attendance(name, day, date, status):
59
+ return log_attendance(name, day, date, status)
 
 
 
 
 
 
 
60
 
61
+ def generate_fees():
62
+ return calculate_fees()
63
 
64
+ with gr.Blocks() as app:
65
+ gr.Markdown("# Attendance Tracker")
 
66
 
67
  with gr.Row():
68
+ name = gr.Textbox(label="Name")
69
+ day = gr.Textbox(label="Day")
70
+ date = gr.Textbox(label="Date (YYYY-MM-DD)")
71
+ status = gr.Radio(["Present", "Absent"], label="Status")
72
 
73
+ submit_button = gr.Button("Submit Attendance")
74
+ submit_message = gr.Textbox(label="Message", interactive=False)
75
 
76
+ calculate_button = gr.Button("Calculate Fees")
77
+ calculate_message = gr.Textbox(label="Fees Calculation Message", interactive=False)
 
 
 
 
 
78
 
79
+ submit_button.click(submit_attendance, inputs=[name, day, date, status], outputs=[submit_message])
80
+ calculate_button.click(generate_fees, outputs=[calculate_message])
81
 
82
  app.launch()