-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·244 lines (192 loc) · 5.74 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import streamlit as st
import datetime as dt
import plotly.express as px
from modules import data
######
# META
st.set_page_config(
page_title="Habit Tracker",
page_icon="💎",
layout='wide',
initial_sidebar_state="expanded"
)
########
# HEADER
st.title("💎 Habit Tracker")
dt_now = dt.datetime.now()
dt_str = dt_now.strftime("%Y-%m-%d")
dt_weekday = dt_now.strftime("%A")
dt_day = dt_now.strftime("%-d")
dt_month = dt_now.strftime("%b")
st.markdown(f" Today is {dt_weekday} - {dt_day}. of {dt_month}.")
#########
# SIDEBAR
st.sidebar.title("Mission Control")
# LOAD/CREATE FILE
sidebar_data_container = st.sidebar.beta_expander(
"💫 Load or Create a file", expanded=True
)
with sidebar_data_container:
st.markdown("### 🗂 Load data")
sidebar_uploaded_file = st.file_uploader(
"Choose your .csv file"
)
st.markdown("### ✨ or create a new file")
sidebar_create_file_name = st.text_input(
"Choose a file name", value="habit_data.csv"
)
sidebar_create_file_button = st.button("* Create new file")
# INPUT
sidebar_input_container = st.sidebar.beta_expander(
"💪🏼 How did you do today?", expanded=False
)
with sidebar_input_container:
# Choose Date
sidebar_date = st.date_input(
"📅 Which day you want to make an entry for?",
max_value=dt_now
)
# Slider Metrics
sidebar_sleep = st.slider(
"😴 How much did you sleep?",
min_value=0.0, max_value=12.0, value=8.0, step=0.1
)
sidebar_mood = st.slider(
"🌈 What mood were you in?",
min_value=1, max_value=7, value=5, step=1
)
sidebar_energy = st.slider(
"⚡️ How energized did you feel?",
min_value=1, max_value=7, value=5, step=1
)
# Radio Button Metrics
sidebar_food = st.radio(
"🥕 Did you eat healthy?", (0, 1)
)
sidebar_exercise = st.radio(
"🏃♀️ Did you exercise?", (0, 1)
)
sidebar_meditation = st.radio(
"🧘 Did you meditate?", (0, 1)
)
sidebar_reading = st.radio(
"📖 Did you read?", (0, 1)
)
sidebar_journaling = st.radio(
"✏️ Did you journal?", (0, 1)
)
sidebar_learning = st.radio(
"🎓 Did you learn something new?", (0, 1)
)
sidebar_work = st.radio(
"🏆 Did you work towards your goals?", (0, 1)
)
# "add data" button
add_row = st.button("➕ Add values")
######
# DATA
@st.cache(allow_output_mutation=True)
def get_file(input_file):
HabitData = data.HabitData()
HabitData.load(file=input_file)
return HabitData
@st.cache(allow_output_mutation=True)
def create_file(filename):
HabitData = data.HabitData()
HabitData.create(filename=filename)
return HabitData
# LOAD/CREATE DATA
if sidebar_uploaded_file is not None:
st.markdown("🔄 Data loaded.")
HabitData = get_file(sidebar_uploaded_file)
elif sidebar_create_file_button:
st.markdown("✨ File created.")
HabitData = create_file(sidebar_create_file_name)
else:
st.markdown("### Create or load a file to continue.")
st.stop()
# ADD DATA
if add_row:
append_dict = {
"date": sidebar_date,
"sleep": sidebar_sleep,
"mood": sidebar_mood,
"energy": sidebar_energy,
"food": sidebar_food,
"exercise": sidebar_exercise,
"meditation": sidebar_meditation,
"reading": sidebar_reading,
"journaling": sidebar_journaling,
"learning": sidebar_learning,
"work": sidebar_work,
}
HabitData.add(append_dict=append_dict)
# REMOVE DATA
# add function to sidebar
sidebar_remove_data_container = st.sidebar.beta_expander(
"🗑 Delete data", expanded=False
)
with sidebar_remove_data_container:
try:
opt = HabitData.data.date.unique()
except AttributeError:
opt = [None]
selectbox_remove_date = st.selectbox(
"Remove data:", options=opt
)
drop_row = st.button("➖ Remove values")
if drop_row:
HabitData.drop(date_index=selectbox_remove_date)
# DOWNLOAD DATA
sidebar_download_data = st.sidebar.button("⬇️ Download data")
if sidebar_download_data:
st.sidebar.markdown(HabitData.download(), unsafe_allow_html=True)
# ######
# # KPIs
# mean_container = st.beta_container() # , current_container = st.beta_columns([1, 1])
# with mean_container:
# mean_dict = HabitData.data.mean().to_dict()
# for k, v in mean_dict.items():
# st.markdown(f"""### {k}:""")
# st.markdown(f"""# {v}""")
# st.dataframe(pd.DataFrame(HabitData.data.mean().to_dict(), index=[0]))
HabitData.data["avg_performance"] = HabitData.data.set_index("date").sum(axis=1).div(28).values
#########
# VISUALS
# dataframe
data_container = st.beta_expander("Display your data", expanded=True)
with data_container:
dataframe = st.dataframe(HabitData.data)
# line plot
st.markdown("### Plot your habits over time")
plot_container, info_container = st.beta_columns([8, 2])
with info_container:
opt = HabitData.data.columns.to_list()
opt.remove("date")
selectbox_columns = st.selectbox(
"Select which column to plot:",
options=opt
)
m = round(HabitData.data[selectbox_columns].mean(), 2)
metric_dict = {
"mood": "lvl",
"energy": "lvl",
"sleep": "h",
"food": "%",
"exercise": "%",
"meditation": "%",
"reading": "%",
"journaling": "%",
"learning": "%",
"work": "%",
}
# {metric_dict.get(selectbox_columns)}
st.markdown(f"""# avg // {m}""")
with plot_container:
px_chart = px.bar(
HabitData.data,
x="date",
y=selectbox_columns,
# marginal="box", hover_data=HabitData.data.columns
)
st.plotly_chart(px_chart, use_container_width=True)