-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
117 lines (94 loc) · 2.1 KB
/
app.py
File metadata and controls
117 lines (94 loc) · 2.1 KB
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
import random
import pandas
import plotnine
import streamlit
BACKGROUND_FILL = 'black'
COLOR_PALETTE = 'YlGn'
WEEKDAYS = [
'Sat',
'Fri',
'Thu',
'Wed',
'Tue',
'Mon',
'Sun'
]
WEEKDAYS_DISPLAYED = [
'Mon',
'Wed',
'Fri',
]
def days_to_counts(days):
"""Convert iterable of integer day numbers to counts of weekday and week.
argument days: iterable of int
returns: pandas.DataFrame with columns:
week: int
weekday: pandas.Categorical
count: int
"""
counts = pandas.DataFrame({
'week': [d // 7 for d in days],
'weekday': pandas.Categorical(
[WEEKDAYS[d % 7] for d in days],
categories=WEEKDAYS,
ordered=True
)
})
counts = counts[['week', 'weekday']].value_counts().reset_index(name='count')
return counts
streamlit.title('example calendar display')
seed = streamlit.sidebar.number_input(
'random seed',
min_value=0
)
random.seed(seed)
n = streamlit.sidebar.number_input(
'number of hits',
min_value=2,
value=300
)
tile_size = streamlit.sidebar.slider(
'size of tiles',
min_value=0.1,
max_value=1.0,
value=0.9
)
data = days_to_counts(random.choices(range(365), k=n))
streamlit.markdown('''
uses plotnine to produce a very basic imitation of GitHub's activity calendar
''')
tile_style = plotnine.aes(
width=tile_size,
height=tile_size
)
fig = (
plotnine.ggplot(data)
+ plotnine.aes(
x='week',
y='weekday',
fill='factor(count)'
)
+ plotnine.theme_void()
+ plotnine.theme(
axis_text_y=plotnine.element_text()
)
+ plotnine.coord_fixed()
+ plotnine.scale_y_discrete(
breaks=WEEKDAYS_DISPLAYED
)
+ plotnine.scale_fill_brewer(
palette=COLOR_PALETTE,
direction=-1
)
+ plotnine.geom_tile(
mapping=tile_style,
data=days_to_counts(range(365)),
fill=BACKGROUND_FILL,
show_legend=False
)
+ plotnine.geom_tile(
mapping=tile_style,
show_legend=False
)
)
streamlit.pyplot(fig.draw())