-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
158 lines (116 loc) · 4.97 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import sys
import os
# Add the path to the core module
# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../core')))
from datetime import date
import streamlit as st
from core.database import get_db_connection,save_daily_task_completion,get_daily_task_completion, update_task_history, get_task_history
from features.auth import register_user, get_user_id
from features.tasks import add_task, get_tasks, mark_task_completed, delete_task
import plotly.graph_objs as go
import pandas as pd
# User Registration/Login
st.sidebar.title("User Login/Register")
username = st.sidebar.text_input("Username")
if st.sidebar.button("Register"):
register_user(username)
if st.sidebar.button("Login"):
st.session_state['user_id'] = get_user_id(username)
# Task Management (go after of the graphs)
if 'user_id' in st.session_state:
user_id = st.session_state['user_id']
st.title(f"Welcome, {username}!")
# check user id
# st.write(f"See user id in session state {user_id} and direct get_user_id function {get_user_id(username)}")
#Create the graph Cumulative from task_history
history_data = get_task_history(user_id)
# Assuming daily_data is a list of tuples (task_id, completion_date, completed)
df = pd.DataFrame(history_data, columns=['date', 'total_tasks', 'completed_tasks', 'completion_rate'] )
df['date'] = pd.to_datetime(df['date'])
# Create the graph Cumulative Completions
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df['date'],
y=df['completion_rate'],
fill='tozeroy', # Fill to the y-axis
mode='lines',
line=dict(color='purple')
))
st.title("Progress")
# Create the graph Cumulative Completions
# Check if the DataFrame is not empty before calculating max
if not df.empty and not df['completion_rate'].empty:
yaxis_range = [0, max(df['completion_rate']) + 0.1] # Adjust based on your data
else:
yaxis_range = [0, 1] # Default range if no data is available
fig.update_layout(
xaxis_title="Date",
yaxis_range=yaxis_range # Use the calculated or default range
)
# Display the graph in Streamlit
st.plotly_chart(fig)
#######################
# Calculate Completion Percentage
if not df.empty and not df['completion_rate'].empty:
completion_percentage = df['completion_rate'].iloc[-1] * 100
else:
completion_percentage = 0 # Set to zero if no data is available
# Display Progress - Completion Percentage
st.title("Completion Percentage %")
fig = go.Figure(go.Indicator(
mode="gauge+number",
value=completion_percentage,
title={'text': "Completion Percentage"},
gauge={
'axis': {'range': [0, 100 ]},
'bar': {'color': "blue"},
}
))
st.plotly_chart(fig)
# Add Task
if 'user_id' not in st.session_state:
st.warning("You must be logged in to add a task.")
else:
user_id = st.session_state['user_id']
task_name = st.text_input("Enter task name:")
if st.button("Add Task"):
if task_name.strip():
add_task(user_id, task_name)
else:
st.warning("Task name cannot be blank.")
# Display Tasks
tasks = get_tasks(user_id)
for task in tasks:
# Get last completion date
task_last_completed_date = task.get('date')
# Display the values using Streamlit
today = str(date.today())
# st.write(f"Task Last Completed Date: {task_last_completed_date}")
# st.write(f"Today's Date: {today}")
# Reset task if it's a new day
if task_last_completed_date != str(date.today()):
mark_task_completed(task['id'], False) # Reset to unchecked
task['completed'] = False # Update in the current session too
# Display the task checkbox
if st.checkbox(task['task_name'], key=task['id'], value=task['completed']):
mark_task_completed(task['id'], True) # update date
save_daily_task_completion(user_id, task['id'], True)
update_task_history()
else:
mark_task_completed(task['id'], False)
save_daily_task_completion(user_id, task['id'], False)
update_task_history()
# if st.checkbox(task['task_name'], key=task['id'], value=task['completed']):
# mark_task_completed(task['id'], True) # Save checked state
# else:
# mark_task_completed(task['id'], False) # Save unchecked state
# Delete a Task Provided by the User
st.title("Delete Task")
task_to_delete = st.selectbox("Select a task to delete", [task['task_name'] for task in tasks if task['task_name'].strip()])
if st.button("Delete Task"):
for task in tasks:
if task['task_name'] == task_to_delete:
delete_task(task['id'])
st.success(f"Task '{task_to_delete}' deleted!")
break
#