PRIYANSHUDHAKED's picture
Update app.py
9d3a170 verified
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")