shamimjony1000 commited on
Commit
6fb7683
1 Parent(s): 4cd768f

Update task_operations.py

Browse files
Files changed (1) hide show
  1. task_operations.py +49 -40
task_operations.py CHANGED
@@ -1,63 +1,72 @@
1
- import streamlit as st
 
2
  import pandas as pd
3
- from datetime import datetime, time, timedelta
4
- import pytz
 
5
 
6
  class TaskManager:
 
 
 
7
  def __init__(self):
8
- self.utc_timezone = pytz.utc
9
- self.local_timezone = pytz.timezone('Asia/Dhaka') # GMT+6 for your timezone
10
  if 'tasks' not in st.session_state:
11
  st.session_state.tasks = self.load_tasks()
12
 
13
  def load_tasks(self):
14
- # Assuming tasks are stored as a list of dictionaries
15
- tasks = st.session_state.get('tasks', [])
16
- if not tasks:
17
- return pd.DataFrame()
18
-
19
- df = pd.DataFrame(tasks)
20
- df['Task Time'] = pd.to_datetime(df['Task Time'])
 
 
21
 
22
- # Convert timezone if the timestamps are already timezone-aware
23
- df['Task Time'] = df['Task Time'].dt.tz_convert(self.local_timezone)
24
-
25
- return df
26
 
27
  def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
28
- task_time_full = datetime.combine(datetime.today(), task_time).replace(tzinfo=self.local_timezone)
29
- task_duration = timedelta(hours=task_duration_hours, minutes=task_duration_minutes)
30
- task = {
31
- 'Task Name': task_name,
32
- 'Task Time': task_time_full,
33
- 'Task Duration (hours)': task_duration_hours,
34
- 'Task Duration (minutes)': task_duration_minutes,
35
- 'Task Category': task_category,
36
  }
37
- st.session_state.tasks.append(task)
38
  self.save_tasks(st.session_state.tasks)
39
 
40
- def save_tasks(self, tasks):
41
- st.session_state.tasks = tasks
 
 
 
 
 
42
 
43
  def generate_report(self, timeframe):
44
  df = pd.DataFrame(st.session_state.tasks)
45
  if df.empty:
46
- return pd.DataFrame()
47
-
48
- df['Task Time'] = pd.to_datetime(df['Task Time']).dt.tz_convert(self.local_timezone)
49
- df['Total Duration'] = df['Task Duration (hours)'] + df['Task Duration (minutes)'] / 60.0
50
 
51
- # Filter based on the timeframe
52
- today = datetime.now(self.local_timezone).date()
 
53
  if timeframe == 'daily':
54
- df = df[df['Task Time'].dt.date == today]
55
  elif timeframe == 'weekly':
56
- start_of_week = today - timedelta(days=today.weekday())
57
- df = df[(df['Task Time'].dt.date >= start_of_week) & (df['Task Time'].dt.date <= today)]
58
  elif timeframe == 'monthly':
59
- df = df[df['Task Time'].dt.month == today.month]
60
  elif timeframe == 'yearly':
61
- df = df[df['Task Time'].dt.year == today.year]
62
-
63
- return df
 
 
 
 
1
+ # components/task_operations.py
2
+
3
  import pandas as pd
4
+ import os
5
+ from datetime import datetime
6
+ import streamlit as st
7
 
8
  class TaskManager:
9
+ TASKS_FILE = "tasks.csv"
10
+ CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']
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
+ return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
20
+ else:
21
+ return []
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
+ task_time_full = datetime.combine(datetime.today(), task_time)
33
+ task_entry = {
34
+ "Task Name": task_name,
35
+ "Task Time": task_time_full,
36
+ "Task Duration (hours)": int(task_duration_hours),
37
+ "Task Duration (minutes)": int(task_duration_minutes),
38
+ "Category": task_category
 
39
  }
40
+ st.session_state.tasks.append(task_entry)
41
  self.save_tasks(st.session_state.tasks)
42
 
43
+ def delete_task_by_name(self, task_name):
44
+ for index, task in enumerate(st.session_state.tasks):
45
+ if task['Task Name'] == task_name:
46
+ st.session_state.tasks.pop(index)
47
+ self.save_tasks(st.session_state.tasks)
48
+ return True
49
+ return False
50
 
51
  def generate_report(self, timeframe):
52
  df = pd.DataFrame(st.session_state.tasks)
53
  if df.empty:
54
+ return pd.DataFrame() # Return empty DataFrame if no tasks are present
 
 
 
55
 
56
+ # Ensure the 'Task Time' is in datetime format
57
+ df['Task Time'] = pd.to_datetime(df['Task Time'])
58
+
59
  if timeframe == 'daily':
60
+ report = df[df['Task Time'].dt.date == pd.Timestamp.today().date()]
61
  elif timeframe == 'weekly':
62
+ week_start = pd.Timestamp.today() - pd.DateOffset(days=pd.Timestamp.today().dayofweek)
63
+ report = df[(df['Task Time'] >= week_start) & (df['Task Time'] < pd.Timestamp.today() + pd.DateOffset(days=1))]
64
  elif timeframe == 'monthly':
65
+ report = df[df['Task Time'].dt.month == pd.Timestamp.today().month]
66
  elif timeframe == 'yearly':
67
+ report = df[df['Task Time'].dt.year == pd.Timestamp.today().year]
68
+ else:
69
+ report = pd.DataFrame() # Empty DataFrame for unsupported timeframes
70
+
71
+ report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0
72
+ return report