Spaces:
Sleeping
Sleeping
File size: 4,876 Bytes
6fb7683 1d44b76 6fb7683 7e8259a 401e8b9 93a0f5c 9afaf17 6fb7683 401e8b9 6fb7683 9afaf17 93a0f5c 401e8b9 93a0f5c 3065bc5 401e8b9 6fb7683 3065bc5 ad23ffc 6fb7683 3065bc5 4cd768f 6fb7683 401e8b9 3065bc5 401e8b9 3065bc5 401e8b9 93a0f5c 3065bc5 401e8b9 3065bc5 93a0f5c 1d44b76 6fb7683 7e8259a 6fb7683 1d44b76 3065bc5 401e8b9 3065bc5 69e3818 4cd768f 6fb7683 4cd768f 6fb7683 69e3818 6fb7683 69e3818 6fb7683 69e3818 6fb7683 69e3818 6fb7683 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
import os
import pandas as pd
from datetime import datetime
import streamlit as st
import uuid # For generating unique IDs
import shutil # To create a backup of the file
class TaskManager:
TASKS_FILE = "tasks.csv"
BACKUP_FILE = "tasks_backup.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 backup_csv(self):
"""Create a backup of the current tasks file."""
if os.path.exists(self.TASKS_FILE):
shutil.copy(self.TASKS_FILE, self.BACKUP_FILE)
def restore_from_backup(self):
"""Restore tasks from backup if needed."""
if os.path.exists(self.BACKUP_FILE):
shutil.copy(self.BACKUP_FILE, self.TASKS_FILE)
def load_tasks(self):
# Check if the file exists and has data
if os.path.exists(self.TASKS_FILE) and os.path.getsize(self.TASKS_FILE) > 0:
try:
return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
except pd.errors.EmptyDataError:
return [] # Return empty list if CSV is empty
else:
# If file doesn't exist, initialize it with an empty DataFrame and correct columns
df = pd.DataFrame(columns=["Task ID", "Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
df.to_csv(self.TASKS_FILE, index=False)
return [] # Return an empty list to the session state
def save_tasks(self, tasks):
"""Save tasks to the CSV file after making a backup."""
if tasks: # Only save if tasks are not empty
# Backup the current file before saving
self.backup_csv()
df = pd.DataFrame(tasks)
df.to_csv(self.TASKS_FILE, index=False)
else:
# Prevent overwriting the file with an empty DataFrame
st.warning("No tasks to save. File write skipped to prevent data loss.")
def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
# Ensure valid task details are being added
if task_name and task_category:
task_time_full = datetime.combine(datetime.today(), task_time)
task_id = str(uuid.uuid4()) # Generate a unique ID using uuid4
task_entry = {
"Task ID": task_id,
"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)
st.success(f"Task '{task_name}' added successfully!")
# Reload tasks to ensure consistency
st.session_state.tasks = self.load_tasks()
else:
st.error("Task name and category are required.")
def delete_task_by_id(self, task_id):
task_found = False
for index, task in enumerate(st.session_state.tasks):
if task['Task ID'] == task_id:
st.session_state.tasks.pop(index)
task_found = True
break
if task_found:
self.save_tasks(st.session_state.tasks)
st.success(f"Task with ID '{task_id}' deleted.")
# Reload tasks to ensure consistency
st.session_state.tasks = self.load_tasks()
else:
st.error(f"Task with ID '{task_id}' not found.")
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
|