Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import csv
|
| 3 |
from datetime import date
|
| 4 |
-
from datasets import load_dataset
|
| 5 |
from huggingface_hub import HfApi, HfFolder, hf_hub_download
|
| 6 |
import os
|
| 7 |
|
|
@@ -35,11 +34,15 @@ class AttendanceTracker:
|
|
| 35 |
local_file_path = self.load_original_attendance_list(course)
|
| 36 |
|
| 37 |
# Load existing data from CSV
|
| 38 |
-
existing_data =
|
| 39 |
with open(local_file_path, "r") as f:
|
| 40 |
reader = csv.reader(f)
|
| 41 |
header = next(reader)
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Read the new attendance file
|
| 45 |
new_attendance_list = []
|
|
@@ -51,34 +54,31 @@ class AttendanceTracker:
|
|
| 51 |
current_date = date.today().strftime("%d-%m-%Y")
|
| 52 |
new_column_name = self.get_new_column_name(header, current_date)
|
| 53 |
|
| 54 |
-
# Update header with new column name if it does not exist
|
| 55 |
-
if new_column_name not in header:
|
| 56 |
-
header.append(new_column_name)
|
| 57 |
-
|
| 58 |
# Update existing records with new attendance data
|
| 59 |
-
name_to_index = {row[0].lower(): i for i, row in enumerate(existing_data)}
|
| 60 |
-
|
| 61 |
-
for row in existing_data:
|
| 62 |
-
name = row[0].lower()
|
| 63 |
-
if name in new_attendance_list:
|
| 64 |
-
# Mark present in the new column
|
| 65 |
-
row.append("Present")
|
| 66 |
-
else:
|
| 67 |
-
# Mark absent in the new column
|
| 68 |
-
row.append("Absent")
|
| 69 |
-
|
| 70 |
-
# Append new rows for any new names in the attendance list that weren't previously in the file
|
| 71 |
for name in new_attendance_list:
|
| 72 |
-
if name
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Save the updated attendance list locally
|
| 78 |
with open(local_file_path, "w", newline="") as f:
|
| 79 |
writer = csv.writer(f)
|
| 80 |
-
writer.writerow(
|
| 81 |
-
writer.writerows(
|
| 82 |
|
| 83 |
# Upload the updated file back to the Hugging Face dataset
|
| 84 |
api = HfApi()
|
|
@@ -107,4 +107,4 @@ iface = gr.Interface(
|
|
| 107 |
description="Upload a CSV file of today's attendance to update the attendance file for the selected course."
|
| 108 |
)
|
| 109 |
|
| 110 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import csv
|
| 3 |
from datetime import date
|
|
|
|
| 4 |
from huggingface_hub import HfApi, HfFolder, hf_hub_download
|
| 5 |
import os
|
| 6 |
|
|
|
|
| 34 |
local_file_path = self.load_original_attendance_list(course)
|
| 35 |
|
| 36 |
# Load existing data from CSV
|
| 37 |
+
existing_data = {}
|
| 38 |
with open(local_file_path, "r") as f:
|
| 39 |
reader = csv.reader(f)
|
| 40 |
header = next(reader)
|
| 41 |
+
for row in reader:
|
| 42 |
+
if row:
|
| 43 |
+
name = row[0].lower() # Convert to lower case for case-insensitive matching
|
| 44 |
+
statuses = row[1:]
|
| 45 |
+
existing_data[name] = statuses
|
| 46 |
|
| 47 |
# Read the new attendance file
|
| 48 |
new_attendance_list = []
|
|
|
|
| 54 |
current_date = date.today().strftime("%d-%m-%Y")
|
| 55 |
new_column_name = self.get_new_column_name(header, current_date)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Update existing records with new attendance data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
for name in new_attendance_list:
|
| 59 |
+
if name in existing_data:
|
| 60 |
+
# Append new status for the new column
|
| 61 |
+
existing_data[name].append("Present")
|
| 62 |
+
else:
|
| 63 |
+
# Add status "Absent" for the new date for students not in the new attendance list
|
| 64 |
+
for student_name in existing_data:
|
| 65 |
+
existing_data[student_name].append("Absent")
|
| 66 |
+
|
| 67 |
+
# Prepare updated data including the new date column
|
| 68 |
+
updated_rows = []
|
| 69 |
+
updated_header = header[:]
|
| 70 |
+
if new_column_name not in updated_header:
|
| 71 |
+
updated_header.append(new_column_name)
|
| 72 |
+
|
| 73 |
+
for name, statuses in existing_data.items():
|
| 74 |
+
row = [name] + statuses
|
| 75 |
+
updated_rows.append(row)
|
| 76 |
|
| 77 |
# Save the updated attendance list locally
|
| 78 |
with open(local_file_path, "w", newline="") as f:
|
| 79 |
writer = csv.writer(f)
|
| 80 |
+
writer.writerow(updated_header)
|
| 81 |
+
writer.writerows(updated_rows)
|
| 82 |
|
| 83 |
# Upload the updated file back to the Hugging Face dataset
|
| 84 |
api = HfApi()
|
|
|
|
| 107 |
description="Upload a CSV file of today's attendance to update the attendance file for the selected course."
|
| 108 |
)
|
| 109 |
|
| 110 |
+
iface.launch()
|