Spaces:
Sleeping
Sleeping
shamimjony1000
commited on
Commit
•
401e8b9
1
Parent(s):
3065bc5
Update task_operations.py
Browse files- task_operations.py +22 -6
task_operations.py
CHANGED
@@ -3,24 +3,34 @@ import pandas as pd
|
|
3 |
from datetime import datetime
|
4 |
import streamlit as st
|
5 |
import uuid # For generating unique IDs
|
|
|
6 |
|
7 |
class TaskManager:
|
8 |
TASKS_FILE = "tasks.csv"
|
|
|
9 |
CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']
|
10 |
|
11 |
def __init__(self):
|
12 |
if 'tasks' not in st.session_state:
|
13 |
st.session_state.tasks = self.load_tasks()
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def load_tasks(self):
|
16 |
# Check if the file exists and has data
|
17 |
if os.path.exists(self.TASKS_FILE) and os.path.getsize(self.TASKS_FILE) > 0:
|
18 |
try:
|
19 |
-
# Load tasks into session state from CSV
|
20 |
return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
|
21 |
except pd.errors.EmptyDataError:
|
22 |
-
#
|
23 |
-
return []
|
24 |
else:
|
25 |
# If file doesn't exist, initialize it with an empty DataFrame and correct columns
|
26 |
df = pd.DataFrame(columns=["Task ID", "Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
|
@@ -28,12 +38,15 @@ class TaskManager:
|
|
28 |
return [] # Return an empty list to the session state
|
29 |
|
30 |
def save_tasks(self, tasks):
|
|
|
31 |
if tasks: # Only save if tasks are not empty
|
|
|
|
|
32 |
df = pd.DataFrame(tasks)
|
33 |
df.to_csv(self.TASKS_FILE, index=False)
|
34 |
else:
|
35 |
-
#
|
36 |
-
st.warning("No tasks to save.
|
37 |
|
38 |
def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
|
39 |
# Ensure valid task details are being added
|
@@ -51,6 +64,8 @@ class TaskManager:
|
|
51 |
st.session_state.tasks.append(task_entry)
|
52 |
self.save_tasks(st.session_state.tasks)
|
53 |
st.success(f"Task '{task_name}' added successfully!")
|
|
|
|
|
54 |
else:
|
55 |
st.error("Task name and category are required.")
|
56 |
|
@@ -64,9 +79,10 @@ class TaskManager:
|
|
64 |
if task_found:
|
65 |
self.save_tasks(st.session_state.tasks)
|
66 |
st.success(f"Task with ID '{task_id}' deleted.")
|
|
|
|
|
67 |
else:
|
68 |
st.error(f"Task with ID '{task_id}' not found.")
|
69 |
-
return task_found
|
70 |
|
71 |
def generate_report(self, timeframe):
|
72 |
df = pd.DataFrame(st.session_state.tasks)
|
|
|
3 |
from datetime import datetime
|
4 |
import streamlit as st
|
5 |
import uuid # For generating unique IDs
|
6 |
+
import shutil # To create a backup of the file
|
7 |
|
8 |
class TaskManager:
|
9 |
TASKS_FILE = "tasks.csv"
|
10 |
+
BACKUP_FILE = "tasks_backup.csv"
|
11 |
CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']
|
12 |
|
13 |
def __init__(self):
|
14 |
if 'tasks' not in st.session_state:
|
15 |
st.session_state.tasks = self.load_tasks()
|
16 |
|
17 |
+
def backup_csv(self):
|
18 |
+
"""Create a backup of the current tasks file."""
|
19 |
+
if os.path.exists(self.TASKS_FILE):
|
20 |
+
shutil.copy(self.TASKS_FILE, self.BACKUP_FILE)
|
21 |
+
|
22 |
+
def restore_from_backup(self):
|
23 |
+
"""Restore tasks from backup if needed."""
|
24 |
+
if os.path.exists(self.BACKUP_FILE):
|
25 |
+
shutil.copy(self.BACKUP_FILE, self.TASKS_FILE)
|
26 |
+
|
27 |
def load_tasks(self):
|
28 |
# Check if the file exists and has data
|
29 |
if os.path.exists(self.TASKS_FILE) and os.path.getsize(self.TASKS_FILE) > 0:
|
30 |
try:
|
|
|
31 |
return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
|
32 |
except pd.errors.EmptyDataError:
|
33 |
+
return [] # Return empty list if CSV is empty
|
|
|
34 |
else:
|
35 |
# If file doesn't exist, initialize it with an empty DataFrame and correct columns
|
36 |
df = pd.DataFrame(columns=["Task ID", "Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
|
|
|
38 |
return [] # Return an empty list to the session state
|
39 |
|
40 |
def save_tasks(self, tasks):
|
41 |
+
"""Save tasks to the CSV file after making a backup."""
|
42 |
if tasks: # Only save if tasks are not empty
|
43 |
+
# Backup the current file before saving
|
44 |
+
self.backup_csv()
|
45 |
df = pd.DataFrame(tasks)
|
46 |
df.to_csv(self.TASKS_FILE, index=False)
|
47 |
else:
|
48 |
+
# Prevent overwriting the file with an empty DataFrame
|
49 |
+
st.warning("No tasks to save. File write skipped to prevent data loss.")
|
50 |
|
51 |
def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
|
52 |
# Ensure valid task details are being added
|
|
|
64 |
st.session_state.tasks.append(task_entry)
|
65 |
self.save_tasks(st.session_state.tasks)
|
66 |
st.success(f"Task '{task_name}' added successfully!")
|
67 |
+
# Reload tasks to ensure consistency
|
68 |
+
st.session_state.tasks = self.load_tasks()
|
69 |
else:
|
70 |
st.error("Task name and category are required.")
|
71 |
|
|
|
79 |
if task_found:
|
80 |
self.save_tasks(st.session_state.tasks)
|
81 |
st.success(f"Task with ID '{task_id}' deleted.")
|
82 |
+
# Reload tasks to ensure consistency
|
83 |
+
st.session_state.tasks = self.load_tasks()
|
84 |
else:
|
85 |
st.error(f"Task with ID '{task_id}' not found.")
|
|
|
86 |
|
87 |
def generate_report(self, timeframe):
|
88 |
df = pd.DataFrame(st.session_state.tasks)
|