shamimjony1000 commited on
Commit
91f4e3b
·
verified ·
1 Parent(s): 6768f40

Update task_operations.py

Browse files
Files changed (1) hide show
  1. task_operations.py +10 -23
task_operations.py CHANGED
@@ -1,39 +1,36 @@
1
  import streamlit as st
2
  import pandas as pd
3
  from datetime import datetime
 
4
  import os
5
 
6
  class TaskManager:
7
  TASKS_FILE = "tasks.csv"
8
  CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']
 
9
 
10
  def __init__(self):
11
- self.local_timezone = 'Asia/Dhaka' # GMT+6
12
  if 'tasks' not in st.session_state:
13
  st.session_state.tasks = self.load_tasks()
14
 
15
  def load_tasks(self):
16
  if os.path.exists(self.TASKS_FILE):
17
  if os.path.getsize(self.TASKS_FILE) > 0:
18
- tasks = pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
19
- # Ensure timezone-awareness when loading tasks
20
- for task in tasks:
21
- task['Task Time'] = pd.Timestamp(task['Task Time']).tz_localize('UTC').tz_convert(self.local_timezone)
22
- return tasks
23
- else:
24
- return []
25
  else:
26
  df = pd.DataFrame(columns=["Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
27
  df.to_csv(self.TASKS_FILE, index=False)
28
- return []
29
 
30
  def save_tasks(self, tasks):
31
  df = pd.DataFrame(tasks)
32
- df['Task Time'] = df['Task Time'].apply(lambda x: x.tz_convert('UTC'))
33
  df.to_csv(self.TASKS_FILE, index=False)
34
 
35
  def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
36
- task_time_full = datetime.combine(datetime.today(), task_time).astimezone(self.local_timezone)
 
37
  task_entry = {
38
  "Task Name": task_name,
39
  "Task Time": task_time_full,
@@ -54,25 +51,15 @@ class TaskManager:
54
 
55
  def generate_report(self, timeframe):
56
  df = pd.DataFrame(st.session_state.tasks)
57
- if df.empty:
58
- return pd.DataFrame() # Return empty DataFrame if no tasks are present
59
-
60
- # Ensure 'Task Time' is already timezone-aware and convert it to local time for the report
61
- df['Task Time'] = pd.to_datetime(df['Task Time']).apply(lambda x: x.tz_convert(self.local_timezone))
62
-
63
- today = pd.Timestamp.now(tz=self.local_timezone).date()
64
-
65
  if timeframe == 'daily':
66
- report = df[df['Task Time'].dt.date == today]
67
  elif timeframe == 'weekly':
68
  week_start = pd.Timestamp.now(tz=self.local_timezone) - pd.DateOffset(days=pd.Timestamp.now(tz=self.local_timezone).dayofweek)
69
- report = df[(df['Task Time'] >= week_start) & (df['Task Time'] < pd.Timestamp.now(tz=self.local_timezone) + pd.DateOffset(days=1))]
70
  elif timeframe == 'monthly':
71
  report = df[df['Task Time'].dt.month == pd.Timestamp.now(tz=self.local_timezone).month]
72
  elif timeframe == 'yearly':
73
  report = df[df['Task Time'].dt.year == pd.Timestamp.now(tz=self.local_timezone).year]
74
- else:
75
- report = pd.DataFrame() # Empty DataFrame for unsupported timeframes
76
 
77
  report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0
78
  return report
 
1
  import streamlit as st
2
  import pandas as pd
3
  from datetime import datetime
4
+ import pytz
5
  import os
6
 
7
  class TaskManager:
8
  TASKS_FILE = "tasks.csv"
9
  CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']
10
+ local_timezone = "Asia/Dhaka" # GMT+6
11
 
12
  def __init__(self):
 
13
  if 'tasks' not in st.session_state:
14
  st.session_state.tasks = self.load_tasks()
15
 
16
  def load_tasks(self):
17
  if os.path.exists(self.TASKS_FILE):
18
  if os.path.getsize(self.TASKS_FILE) > 0:
19
+ df = pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time'])
20
+ df['Task Time'] = pd.to_datetime(df['Task Time']).dt.tz_localize('UTC').dt.tz_convert(self.local_timezone)
21
+ return df.to_dict(orient='records')
 
 
 
 
22
  else:
23
  df = pd.DataFrame(columns=["Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
24
  df.to_csv(self.TASKS_FILE, index=False)
25
+ return []
26
 
27
  def save_tasks(self, tasks):
28
  df = pd.DataFrame(tasks)
 
29
  df.to_csv(self.TASKS_FILE, index=False)
30
 
31
  def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
32
+ local_tz = pytz.timezone(self.local_timezone)
33
+ task_time_full = local_tz.localize(datetime.combine(datetime.today(), task_time))
34
  task_entry = {
35
  "Task Name": task_name,
36
  "Task Time": task_time_full,
 
51
 
52
  def generate_report(self, timeframe):
53
  df = pd.DataFrame(st.session_state.tasks)
 
 
 
 
 
 
 
 
54
  if timeframe == 'daily':
55
+ report = df[df['Task Time'].dt.date == pd.Timestamp.now(tz=self.local_timezone).date()]
56
  elif timeframe == 'weekly':
57
  week_start = pd.Timestamp.now(tz=self.local_timezone) - pd.DateOffset(days=pd.Timestamp.now(tz=self.local_timezone).dayofweek)
58
+ report = df[(df['Task Time'] >= week_start) & (df['Task Time'] <= pd.Timestamp.now(tz=self.local_timezone))]
59
  elif timeframe == 'monthly':
60
  report = df[df['Task Time'].dt.month == pd.Timestamp.now(tz=self.local_timezone).month]
61
  elif timeframe == 'yearly':
62
  report = df[df['Task Time'].dt.year == pd.Timestamp.now(tz=self.local_timezone).year]
 
 
63
 
64
  report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0
65
  return report