# components/task_operations.py import pandas as pd import os from datetime import datetime import streamlit as st class TaskManager: TASKS_FILE = "tasks.csv" CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer'] def __init__(self): if 'tasks' not in st.session_state: st.session_state.tasks = self.load_tasks() def load_tasks(self): if os.path.exists(self.TASKS_FILE): if os.path.getsize(self.TASKS_FILE) > 0: return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records') else: return [] else: df = pd.DataFrame(columns=["Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"]) df.to_csv(self.TASKS_FILE, index=False) return [] def save_tasks(self, tasks): df = pd.DataFrame(tasks) df.to_csv(self.TASKS_FILE, index=False) def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category): task_time_full = datetime.combine(datetime.today(), task_time) task_entry = { "Task Name": task_name, "Task Time": task_time_full, "Task Duration (hours)": int(task_duration_hours), "Task Duration (minutes)": int(task_duration_minutes), "Category": task_category } st.session_state.tasks.append(task_entry) self.save_tasks(st.session_state.tasks) def delete_task_by_name(self, task_name): for index, task in enumerate(st.session_state.tasks): if task['Task Name'] == task_name: st.session_state.tasks.pop(index) self.save_tasks(st.session_state.tasks) return True return False def generate_report(self, timeframe): df = pd.DataFrame(st.session_state.tasks) if df.empty: return pd.DataFrame() # Return empty DataFrame if no tasks are present # Ensure the 'Task Time' is in datetime format df['Task Time'] = pd.to_datetime(df['Task Time']) if timeframe == 'daily': report = df[df['Task Time'].dt.date == pd.Timestamp.today().date()] elif timeframe == 'weekly': week_start = pd.Timestamp.today() - pd.DateOffset(days=pd.Timestamp.today().dayofweek) report = df[(df['Task Time'] >= week_start) & (df['Task Time'] < pd.Timestamp.today() + pd.DateOffset(days=1))] elif timeframe == 'monthly': report = df[df['Task Time'].dt.month == pd.Timestamp.today().month] elif timeframe == 'yearly': report = df[df['Task Time'].dt.year == pd.Timestamp.today().year] else: report = pd.DataFrame() # Empty DataFrame for unsupported timeframes report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0 return report