Spaces:
Runtime error
Runtime error
Update record_attendance.py
Browse files- record_attendance.py +58 -74
record_attendance.py
CHANGED
@@ -1,76 +1,60 @@
|
|
1 |
-
import csv
|
2 |
-
import os
|
3 |
-
from datetime import datetime
|
4 |
-
import streamlit as st
|
5 |
|
6 |
-
def record_attendance_new(present_students: list, subject: str, max_roll_number=26):
|
7 |
-
# Automatically get the current date in YYYY-MM-DD format
|
8 |
-
date = datetime.now().strftime("%Y-%m-%d")
|
9 |
-
|
10 |
-
# Convert present_students to integers
|
11 |
-
present_students = list(map(int, present_students))
|
12 |
-
print("Present Students:", present_students) # Debugging line
|
13 |
-
|
14 |
-
# Get the path of the uploaded CSV file
|
15 |
-
csv_file_path = f'{subject}.csv'
|
16 |
-
print("CSV File Path:", csv_file_path) # Debugging line
|
17 |
-
|
18 |
-
# Read the existing CSV file if it exists, otherwise create a new one
|
19 |
-
if os.path.exists(csv_file_path):
|
20 |
-
with open(csv_file_path, 'r', newline='') as csvfile:
|
21 |
-
reader = csv.reader(csvfile)
|
22 |
-
rows = list(reader)
|
23 |
-
else:
|
24 |
-
# If file doesn't exist, create a new one with header and roll numbers
|
25 |
-
rows = [['Roll Number']]
|
26 |
-
for roll_number in range(1, max_roll_number + 1):
|
27 |
-
rows.append([str(roll_number)])
|
28 |
-
|
29 |
-
# Check if the date column already exists
|
30 |
-
if date not in rows[0]:
|
31 |
-
# Add the new date column if it doesn't exist
|
32 |
-
rows[0].append(date)
|
33 |
-
|
34 |
-
date_col_index = rows[0].index(date)
|
35 |
-
|
36 |
-
# Update attendance for each roll number
|
37 |
-
for row in rows[1:]:
|
38 |
-
roll_number = int(row[0])
|
39 |
-
# Ensure the row has enough columns
|
40 |
-
while len(row) <= date_col_index:
|
41 |
-
row.append('')
|
42 |
-
|
43 |
-
if roll_number in present_students:
|
44 |
-
row[date_col_index] = 'Present'
|
45 |
-
print(f"Marking {roll_number}: Present") # Debugging line
|
46 |
-
elif row[date_col_index] == '':
|
47 |
-
# Only mark as absent if the cell is empty
|
48 |
-
row[date_col_index] = 'Absent'
|
49 |
-
print(f"Marking {roll_number}: Absent") # Debugging line
|
50 |
-
|
51 |
-
# Cross-check function to ensure all present students are marked correctly
|
52 |
-
def cross_check_attendance():
|
53 |
-
for row in rows[1:]:
|
54 |
-
roll_number = int(row[0])
|
55 |
-
if roll_number in present_students and row[date_col_index] != 'Present':
|
56 |
-
row[date_col_index] = 'Present'
|
57 |
-
print(f"Cross-check: Correcting {roll_number} to Present")
|
58 |
|
59 |
-
#
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
#
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
import os
|
3 |
+
from datetime import datetime
|
4 |
+
import streamlit as st
|
5 |
|
6 |
+
def record_attendance_new(present_students: list, subject: str, max_roll_number=26):
|
7 |
+
# Automatically get the current date in YYYY-MM-DD format
|
8 |
+
date = datetime.now().strftime("%Y-%m-%d")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Convert present_students to integers
|
11 |
+
present_students = list(map(int, present_students))
|
12 |
+
print("Present Students:", present_students) # Debugging line
|
13 |
+
|
14 |
+
# Get the path of the uploaded CSV file
|
15 |
+
csv_file_path = f'{subject}.csv'
|
16 |
+
print("CSV File Path:", csv_file_path) # Debugging line
|
17 |
+
|
18 |
+
# Read the existing CSV file
|
19 |
+
if os.path.exists(csv_file_path):
|
20 |
+
with open(csv_file_path, 'r', newline='') as csvfile:
|
21 |
+
reader = csv.reader(csvfile)
|
22 |
+
rows = list(reader)
|
23 |
+
else:
|
24 |
+
# If file doesn't exist, create a new one with header and roll numbers
|
25 |
+
rows = [['Roll Number']]
|
26 |
+
for roll_number in range(1, max_roll_number + 1):
|
27 |
+
rows.append([str(roll_number)])
|
28 |
+
|
29 |
+
# Add the new date column
|
30 |
+
rows[0].append(date)
|
31 |
+
date_col_index = len(rows[0]) - 1
|
32 |
+
|
33 |
+
# Update attendance for each roll number
|
34 |
+
for row in rows[1:]:
|
35 |
+
roll_number = int(row[0])
|
36 |
+
if roll_number in present_students:
|
37 |
+
row[date_col_index] = 'Present'
|
38 |
+
print(f"Marking {roll_number}: Present") # Debugging line
|
39 |
+
else:
|
40 |
+
row[date_col_index] = 'Absent'
|
41 |
+
print(f"Marking {roll_number}: Absent") # Debugging line
|
42 |
+
|
43 |
+
# Write the updated data back to the CSV file
|
44 |
+
with open(csv_file_path, 'w', newline='') as csvfile:
|
45 |
+
writer = csv.writer(csvfile)
|
46 |
+
writer.writerows(rows)
|
47 |
+
|
48 |
+
# Create a download button
|
49 |
+
with open(csv_file_path, 'r') as file:
|
50 |
+
csv_contents = file.read()
|
51 |
+
|
52 |
+
st.download_button(
|
53 |
+
label="Download updated attendance file",
|
54 |
+
data=csv_contents,
|
55 |
+
file_name=csv_file_path,
|
56 |
+
mime='text/csv'
|
57 |
+
)
|
58 |
+
|
59 |
+
print(f"Attendance recorded for {len(present_students)} students on {date} in {subject}.")
|
60 |
+
|