forked from yandiandriamasy/time_management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_management_app.py
157 lines (130 loc) · 4.89 KB
/
time_management_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
import streamlit as st
import datetime
from src.connect_to_notion import write_new_row, get_table_content
import pytz
from src.utils import create_timeline_plot, adapt_data_for_plotting
from src.constants import REFERENCE_RUEIL_RER_A
from src.connect_to_prim import get_next_departures, format_prim_response
import asyncio
tz = pytz.timezone("Europe/Berlin")
# Different credentials
NOTION_TOKEN = st.secrets["NOTION_TOKEN"]
TABLE_ID = st.secrets["TABLE_ID"]
PRIM_TOKEN = st.secrets["PRIM_TOKEN"]
def get_current_hour() -> str:
current_time = datetime.datetime.now(tz)
return f"{current_time.hour}:{current_time.minute}"
def start_activity():
st.session_state.activity_in_progress = True
st.session_state.activity_name = st.session_state.widget
st.session_state.activity_start = datetime.datetime.now(tz)
with open(
f"{st.session_state.activity_start.date()}.txt", "a", encoding="utf-8"
) as f:
f.write(
f'{st.session_state.activity_start.strftime("%H:%M:%S")}, "Start", {st.session_state.activity_name} \n'
)
write_new_row(st.session_state.activity_name, NOTION_TOKEN)
def show_current_activity():
if st.session_state.activity_name != "":
st.write(
f"🚀 {st.session_state.activity_name} started at "
f"{st.session_state.activity_start.strftime('%H:%M:%S')} ! "
)
time_elapsed = datetime.datetime.now(tz) - st.session_state.activity_start
st.write(f"You have been working on this task for: {time_elapsed}")
def end_activity():
with open(
f"{st.session_state.activity_start.date()}.txt", "a", encoding="utf-8"
) as f:
f.write(
f'{datetime.datetime.now(tz).strftime("%H:%M:%S")}, "End", {st.session_state.activity_name} \n'
)
st.session_state.activity_in_progress = False
st.write(
f"✨ {st.session_state.activity_name} ended at {datetime.datetime.now(tz).strftime('%d/%m/%Y, %H:%M:%S')} ! "
)
st.session_state.activity_name = ""
st.balloons()
# Streamlit app
def main():
st.sidebar.title("🗄 Menu")
pages = ["👻Ghost mode", "👀Visualize your timeline", "🚆Go home"]
page = st.sidebar.radio("Go to", pages)
if page == "👻Ghost mode":
page1()
elif page == "👀Visualize your timeline":
page2()
elif page == "🚆Go home":
page3()
def page1():
if "activity_in_progress" not in st.session_state:
st.session_state.activity_in_progress = False
if "activity_name" not in st.session_state:
st.session_state.activity_name = ""
st.title("Ghost mode 👻")
st.markdown(
"""
<style>
.time {
font-size: 20px;
font-weight: 300;
color: #6f737d;
}
</style>
""",
unsafe_allow_html=True,
)
async def watch(test, warn_message):
while True:
if st.session_state.activity_name != "":
total_seconds = (
datetime.datetime.now(tz) - st.session_state.activity_start
).total_seconds()
if total_seconds > 60 * 15:
warn_message.markdown(
"""
😅 More than 15 minutes on current task !
💡 Do you need help ?
"""
)
# Calculate hours, minutes, and seconds
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
# Format the result as a string
formatted_time = f"{int(hours)}H:{int(minutes)}M:{int(seconds)}S"
test.markdown(
f"""
🚀 Current task : {st.session_state.activity_name}
<p class="time">
Time spent : {formatted_time}
</p>
""",
unsafe_allow_html=True,
)
else:
test.markdown("🚀 Ready to start the day ?")
await asyncio.sleep(1)
test = st.empty()
warn_message = st.empty()
st.text_input("Activity name 📝:", key="widget", on_change=start_activity)
asyncio.run(watch(test, warn_message))
def page2():
st.title("👀 Visualize your timeline")
filter_date = st.date_input("😎 What day do you want to visualize ? ")
df = get_table_content(TABLE_ID, NOTION_TOKEN, filter_date)
df = adapt_data_for_plotting(df)
fig = create_timeline_plot(df)
# Show plot in Streamlit
st.plotly_chart(fig)
st.dataframe(df)
def page3():
st.title("Go home 🚆")
st.header("You can go home now, see you tomorrow!")
st.header("Next departures from Rueil-Malmaison RER A station:")
st.dataframe(
format_prim_response(get_next_departures(REFERENCE_RUEIL_RER_A, PRIM_TOKEN))
)
st.balloons()
if __name__ == "__main__":
main()