-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcard.py
145 lines (120 loc) · 3.5 KB
/
card.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
import arrow
import dash_core_components as dcc
import dash_html_components as html
DATE_PICKER_STYLE = {
"text-align": "center",
"width": "100%",
"margin-bottom": "15px",
}
def date_picker_with_chart_card(date_picker_id, chart_cards, date=arrow.now()):
date_picker_html = html.Div(
[
html.Span(
"Date:", style={"font-size": "1.5em", "margin-right": "5px"}
),
dcc.DatePickerSingle(
id=date_picker_id,
date=date.datetime,
max_date_allowed=arrow.now().datetime,
),
],
style=DATE_PICKER_STYLE,
)
items = [date_picker_html] + chart_cards
return row_card(items)
def date_range_with_chart_card(date_range_id, chart_cards, start_date=arrow.now().shift(days=-30), end_date=arrow.now()):
date_range_html = html.Div(
[
html.Span(
"DateRange:", style={"font-size": "1.5em", "margin-right": "5px"}
),
dcc.DatePickerRange(
id=date_range_id,
first_day_of_week=1, # Monday
start_date=start_date.datetime,
end_date=end_date.datetime,
max_date_allowed=end_date.datetime,
),
],
style=DATE_PICKER_STYLE,
)
items = [date_range_html] + chart_cards
return row_card(items)
def chart_card(graph_id, title_text=None, description_text=None, size=6, control_item=None):
items = [
dcc.Graph(id=graph_id),
]
if control_item is not None:
items.insert(0, control_item)
return make_card_html(
items,
title_text=title_text,
description_text=description_text,
size=size,
)
def simple_value_card(
card_id,
value_id,
title_text,
description_text=None,
size=2
):
return make_card_html(
[
html.P(id=value_id),
],
card_id=card_id,
title_text=title_text,
description_text=description_text,
size=size,
)
def row_card(item, title_text=None, description_text=None):
return make_card_html(
item,
title_text=title_text,
description_text=description_text,
card_body_style={
"display": "flex",
"flex-wrap": "wrap",
}
)
def make_card_html(
body,
card_id=None,
title_text=None,
description_text=None,
size=12,
background_color="white",
class_name="",
card_body_style={},
):
card_html = html.Div(
[html.Div([], className="card mb-3 text-center")],
className=f"col-sm-{size}",
)
card_body_style["background-color"] = background_color
if card_id is None:
div = html.Div(
body,
className="card-body " + class_name,
style=card_body_style,
)
else:
div = html.Div(
body,
id=card_id,
className="card-body " + class_name,
style=card_body_style,
)
card_html.children[0].children.append(div)
if description_text is not None:
description = html.P(description_text, className="col-sm-12")
card_html.children[0].children[0].children.insert(0, description)
if title_text is not None:
if size == 12:
H_html = html.H4
else:
H_html = html.H5
title = H_html(title_text, className="card-title col-sm-12")
card_html.children[0].children[0].children.insert(0, title)
return card_html