-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp.py
197 lines (175 loc) · 5.85 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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os
import dash
import dash_daq as daq
import dash_core_components as dcc
import dash_html_components as html
import plotly
from dash.dependencies import Input, Output, State
# import created class to access bigtable iot data
import access_iot_data
# initialize class and create row keys list once
cbt_data_generator = access_iot_data.iot_pipeline_data()
devices_list = cbt_data_generator.get_device_names()
row_keys_list = cbt_data_generator.create_device_rowkeys(devices_list)
# setup empty dictionary lists to be used by live graph
# define max number of data points per graph
device_1 = cbt_data_generator.set_graph_data_limit(n_items=20)
device_2 = cbt_data_generator.set_graph_data_limit(n_items=20)
device_3 = cbt_data_generator.set_graph_data_limit(n_items=20)
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
colors = {"background": "#303030", "text": "#FFFFFF", "graph-text": "#FFFFFF"}
# setup placement, update frequency, and general formatting
app.layout = html.Div(
[
html.H4("IoT Temperature Device Live Dashboard"),
html.Div(id="live-update-text"),
dcc.Graph(
id="live-update-graph",
animate=False,
style={
"width": "49%",
"display": "inline-block",
"vertical-align": "middle",
},
),
dcc.Interval(
id="interval-component", interval=1 * 1000, n_intervals=0 # in milliseconds
),
],
style={
"textAlign": "center",
"background-color": colors["background"],
"color": colors["text"],
},
)
# mechanism defining live updates
@app.callback(
Output("live-update-text", "children"), [Input("interval-component", "n_intervals")]
)
def update_metrics(n):
"""Updates the top level metrics
in the web app every refresh interval
"""
style = {"padding": "5px", "fontSize": "16px"}
all_device_row_list = cbt_data_generator.create_all_device_rows(
row_keys_list, n_rows=1
)
(
device_name_1,
device_temp_1,
temp_timestamp_1,
) = cbt_data_generator.get_name_temp_time(all_device_row_list, 0)
(
device_name_2,
device_temp_2,
temp_timestamp_2,
) = cbt_data_generator.get_name_temp_time(all_device_row_list, 1)
(
device_name_3,
device_temp_3,
temp_timestamp_3,
) = cbt_data_generator.get_name_temp_time(all_device_row_list, 2)
return [
html.Span("{0}: {1:.2f}".format(device_name_1, device_temp_1), style=style),
html.Span("{0}: {1:0.2f}".format(device_name_2, device_temp_2), style=style),
html.Span("{0}: {1:0.2f}".format(device_name_3, device_temp_3), style=style),
]
# Multiple components can update everytime interval gets fired
@app.callback(
Output("live-update-graph", "figure"), [Input("interval-component", "n_intervals")]
)
def update_graph_live(n):
"""Updates the 3 line charts with each new data point
refreshing a list object that holds n data points
depending on limit defined above
"""
# Create the graph with subplots
fig = plotly.subplots.make_subplots(rows=3, cols=1, vertical_spacing=0.1)
fig.update_layout(
autosize=True,
width=700,
height=600,
plot_bgcolor=colors["background"],
paper_bgcolor=colors["background"],
font={"color": colors["graph-text"]},
legend={
"x": 0.5,
"y": 1.1,
"xanchor": "center",
"yanchor": "top",
"orientation": "h",
},
margin={"l": 30, "r": 10, "b": 30, "t": 10},
)
# Update xaxis properties
fig.update_xaxes(title_text="Timestamp", row=3, col=1)
# Update yaxis properties
fig.update_yaxes(title_text="Temperature", row=2, col=1)
# setup data
all_device_row_list = cbt_data_generator.create_all_device_rows(
row_keys_list, n_rows=1
)
(
device_name_1,
device_temp_1,
temp_timestamp_1,
) = cbt_data_generator.get_name_temp_time(all_device_row_list, 0)
(
device_name_2,
device_temp_2,
temp_timestamp_2,
) = cbt_data_generator.get_name_temp_time(all_device_row_list, 1)
(
device_name_3,
device_temp_3,
temp_timestamp_3,
) = cbt_data_generator.get_name_temp_time(all_device_row_list, 2)
device_1["device_name"] = device_name_1
device_1["temp"].append(float(device_temp_1))
device_1["temp_timestamp"].append(temp_timestamp_1)
device_2["device_name"] = device_name_2
device_2["temp"].append(float(device_temp_2))
device_2["temp_timestamp"].append(temp_timestamp_2)
device_3["device_name"] = device_name_3
device_3["temp"].append(float(device_temp_3))
device_3["temp_timestamp"].append(temp_timestamp_3)
# append data to each device subplot
fig.append_trace(
{
"x": list(device_1["temp_timestamp"]),
"y": list(device_1["temp"]),
"name": device_1["device_name"],
"mode": "lines+markers",
"type": "scatter",
},
1,
1,
)
fig.append_trace(
{
"x": list(device_2["temp_timestamp"]),
"y": list(device_2["temp"]),
"name": device_2["device_name"],
"mode": "lines+markers",
"type": "scatter",
},
2,
1,
)
fig.append_trace(
{
"x": list(device_3["temp_timestamp"]),
"y": list(device_3["temp"]),
"name": device_3["device_name"],
"mode": "lines+markers",
"type": "scatter",
},
3,
1,
)
return fig
if __name__ == "__main__":
app.run_server(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))