File size: 2,318 Bytes
bff6c4f
 
 
 
0655f10
bff6c4f
 
 
2efe269
 
6568643
bff6c4f
 
 
2efe269
 
 
 
 
 
 
 
6568643
bff6c4f
 
 
6568643
bff6c4f
 
 
 
 
 
 
 
 
 
c841e0b
bff6c4f
 
 
c841e0b
bff6c4f
 
 
 
 
 
 
 
 
7a7c339
bff6c4f
 
 
 
c841e0b
2efe269
 
 
bff6c4f
 
 
2efe269
c841e0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import csv
import os
from datetime import datetime
import streamlit as st

def record_attendance_new(present_students: list, subject: str, max_roll_number=26):
    # Automatically get the current date in YYYY-MM-DD format
    date = datetime.now().strftime("%Y-%m-%d")
    length = len(present_students)
    st.success(f"{length} students' attendance is going to record.")

    # Convert present_students to integers
    present_students = list(map(int, present_students))
    print("Present Students:", present_students)  # Debugging line

    length = len(present_students)
    if present_students:
        ty = type(present_students[0]).__name__
    else:
        ty = "No students present in combined_student of present_student"
    # Get the name of the type as a string 
    st.success(f"{length} students' roll number is going to record {ty}.")

    # Get the path of the uploaded CSV file
    csv_file_path = f'{subject}.csv'
    print("CSV File Path:", csv_file_path)  # Debugging line

    # Read the existing CSV file
    if os.path.exists(csv_file_path):
        with open(csv_file_path, 'r', newline='') as csvfile:
            reader = csv.reader(csvfile)
            rows = list(reader)
    else:
        # If file doesn't exist, create a new one with header and roll numbers
        rows = [['Roll Number']]
        for roll_number in range(1, max_roll_number + 1):
            rows.append([str(roll_number)])

    # Add the new date column
    rows[0].append(date)
    date_col_index = len(rows[0]) - 1

    # Update attendance for each roll number
    for row in rows[1:]:
        roll_number = int(row[0])
        if roll_number in present_students:
            row.append('Present')
            print(f"Marking {roll_number}: Present")  # Debugging line
        else:
            row.append('Absent')
            print(f"Marking {roll_number}: Absent")  # Debugging line

    # Write the updated data back to the CSV file
    with open(csv_file_path, 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerows(rows)

    print(f"Attendance recorded for {len(present_students)} students on {date} in {subject}.")

    # Return the CSV contents as a string
    with open(csv_file_path, 'r') as file:
        csv_contents = file.read()
    
    return csv_contents