Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio import Interface
|
| 3 |
+
import csv
|
| 4 |
+
from datetime import date
|
| 5 |
+
|
| 6 |
+
class AttendanceTracker:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
self.original_attendance_list = []
|
| 9 |
+
self.attendance_file_path = ""
|
| 10 |
+
|
| 11 |
+
# Load original attendance list
|
| 12 |
+
self.load_original_attendance_list()
|
| 13 |
+
|
| 14 |
+
def load_original_attendance_list(self):
|
| 15 |
+
# Load original attendance list from a file (e.g. original_attendance_list.csv)
|
| 16 |
+
with open("original_attendance.csv", "r") as f:
|
| 17 |
+
reader = csv.reader(f)
|
| 18 |
+
for row in reader:
|
| 19 |
+
self.original_attendance_list.append(row[0])
|
| 20 |
+
|
| 21 |
+
def mark_attendance(self, attendance_file):
|
| 22 |
+
# Read attendance file
|
| 23 |
+
attendance_list = []
|
| 24 |
+
with open(attendance_file.name, "r") as f:
|
| 25 |
+
reader = csv.reader(f)
|
| 26 |
+
attendance_list = [row[0] for row in reader if row]
|
| 27 |
+
|
| 28 |
+
# Get current date
|
| 29 |
+
current_date = date.today().strftime("%d-%m-%Y")
|
| 30 |
+
|
| 31 |
+
# Create a dictionary to store attendance
|
| 32 |
+
attendance_dict = {name.split()[0]: "Absent" for name in self.original_attendance_list if len(name.split()) > 0}
|
| 33 |
+
|
| 34 |
+
# Mark present students
|
| 35 |
+
for name in attendance_list:
|
| 36 |
+
if name.split()[0] in attendance_dict:
|
| 37 |
+
attendance_dict[name.split()[0]] = "Present"
|
| 38 |
+
|
| 39 |
+
# Save marked attendance to the original file
|
| 40 |
+
with open("original_attendance.csv", "r") as f:
|
| 41 |
+
reader = csv.reader(f)
|
| 42 |
+
rows = list(reader)
|
| 43 |
+
|
| 44 |
+
# Add header for the new column
|
| 45 |
+
rows[0].append(current_date)
|
| 46 |
+
|
| 47 |
+
# Update the rows with attendance data
|
| 48 |
+
for row in range(1, len(rows)):
|
| 49 |
+
if rows[row] and len(rows[row][0].split()) > 0 and rows[row][0].split()[0] in attendance_dict:
|
| 50 |
+
rows[row].append(attendance_dict[rows[row][0].split()[0]])
|
| 51 |
+
else:
|
| 52 |
+
rows[row].append("Absent")
|
| 53 |
+
|
| 54 |
+
# Save the updated attendance list
|
| 55 |
+
with open("original_attendance.csv", "w", newline="") as f:
|
| 56 |
+
writer = csv.writer(f)
|
| 57 |
+
writer.writerows(rows)
|
| 58 |
+
|
| 59 |
+
return "Attendance marked successfully!"
|
| 60 |
+
|
| 61 |
+
tracker = AttendanceTracker()
|
| 62 |
+
|
| 63 |
+
def upload_and_mark(file):
|
| 64 |
+
return tracker.mark_attendance(file)
|
| 65 |
+
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=upload_and_mark,
|
| 68 |
+
inputs=gr.File(label="Upload Attendance File (CSV)"),
|
| 69 |
+
outputs=gr.Textbox(label="Status"),
|
| 70 |
+
title="Attendance Tracker",
|
| 71 |
+
description="Upload a CSV file of today's attendance to update the original attendance file."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
iface.launch()
|