import gradio as gr import pandas as pd import os from datetime import datetime # Attendance Tracker Setup try: if not os.path.exists("attendance_records"): os.makedirs("attendance_records") except error: print(error) # Helper Functions def log_attendance(name, day, date, status): month = datetime.strptime(date, "%Y-%m-%d").strftime("%Y-%m") file_path = f"attendance_records/{month}.csv" # Load or create attendance sheet for the month if os.path.exists(file_path): df = pd.read_csv(file_path) else: df = pd.DataFrame(columns=["Name", "Day", "Date", "Status"]) # Add new attendance record new_entry = {"Name": name, "Day": day, "Date": date, "Status": status} df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True) # Save back to the file df.to_csv(file_path, index=False) return "Attendance logged successfully!" def calculate_fees(): attendance_summaries = [] for file in os.listdir("attendance_records"): if file.endswith(".csv"): file_path = os.path.join("attendance_records", file) df = pd.read_csv(file_path) # Calculate fees for each candidate fees_summary = df[df["Status"] == "Present"].groupby("Name").size() * (1000 / 12) # Example: Monthly fees divided by 12 fees_summary.name = "Fees" # Merge fees into the attendance sheet df = df.merge(fees_summary, on="Name", how="left") # Save updated file df.to_csv(file_path, index=False) # Summarize for all candidates attendance_summaries.append(df[["Name", "Fees"]].drop_duplicates()) if attendance_summaries: summary_df = pd.concat(attendance_summaries).drop_duplicates() summary_df.to_csv("attendance_records/fees_summary.csv", index=False) return "Fees calculated and updated successfully!" return "No attendance records found for fees calculation." # Gradio Interface def submit_attendance(name, day, date, status): return log_attendance(name, day, date, status) def generate_fees(): return calculate_fees() def is_month_end(): today = datetime.now() return (today.day == (pd.Period(today.strftime("%Y-%m")).days_in_month)) with gr.Blocks() as app: gr.Markdown("# Attendance Tracker") with gr.Row(): name = gr.Textbox(label="Name") day = gr.Textbox(label="Day") date = gr.Textbox(label="Date (YYYY-MM-DD)") status = gr.Radio(["Present", "Absent"], label="Status") submit_button = gr.Button("Submit Attendance") submit_message = gr.Textbox(label="Message", interactive=False) calculate_button = gr.Button("Calculate Fees", interactive=is_month_end()) calculate_message = gr.Textbox(label="Fees Calculation Message", interactive=False) submit_button.click(submit_attendance, inputs=[name, day, date, status], outputs=[submit_message]) calculate_button.click(generate_fees, outputs=[calculate_message]) def update_calculate_button(): return gr.update(interactive=is_month_end()) app.load(update_calculate_button, None, [calculate_button]) app.launch()