Spaces:
Runtime error
Runtime error
Update record_attendance.py
Browse files- record_attendance.py +32 -16
record_attendance.py
CHANGED
@@ -1,20 +1,28 @@
|
|
1 |
import csv
|
|
|
2 |
from datetime import datetime
|
3 |
import streamlit as st
|
4 |
-
import os
|
5 |
|
6 |
-
def record_attendance_new(present_students: list, subject: str):
|
7 |
# Automatically get the current date in YYYY-MM-DD format
|
8 |
date = datetime.now().strftime("%Y-%m-%d")
|
9 |
-
csv_file_path = f'{subject}_attendance.csv'
|
10 |
|
11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
if os.path.exists(csv_file_path):
|
13 |
-
with open(csv_file_path, 'r', newline='') as
|
14 |
-
reader = csv.reader(
|
15 |
rows = list(reader)
|
16 |
else:
|
17 |
-
|
|
|
18 |
|
19 |
# Check if the date column already exists
|
20 |
if date not in rows[0]:
|
@@ -22,23 +30,32 @@ def record_attendance_new(present_students: list, subject: str):
|
|
22 |
|
23 |
date_col_index = rows[0].index(date)
|
24 |
|
25 |
-
# Update or add
|
26 |
-
for roll_number in
|
27 |
found = False
|
28 |
for row in rows[1:]:
|
29 |
-
if row[0] ==
|
30 |
found = True
|
31 |
if len(row) < date_col_index + 1:
|
32 |
row.extend([''] * (date_col_index - len(row) + 1))
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
break
|
|
|
35 |
if not found:
|
36 |
-
new_row = [str(roll_number)] + [''] * (date_col_index - 1)
|
|
|
37 |
rows.append(new_row)
|
|
|
38 |
|
39 |
# Write the updated data back to the CSV file
|
40 |
-
with open(csv_file_path, 'w', newline='') as
|
41 |
-
writer = csv.writer(
|
42 |
writer.writerows(rows)
|
43 |
|
44 |
# Create a download button
|
@@ -52,5 +69,4 @@ def record_attendance_new(present_students: list, subject: str):
|
|
52 |
mime='text/csv'
|
53 |
)
|
54 |
|
55 |
-
print(f"Attendance recorded for {len(present_students)} students on {date} in {subject}.")
|
56 |
-
|
|
|
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
|
25 |
+
rows = [['Roll Number']]
|
26 |
|
27 |
# Check if the date column already exists
|
28 |
if date not in rows[0]:
|
|
|
30 |
|
31 |
date_col_index = rows[0].index(date)
|
32 |
|
33 |
+
# Update or add attendance for each roll number
|
34 |
+
for roll_number in range(1, max_roll_number + 1):
|
35 |
found = False
|
36 |
for row in rows[1:]:
|
37 |
+
if int(row[0]) == roll_number:
|
38 |
found = True
|
39 |
if len(row) < date_col_index + 1:
|
40 |
row.extend([''] * (date_col_index - len(row) + 1))
|
41 |
+
|
42 |
+
if roll_number in present_students:
|
43 |
+
row[date_col_index] = 'Present'
|
44 |
+
print(f"Updating {roll_number}: Present") # Debugging line
|
45 |
+
else:
|
46 |
+
row[date_col_index] = 'Absent'
|
47 |
+
print(f"Updating {roll_number}: Absent") # Debugging line
|
48 |
break
|
49 |
+
|
50 |
if not found:
|
51 |
+
new_row = [str(roll_number)] + [''] * (date_col_index - 1)
|
52 |
+
new_row.append('Present' if roll_number in present_students else 'Absent')
|
53 |
rows.append(new_row)
|
54 |
+
print(f"Adding new row for {roll_number}: {'Present' if roll_number in present_students else 'Absent'}")
|
55 |
|
56 |
# Write the updated data back to the CSV file
|
57 |
+
with open(csv_file_path, 'w', newline='') as csvfile:
|
58 |
+
writer = csv.writer(csvfile)
|
59 |
writer.writerows(rows)
|
60 |
|
61 |
# Create a download button
|
|
|
69 |
mime='text/csv'
|
70 |
)
|
71 |
|
72 |
+
print(f"Attendance recorded for {len(present_students)} students on {date} in {subject}.")
|
|