Spaces:
Sleeping
Sleeping
shamimjony1000
commited on
Update task_operations.py
Browse files- task_operations.py +24 -80
task_operations.py
CHANGED
@@ -1,84 +1,28 @@
|
|
1 |
-
# components/task_operations.py
|
2 |
-
|
3 |
-
import pandas as pd
|
4 |
-
import os
|
5 |
-
from datetime import datetime, time
|
6 |
-
import streamlit as st
|
7 |
-
import pytz
|
8 |
-
|
9 |
class TaskManager:
|
10 |
-
TASKS_FILE = "tasks.csv"
|
11 |
-
CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']
|
12 |
-
|
13 |
def __init__(self):
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
if
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
else:
|
32 |
-
|
33 |
-
df.to_csv(self.TASKS_FILE, index=False)
|
34 |
-
return []
|
35 |
-
|
36 |
-
def save_tasks(self, tasks):
|
37 |
-
# Convert local time back to UTC for saving
|
38 |
-
tasks_copy = []
|
39 |
-
for task in tasks:
|
40 |
-
task_copy = task.copy()
|
41 |
-
task_copy['Task Time'] = task_copy['Task Time'].astimezone(self.utc_timezone)
|
42 |
-
tasks_copy.append(task_copy)
|
43 |
-
|
44 |
-
df = pd.DataFrame(tasks_copy)
|
45 |
-
df.to_csv(self.TASKS_FILE, index=False)
|
46 |
-
|
47 |
-
def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
|
48 |
-
task_time_full = datetime.combine(datetime.today(), task_time)
|
49 |
-
# Localize to your local timezone and then convert to UTC for consistency
|
50 |
-
task_time_full = self.local_timezone.localize(task_time_full).astimezone(self.utc_timezone)
|
51 |
-
task_entry = {
|
52 |
-
"Task Name": task_name,
|
53 |
-
"Task Time": task_time_full,
|
54 |
-
"Task Duration (hours)": int(task_duration_hours),
|
55 |
-
"Task Duration (minutes)": int(task_duration_minutes),
|
56 |
-
"Category": task_category
|
57 |
-
}
|
58 |
-
st.session_state.tasks.append(task_entry)
|
59 |
-
self.save_tasks(st.session_state.tasks)
|
60 |
-
|
61 |
-
def generate_report(self, timeframe):
|
62 |
-
df = pd.DataFrame(st.session_state.tasks)
|
63 |
-
if df.empty:
|
64 |
-
return pd.DataFrame() # Return empty DataFrame if no tasks are present
|
65 |
-
|
66 |
-
# Ensure 'Task Time' is already timezone-aware and convert it to local time for the report
|
67 |
-
df['Task Time'] = pd.to_datetime(df['Task Time']).apply(lambda x: x.tz_convert(self.local_timezone))
|
68 |
-
|
69 |
-
today = pd.Timestamp.now(tz=self.local_timezone).date()
|
70 |
-
|
71 |
-
if timeframe == 'daily':
|
72 |
-
report = df[df['Task Time'].dt.date == today]
|
73 |
-
elif timeframe == 'weekly':
|
74 |
-
week_start = pd.Timestamp.now(tz=self.local_timezone) - pd.DateOffset(days=pd.Timestamp.now(tz=self.local_timezone).dayofweek)
|
75 |
-
report = df[(df['Task Time'] >= week_start) & (df['Task Time'] < pd.Timestamp.now(tz=self.local_timezone) + pd.DateOffset(days=1))]
|
76 |
-
elif timeframe == 'monthly':
|
77 |
-
report = df[df['Task Time'].dt.month == pd.Timestamp.now(tz=self.local_timezone).month]
|
78 |
-
elif timeframe == 'yearly':
|
79 |
-
report = df[df['Task Time'].dt.year == pd.Timestamp.now(tz=self.local_timezone).year]
|
80 |
-
else:
|
81 |
-
report = pd.DataFrame() # Empty DataFrame for unsupported timeframes
|
82 |
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
class TaskManager:
|
|
|
|
|
|
|
2 |
def __init__(self):
|
3 |
+
# Initialization code (e.g., setting up local time zones and loading tasks)
|
4 |
+
|
5 |
+
def generate_report(self, timeframe):
|
6 |
+
df = pd.DataFrame(st.session_state.tasks)
|
7 |
+
if df.empty:
|
8 |
+
return pd.DataFrame() # Return empty DataFrame if no tasks are present
|
9 |
+
|
10 |
+
# Ensure 'Task Time' is already timezone-aware and convert it to local time for the report
|
11 |
+
df['Task Time'] = pd.to_datetime(df['Task Time']).apply(lambda x: x.tz_convert(self.local_timezone))
|
12 |
+
|
13 |
+
today = pd.Timestamp.now(tz=self.local_timezone).date()
|
14 |
+
|
15 |
+
if timeframe == 'daily':
|
16 |
+
report = df[df['Task Time'].dt.date == today]
|
17 |
+
elif timeframe == 'weekly':
|
18 |
+
week_start = pd.Timestamp.now(tz=self.local_timezone) - pd.DateOffset(days=pd.Timestamp.now(tz=self.local_timezone).dayofweek)
|
19 |
+
report = df[(df['Task Time'] >= week_start) & (df['Task Time'] < pd.Timestamp.now(tz=self.local_timezone) + pd.DateOffset(days=1))]
|
20 |
+
elif timeframe == 'monthly':
|
21 |
+
report = df[df['Task Time'].dt.month == pd.Timestamp.now(tz=self.local_timezone).month]
|
22 |
+
elif timeframe == 'yearly':
|
23 |
+
report = df[df['Task Time'].dt.year == pd.Timestamp.now(tz=self.local_timezone).year]
|
24 |
else:
|
25 |
+
report = pd.DataFrame() # Empty DataFrame for unsupported timeframes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0
|
28 |
+
return report
|