File size: 3,436 Bytes
ed92cf4
 
 
 
9e14ec7
ed92cf4
 
 
 
 
 
 
9d3a170
 
 
 
ed92cf4
 
 
 
 
 
 
 
77af0f9
ed92cf4
 
77af0f9
ed92cf4
 
 
 
 
 
 
 
77af0f9
 
9e14ec7
ed92cf4
 
 
 
 
9d3a170
 
77af0f9
9d3a170
 
09c1ec7
9d3a170
7496feb
 
9d3a170
7496feb
9d3a170
7496feb
 
 
 
 
 
 
 
 
 
 
 
77af0f9
 
 
 
 
 
 
 
 
 
 
 
2553622
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
from attendance_system import AttendanceSystem
from record_attendance import record_attendance_new
import streamlit as st

# Configuration
UPLOAD_FOLDER = 'uploads'
DATABASE_DIR = 'database'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(DATABASE_DIR, exist_ok=True)
attendance_system = AttendanceSystem(DATABASE_DIR)

# Initialize session state
if 'present_students' not in st.session_state:
    st.session_state.present_students = []

# Streamlit application
st.title("Attendance System")

# Index page
st.subheader("Select Course to View or Upload Files")

# Display available courses
courses = ["RL", "OT", "CLOUD"]
selected_course = st.selectbox("Choose a Course", courses, key="course_selector")

# File upload section
uploaded_file = st.file_uploader("Upload Attendance File", type=['csv'], key="attendance_file_uploader")
if uploaded_file is not None:
    file_path = os.path.join(UPLOAD_FOLDER, f"{selected_course}.csv")
    with open(file_path, "wb") as f:
        f.write(uploaded_file.getbuffer())
    st.success(f"{selected_course}.csv File uploaded successfully")

# Take Attendance
st.subheader("Record Attendance")
attendance_date = st.date_input("Select Date", key="attendance_date")
image_file = st.file_uploader("Upload Image for Attendance", type=['jpg', 'png', 'jpeg'], key="attendance_image_uploader")

if st.button("Take Attendance"):
    if image_file is not None and selected_course and attendance_date:
        image_path = os.path.join(UPLOAD_FOLDER, image_file.name)
        with open(image_path, "wb") as f:
            f.write(image_file.getbuffer())
        st.session_state.present_students = attendance_system.record_attendance(selected_course, attendance_date, image_path)
        st.write(st.session_state.present_students)
        # Display the count of present students
        length = len(st.session_state.present_students)
        ty = type(st.session_state.present_students).__name__  # Get the name of the type as a string
        st.success(f"{length} students' attendance completed successfully.")
        st.json(st.session_state.present_students)

if st.button("Mark Attendance"):
    if st.session_state.present_students:
        # Convert present_students to integers
        present_students_int = [int(student) for student in st.session_state.present_students]
        csv_contents = record_attendance_new(present_students_int, selected_course)
        st.success("Attendance marked successfully")
        
        # Create a download button
        st.download_button(
            label="Download updated attendance file",
            data=csv_contents,
            file_name=f"{selected_course}_attendance.csv",
            mime='text/csv'
        )
    else:
        st.warning("No attendance data to mark. Please take attendance first.")

# Add to database
st.subheader("Add Student to Database")
roll_number = st.text_input("Enter Roll Number", key="roll_number_input")
database_file = st.file_uploader("Upload Student Image", type=['jpg', 'png', 'jpeg'], key="database_image_uploader")

if st.button("Add to Database"):
    if database_file is not None and roll_number:
        db_image_path = os.path.join(DATABASE_DIR, f"{roll_number}.jpg")
        with open(db_image_path, "wb") as f:
            f.write(database_file.getbuffer())
        attendance_system.add_to_database(roll_number, db_image_path)
        st.success("Image added to database successfully")