PRIYANSHUDHAKED commited on
Commit
0655f10
·
verified ·
1 Parent(s): 4b588cf

Update record_attendance.py

Browse files
Files changed (1) hide show
  1. record_attendance.py +65 -59
record_attendance.py CHANGED
@@ -1,61 +1,67 @@
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.append('Present')
38
- print(f"Marking {roll_number}: Present") # Debugging line
39
- else:
40
- row.append('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
 
61
- print(f"Attendance recorded for {len(present_students)} students in {subject} on {date}.")
 
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 CSV file for the specified subject
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'] + [date]] # Start with header row
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
+ rows[0].append(date) # Add the new date column if it doesn't exist
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ # Get the index of the date column
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 for the date
40
+ if len(row) <= date_col_index:
41
+ row.extend([''] * (date_col_index - len(row) + 1)) # Fill with empty strings if needed
42
+
43
+ # Mark attendance
44
+ if roll_number in present_students:
45
+ row[date_col_index] = 'Present'
46
+ print(f"Marking {roll_number}: Present") # Debugging line
47
+ else:
48
+ row[date_col_index] = 'Absent'
49
+ print(f"Marking {roll_number}: Absent") # Debugging line
50
+
51
+ # Write the updated data back to the CSV file
52
+ with open(csv_file_path, 'w', newline='') as csvfile:
53
+ writer = csv.writer(csvfile)
54
+ writer.writerows(rows)
55
+
56
+ # Create a download button
57
+ with open(csv_file_path, 'r') as file:
58
+ csv_contents = file.read()
59
+
60
+ st.download_button(
61
+ label="Download updated attendance file",
62
+ data=csv_contents,
63
+ file_name=csv_file_path,
64
+ mime='text/csv'
65
+ )
66
 
67
+ print(f"Attendance recorded for {len(present_students)} students on {date} in {subject}.")