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