forked from streamlit/streamlit-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
424 lines (381 loc) · 10.8 KB
/
gui.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
"""Utility functions to handle repeated GUI tasks for the dashboard."""
import base64
from io import StringIO
import itertools
import pandas as pd
import streamlit as st
from pandas._libs.tslibs.timestamps import Timestamp
from datetime import date, timedelta
ST_COLOR_PALETTE = {
"red": {
"100": "#7d353b",
"90": "#bd4043",
"80": "#ff2b2b",
"70": "#ff4b4b",
"60": "#ff6c6c",
"50": "#ff8c8c",
"40": "#ffabab",
"30": "#ffc7c7",
"20": "#ffdede",
"10": "#fff0f0",
},
"orange": {
"100": "#d95a00",
"90": "#ed6f13",
"80": "#ff8700",
"70": "#ffa421",
"60": "#ffbd45",
"50": "#ffd16a",
"40": "#ffe08e",
"30": "#ffecb0",
"20": "#fff6d0",
"10": "#fffae8",
},
"yellow": {
"100": "#dea816",
"90": "#edbb16",
"80": "#faca2b",
"70": "#ffe312",
"60": "#fff835",
"50": "#ffff59",
"40": "#ffff7d",
"30": "#ffffa0",
"20": "#ffffc2",
"10": "#ffffe1",
},
"green": {
"100": "#177233",
"90": "#158237",
"80": "#09ab3b",
"70": "#21c354",
"60": "#3dd56d",
"50": "#5ce488",
"40": "#7defa1",
"30": "#9ef6bb",
"20": "#c0fcd3",
"10": "#dffde9",
},
"blue-green": {
"100": "#246e69",
"90": "#2c867c",
"80": "#29b09d",
"70": "#00d4b1",
"60": "#20e7c5",
"50": "#45f4d5",
"40": "#6bfde3",
"30": "#93ffee",
"20": "#bafff7",
"10": "#dcfffb",
},
"light-blue": {
"100": "#15799e",
"90": "#0d8cb5",
"80": "#00a4d4",
"70": "#00c0f2",
"60": "#24d4ff",
"50": "#4be4ff",
"40": "#73efff",
"30": "#9af8ff",
"20": "#bffdff",
"10": "#e0feff",
},
"blue": {
"100": "#004280",
"90": "#0054a3",
"80": "#0068c9",
"70": "#1c83e1",
"60": "#3d9df3",
"50": "#60b4ff",
"40": "#83c9ff",
"30": "#a6dcff",
"20": "#c7ebff",
"10": "#e4f5ff",
},
"violet": {
"100": "#3f3163",
"90": "#583f84",
"80": "#6d3fc0",
"70": "#803df5",
"60": "#9a5dff",
"50": "#b27eff",
"40": "#c89dff",
"30": "#dbbbff",
"20": "#ebd6ff",
"10": "#f5ebff",
},
"gray": {
"100": "#0e1117",
"90": "#262730",
"80": "#555867",
"70": "#808495",
"60": "#a3a8b8",
"50": "#bfc5d3",
"40": "#d5dae5",
"30": "#e6eaf1",
"20": "#f0f2f6",
"10": "#fafafa",
},
}
def color(name):
"""Returns a color from the streamlit color palette, e.g. red-100, as hex."""
hue, intensity = name.rsplit("-", 1)
return ST_COLOR_PALETTE[hue][intensity]
ST_COLOR_PALETTE = {
"red": {
"100": "#7d353b",
"90": "#bd4043",
"80": "#ff2b2b",
"70": "#ff4b4b",
"60": "#ff6c6c",
"50": "#ff8c8c",
"40": "#ffabab",
"30": "#ffc7c7",
"20": "#ffdede",
"10": "#fff0f0",
},
"orange": {
"100": "#d95a00",
"90": "#ed6f13",
"80": "#ff8700",
"70": "#ffa421",
"60": "#ffbd45",
"50": "#ffd16a",
"40": "#ffe08e",
"30": "#ffecb0",
"20": "#fff6d0",
"10": "#fffae8",
},
"yellow": {
"100": "#dea816",
"90": "#edbb16",
"80": "#faca2b",
"70": "#ffe312",
"60": "#fff835",
"50": "#ffff59",
"40": "#ffff7d",
"30": "#ffffa0",
"20": "#ffffc2",
"10": "#ffffe1",
},
"green": {
"100": "#177233",
"90": "#158237",
"80": "#09ab3b",
"70": "#21c354",
"60": "#3dd56d",
"50": "#5ce488",
"40": "#7defa1",
"30": "#9ef6bb",
"20": "#c0fcd3",
"10": "#dffde9",
},
"blue-green": {
"100": "#246e69",
"90": "#2c867c",
"80": "#29b09d",
"70": "#00d4b1",
"60": "#20e7c5",
"50": "#45f4d5",
"40": "#6bfde3",
"30": "#93ffee",
"20": "#bafff7",
"10": "#dcfffb",
},
"light-blue": {
"100": "#15799e",
"90": "#0d8cb5",
"80": "#00a4d4",
"70": "#00c0f2",
"60": "#24d4ff",
"50": "#4be4ff",
"40": "#73efff",
"30": "#9af8ff",
"20": "#bffdff",
"10": "#e0feff",
},
"blue": {
"100": "#004280",
"90": "#0054a3",
"80": "#0068c9",
"70": "#1c83e1",
"60": "#3d9df3",
"50": "#60b4ff",
"40": "#83c9ff",
"30": "#a6dcff",
"20": "#c7ebff",
"10": "#e4f5ff",
},
"violet": {
"100": "#3f3163",
"90": "#583f84",
"80": "#6d3fc0",
"70": "#803df5",
"60": "#9a5dff",
"50": "#b27eff",
"40": "#c89dff",
"30": "#dbbbff",
"20": "#ebd6ff",
"10": "#f5ebff",
},
"gray": {
"100": "#0e1117",
"90": "#262730",
"80": "#555867",
"70": "#808495",
"60": "#a3a8b8",
"50": "#bfc5d3",
"40": "#d5dae5",
"30": "#e6eaf1",
"20": "#f0f2f6",
"10": "#fafafa",
},
}
def color(name):
"""Returns a color from the streamlit color palette, e.g. red-100, as hex."""
hue, intensity = name.rsplit("-", 1)
return ST_COLOR_PALETTE[hue][intensity]
def space(num_lines=1):
for _ in range(num_lines):
st.write("")
HEADER_COLOR_CYCLE = itertools.cycle(
[
"light-blue-70",
"orange-70",
"blue-green-70",
"blue-70",
"violet-70",
"red-70",
"green-70",
"yellow-80",
]
)
def colored_header(label, description=None, color_name=None):
"""Shows a header with a colored underline and an optional description."""
if color_name is None:
color_name = next(HEADER_COLOR_CYCLE)
st.subheader(label)
st.write(
f'<hr style="background-color: {color(color_name)}; margin-top: 0; margin-bottom: 0; height: 3px; border: none; border-radius: 3px;">',
unsafe_allow_html=True,
)
if description:
st.caption(description)
def icon(emoji: str):
"""Shows an emoji as a Notion-style page icon."""
st.write(
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
unsafe_allow_html=True,
)
def convert_timestamps_to_string(df: pd.DataFrame) -> pd.DataFrame:
for col in df:
if df[col].dtype.type == Timestamp:
df[col] = df[col].dt.strftime("%Y-%m-%d %H:%M:%S")
return df
def pretty_print_df(name: str, df: pd.DataFrame, allow_html: bool = False):
"""
Prints out a dataframe, handling NANs properly and including useful metadata such as
column types and number of rows.
This function solves some dataframe display bugs I discovered:
- https://www.notion.so/660f4cfb3fa941f6ae79bff2ea92789c
Parameters
----
name: str
Pretty print a name for this dataframe
df: pd.DataFrame
The dataframe to proint
"""
DEFAULT_N_ROWS = 500
st.subheader(name)
# Style NANs and timestamps specially.
def _cell_styler(x):
if "NaTType" in str(type(x)) or "<NA>" == str(x):
return "n/a"
# elif "timestamp" in str(type(x)):
# return x.strftime("%b %d, %Y")
else:
return x
# Turn nonpositive numbers red.
def _cell_colorer(x):
try:
if x == True:
return "color: green"
if x <= 0:
return "color: red"
except TypeError:
pass
return ""
# This is where we'll display the data frame
dataframe_location = st.empty()
st.caption(f"`{len(df)}` elements in {name}") # type: ignore
# Display options
with st.expander(f"Show options for {name}"):
# Let the user select the number of rows.
n_rows = st.slider(f"Rows for {name}", 0, len(df), min(DEFAULT_N_ROWS, len(df)))
# Provide a link to download a CSV file.
if st.checkbox(f"Show download link for {name}"):
csv = df[:n_rows].to_csv(index=True)
b64 = base64.b64encode(csv.encode()).decode()
filename = name.replace(" ", "_").lower() + ".csv"
href = (
f'<a download="{filename}"'
f' href="data:file/csv;base64,{b64}">`{filename}`'
f"</a>"
)
st.markdown(
f":bulb: Download `{n_rows}` rows: {href}", unsafe_allow_html=True
)
# Show more details about the dataframe
if st.checkbox(f"Show info for {name}"):
str_buffer = StringIO()
df.info(buf=str_buffer)
st.code(str_buffer.getvalue())
# Apply these stylings.
try:
df = df[:n_rows]
df.style.format(_cell_styler).applymap(_cell_colorer)
if allow_html:
dataframe_location.write(df.to_html(escape=False), unsafe_allow_html=True)
else:
dataframe_location.dataframe(df)
except ValueError:
styled_df = (
df.reset_index()[:n_rows].style.format(_cell_styler).applymap(_cell_colorer)
)
if allow_html:
dataframe_location.write(
styled_df.to_html(escape=False), unsafe_allow_html=True
)
else:
dataframe_location.dataframe(styled_df)
def space(num_lines=1):
"""Adds empty lines to the Streamlit app."""
for _ in range(num_lines):
st.write("")
def week_paginator(key="week_paginator"):
"""Shows a widget to paginate weeks, returns start & end date of selected week."""
# Store page number in session state.
if key not in st.session_state:
st.session_state[key] = 0
def next_page():
st.session_state[key] += 1
def prev_page():
st.session_state[key] -= 1
# Show buttons to increase or decrease the selected week/page.
col1, col2, col3, _ = st.columns([0.09, 0.22, 0.09, 0.6])
if st.session_state[key] < 0:
col3.button(">", on_click=next_page)
else:
col3.write("") # this makes the empty column show up on mobile
if st.session_state[key] > -52:
col1.button("<", on_click=prev_page)
else:
col1.write("") # this makes the empty column show up on mobile
# Calculate start and end date of the selected week based on page number.
end_date = date.today() + timedelta(
7 * st.session_state[key]
) # ...page is negative!
start_date = end_date - timedelta(6)
# Show selected week between the buttons.
col2.write(start_date.strftime("%d %b") + " to " + end_date.strftime("%d %b"))
# print(start_date, end_date)
return start_date, end_date