File size: 7,322 Bytes
b074821
 
8197815
b074821
8197815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b074821
8197815
 
 
 
b074821
8197815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b074821
 
 
 
 
 
3ea7ee1
8197815
 
b074821
8197815
 
 
 
b074821
8197815
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9555123
8197815
 
 
 
 
 
 
 
 
 
 
 
 
 
9555123
 
 
b074821
8197815
b074821
8197815
b074821
 
8197815
b074821
 
8197815
 
 
 
 
 
 
 
 
 
 
 
b074821
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import streamlit as st
import pandas as pd
import sqlite3
from datetime import datetime

# Database connection and creation
def create_connection():
    conn = sqlite3.connect("attendance.db")
    return conn

def create_tables():
    conn = create_connection()
    cursor = conn.cursor()

    # Create tables if not exist
    cursor.execute('''CREATE TABLE IF NOT EXISTS students (
                        student_id INTEGER PRIMARY KEY,
                        name TEXT,
                        semester TEXT,
                        attendance TEXT)''')
    
    cursor.execute('''CREATE TABLE IF NOT EXISTS teachers (
                        teacher_id INTEGER PRIMARY KEY,
                        name TEXT,
                        attendance TEXT)''')
    
    cursor.execute('''CREATE TABLE IF NOT EXISTS events (
                        event_id INTEGER PRIMARY KEY,
                        date TEXT,
                        event TEXT)''')

    cursor.execute('''CREATE TABLE IF NOT EXISTS users (
                        username TEXT PRIMARY KEY,
                        password TEXT,
                        role TEXT)''')
    
    conn.commit()
    conn.close()

# Initialize database
create_tables()

# Function to check login credentials
def check_login(username, password):
    conn = create_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, password))
    user = cursor.fetchone()
    conn.close()
    return user

# Admin login
def admin_login():
    st.sidebar.header("Admin Login")
    admin_username = st.text_input('Username:')
    admin_password = st.text_input('Password:', type='password')

    if st.button('Login'):
        # Check credentials for 'uni' as username and 'admin123' as password
        user = check_login(admin_username, admin_password)
        if user and user[2] == 'admin':  # Role should be 'admin'
            st.sidebar.success("Logged in as Admin")
            return True
        elif user and user[2] == 'teacher':  # Role should be 'teacher'
            st.sidebar.success("Logged in as Teacher")
            return False
        else:
            st.sidebar.error('Invalid credentials')
            return False
    return False

# Function to add students, teachers, and events
def add_student():
    st.header("Add Student")
    name = st.text_input("Student Name:")
    semester = st.selectbox("Select Semester:", ["Semester 1", "Semester 2", "Semester 3"])
    
    if st.button("Add Student"):
        conn = create_connection()
        cursor = conn.cursor()
        cursor.execute("INSERT INTO students (name, semester, attendance) VALUES (?, ?, ?)", (name, semester, 'Absent'))
        conn.commit()
        conn.close()
        st.success(f"Student {name} added successfully!")

def add_teacher():
    st.header("Add Teacher")
    name = st.text_input("Teacher Name:")
    
    if st.button("Add Teacher"):
        conn = create_connection()
        cursor = conn.cursor()
        cursor.execute("INSERT INTO teachers (name, attendance) VALUES (?, ?)", (name, 'Absent'))
        conn.commit()
        conn.close()
        st.success(f"Teacher {name} added successfully!")

def add_event():
    st.header("Add Event")
    event_name = st.text_input("Event Name:")
    event_date = st.date_input("Event Date:", datetime.today())
    
    if st.button("Add Event"):
        conn = create_connection()
        cursor = conn.cursor()
        cursor.execute("INSERT INTO events (date, event) VALUES (?, ?)", (event_date, event_name))
        conn.commit()
        conn.close()
        st.success(f"Event {event_name} added for {event_date}")

def mark_attendance():
    st.header("Mark Attendance")
    name = st.selectbox("Select Student/Teacher", ["Select", "Student", "Teacher"])
    
    if name == "Student":
        student_name = st.selectbox("Select Student:", [row[1] for row in get_all_students()])
        attendance_status = st.radio("Attendance Status:", ["Present", "Absent"])
        if st.button("Mark Attendance for Student"):
            update_student_attendance(student_name, attendance_status)
            st.success(f"Attendance marked for {student_name} as {attendance_status}")
    
    elif name == "Teacher":
        teacher_name = st.selectbox("Select Teacher:", [row[1] for row in get_all_teachers()])
        attendance_status = st.radio("Attendance Status:", ["Present", "Absent"])
        if st.button("Mark Attendance for Teacher"):
            update_teacher_attendance(teacher_name, attendance_status)
            st.success(f"Attendance marked for {teacher_name} as {attendance_status}")

def get_all_students():
    conn = create_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM students")
    students = cursor.fetchall()
    conn.close()
    return students

def get_all_teachers():
    conn = create_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM teachers")
    teachers = cursor.fetchall()
    conn.close()
    return teachers

def update_student_attendance(student_name, status):
    conn = create_connection()
    cursor = conn.cursor()
    cursor.execute("UPDATE students SET attendance = ? WHERE name = ?", (status, student_name))
    conn.commit()
    conn.close()

def update_teacher_attendance(teacher_name, status):
    conn = create_connection()
    cursor = conn.cursor()
    cursor.execute("UPDATE teachers SET attendance = ? WHERE name = ?", (status, teacher_name))
    conn.commit()
    conn.close()

# Attendance Analytics without matplotlib
def attendance_analytics():
    st.header("Attendance Analytics")
    student_attendance = pd.DataFrame(get_all_students(), columns=["ID", "Name", "Semester", "Attendance"])
    teacher_attendance = pd.DataFrame(get_all_teachers(), columns=["ID", "Name", "Attendance"])

    student_attendance['Attendance'] = student_attendance['Attendance'].apply(lambda x: 1 if x == "Present" else 0)
    teacher_attendance['Attendance'] = teacher_attendance['Attendance'].apply(lambda x: 1 if x == "Present" else 0)

    student_percentage = student_attendance['Attendance'].mean() * 100
    teacher_percentage = teacher_attendance['Attendance'].mean() * 100

    st.write(f"Student Attendance: {student_percentage:.2f}%")
    st.write(f"Teacher Attendance: {teacher_percentage:.2f}%")

    st.subheader("Student and Teacher Attendance Percentage")
    st.write(f"Student Attendance Percentage: {student_percentage:.2f}%")
    st.write(f"Teacher Attendance Percentage: {teacher_percentage:.2f}%")

# Main function
def main():
    st.title("Student & Teacher Attendance Management System")

    if admin_login():
        menu = ["Add Student", "Add Teacher", "Add Event", "Mark Attendance", "View Attendance", "Attendance Analytics"]
        choice = st.sidebar.selectbox("Select an option", menu)

        if choice == "Add Student":
            add_student()
        elif choice == "Add Teacher":
            add_teacher()
        elif choice == "Add Event":
            add_event()
        elif choice == "Mark Attendance":
            mark_attendance()
        elif choice == "View Attendance":
            view_attendance()
        elif choice == "Attendance Analytics":
            attendance_analytics()

if __name__ == "__main__":
    main()