Spaces:
Sleeping
Sleeping
shamimjony1000
commited on
Commit
•
9afaf17
1
Parent(s):
3f2d5fb
Update task_operations.py
Browse files- task_operations.py +70 -62
task_operations.py
CHANGED
@@ -1,62 +1,70 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
import os
|
3 |
-
from datetime import datetime
|
4 |
-
import streamlit as st
|
5 |
-
|
6 |
-
class TaskManager:
|
7 |
-
TASKS_FILE = "tasks.csv"
|
8 |
-
CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']
|
9 |
-
|
10 |
-
def __init__(self):
|
11 |
-
if 'tasks' not in st.session_state:
|
12 |
-
st.session_state.tasks = self.load_tasks()
|
13 |
-
|
14 |
-
def load_tasks(self):
|
15 |
-
if os.path.exists(self.TASKS_FILE):
|
16 |
-
if os.path.getsize(self.TASKS_FILE) > 0:
|
17 |
-
return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
|
18 |
-
else:
|
19 |
-
return []
|
20 |
-
else:
|
21 |
-
df = pd.DataFrame(columns=["Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
|
22 |
-
df.to_csv(self.TASKS_FILE, index=False)
|
23 |
-
return []
|
24 |
-
|
25 |
-
def save_tasks(self, tasks):
|
26 |
-
df = pd.DataFrame(tasks)
|
27 |
-
df.to_csv(self.TASKS_FILE, index=False)
|
28 |
-
|
29 |
-
def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
|
30 |
-
task_time_full = datetime.combine(datetime.today(), task_time)
|
31 |
-
task_entry = {
|
32 |
-
"Task Name": task_name,
|
33 |
-
"Task Time": task_time_full,
|
34 |
-
"Task Duration (hours)": int(task_duration_hours),
|
35 |
-
"Task Duration (minutes)": int(task_duration_minutes),
|
36 |
-
"Category": task_category
|
37 |
-
}
|
38 |
-
st.session_state.tasks.append(task_entry)
|
39 |
-
self.save_tasks(st.session_state.tasks)
|
40 |
-
|
41 |
-
def delete_task_by_name(self, task_name):
|
42 |
-
for index, task in enumerate(st.session_state.tasks):
|
43 |
-
if task['Task Name'] == task_name:
|
44 |
-
st.session_state.tasks.pop(index)
|
45 |
-
self.save_tasks(st.session_state.tasks)
|
46 |
-
return True
|
47 |
-
return False
|
48 |
-
|
49 |
-
def generate_report(self, timeframe):
|
50 |
-
df = pd.DataFrame(st.session_state.tasks)
|
51 |
-
if
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import os
|
3 |
+
from datetime import datetime
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
class TaskManager:
|
7 |
+
TASKS_FILE = "tasks.csv"
|
8 |
+
CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']
|
9 |
+
|
10 |
+
def __init__(self):
|
11 |
+
if 'tasks' not in st.session_state:
|
12 |
+
st.session_state.tasks = self.load_tasks()
|
13 |
+
|
14 |
+
def load_tasks(self):
|
15 |
+
if os.path.exists(self.TASKS_FILE):
|
16 |
+
if os.path.getsize(self.TASKS_FILE) > 0:
|
17 |
+
return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
|
18 |
+
else:
|
19 |
+
return []
|
20 |
+
else:
|
21 |
+
df = pd.DataFrame(columns=["Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
|
22 |
+
df.to_csv(self.TASKS_FILE, index=False)
|
23 |
+
return []
|
24 |
+
|
25 |
+
def save_tasks(self, tasks):
|
26 |
+
df = pd.DataFrame(tasks)
|
27 |
+
df.to_csv(self.TASKS_FILE, index=False)
|
28 |
+
|
29 |
+
def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
|
30 |
+
task_time_full = datetime.combine(datetime.today(), task_time)
|
31 |
+
task_entry = {
|
32 |
+
"Task Name": task_name,
|
33 |
+
"Task Time": task_time_full,
|
34 |
+
"Task Duration (hours)": int(task_duration_hours),
|
35 |
+
"Task Duration (minutes)": int(task_duration_minutes),
|
36 |
+
"Category": task_category
|
37 |
+
}
|
38 |
+
st.session_state.tasks.append(task_entry)
|
39 |
+
self.save_tasks(st.session_state.tasks)
|
40 |
+
|
41 |
+
def delete_task_by_name(self, task_name):
|
42 |
+
for index, task in enumerate(st.session_state.tasks):
|
43 |
+
if task['Task Name'] == task_name:
|
44 |
+
st.session_state.tasks.pop(index)
|
45 |
+
self.save_tasks(st.session_state.tasks)
|
46 |
+
return True
|
47 |
+
return False
|
48 |
+
|
49 |
+
def generate_report(self, timeframe):
|
50 |
+
df = pd.DataFrame(st.session_state.tasks)
|
51 |
+
if df.empty:
|
52 |
+
return pd.DataFrame() # Return empty DataFrame if no tasks are present
|
53 |
+
|
54 |
+
# Ensure the 'Task Time' is in datetime format
|
55 |
+
df['Task Time'] = pd.to_datetime(df['Task Time'])
|
56 |
+
|
57 |
+
if timeframe == 'daily':
|
58 |
+
report = df[df['Task Time'].dt.date == pd.Timestamp.today().date()]
|
59 |
+
elif timeframe == 'weekly':
|
60 |
+
week_start = pd.Timestamp.today() - pd.DateOffset(days=pd.Timestamp.today().dayofweek)
|
61 |
+
report = df[(df['Task Time'] >= week_start) & (df['Task Time'] < pd.Timestamp.today() + pd.DateOffset(days=1))]
|
62 |
+
elif timeframe == 'monthly':
|
63 |
+
report = df[df['Task Time'].dt.month == pd.Timestamp.today().month]
|
64 |
+
elif timeframe == 'yearly':
|
65 |
+
report = df[df['Task Time'].dt.year == pd.Timestamp.today().year]
|
66 |
+
else:
|
67 |
+
report = pd.DataFrame() # Empty DataFrame for unsupported timeframes
|
68 |
+
|
69 |
+
report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0
|
70 |
+
return report
|