/
app.py
321 lines (277 loc) · 9.63 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
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
#! /usr/bin/python3.6
# -*- coding: utf-8 -*-
#Ricardos.geral@gmail.com
#Visualization of sensor data from relier acquisition system using Dash/Plotly on Heroku
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from plotly.graph_objs import *
import numpy as np
import pandas.io.sql as psql
import psycopg2 as p
import urllib.parse as urlparse
import os
url = urlparse.urlparse(os.environ['DATABASE_URL'])
dbname = url.path[1:]
user = url.username
password = url.password
host = url.hostname
port = url.port
app = dash.Dash()
app.title='relier web stream'
interval = 3*1000 # milliseconds
app.layout = html.Div([
html.Div([
html.H2("Sensor data streaming"),
html.Img(
src="https://raw.githubusercontent.com/Ricardosgeral/relier/master/Nextion/Illustrator/relier_dash-banner_.png"),
], className='banner'),
html.Div([
# html.Div([
# html.H5("Plots")
# ], className='Title'),
html.Div([
dcc.Graph(id='plots'),
], className='row'),
html.Div([
html.H6(""),
], className='row'),
html.Div([
html.H3("App developed by Ricardo Correia dos Santos | 2018"),
html.H3("ricardos.geral @ gmail.com"),
], className='Title'),
dcc.Interval(id='data-update', interval=interval, n_intervals=0),
], className='row wind-speed-row'),
],
style={'padding': '0px 10px 15px 10px',
'marginLeft': 'auto', 'marginRight': 'auto', "width": "900px",
'boxShadow': '0px 0px 5px 5px rgba(204,204,204,0.4)'})
def connect_db():
con = p.connect(
dbname=dbname,
user=user,
password=password,
host=host,
port=port
)
df_inputs = psql.read_sql('SELECT * FROM testinputs ORDER BY test_name;', con)
df = psql.read_sql('SELECT * FROM testdata ORDER BY id;', con) # outputs
test_name = df_inputs['test_name'][0]
started = df_inputs['start'][0]
test_type = df_inputs['test_type'][0]
if test_type == 1:
test_type_name = 'FLET'
elif test_type == 2:
test_type_name = 'CFET'
elif test_type == 3:
test_type_name = 'HET'
else:
test_type_name = "Other"
con.close()
return started, test_name, test_type_name, df
@app.callback(Output('plots', 'figure'), [Input('data-update', 'n_intervals')])
def plots(interval):
started, test_name, test_type_name, df = connect_db()
###########################################
flow = Scatter(
y=df['flow'],
x=df['duration']/ np.timedelta64(1, 'm'),
name='Flow rate ',
mode='lines+markers',
line=Line(color='green', width= 2.0, shape='linear', dash = 'solid'),
marker=Marker(color='green', symbol='circle-open', opacity=0.7, size=6.5,
line=dict(width=1.5, color='green')
), #https://plot.ly/python/reference/#scatter-marker
connectgaps = False,
legendgroup = 'flowmeter',
yaxis='y2',
)
volume = Scatter(
x=df['duration'] / np.timedelta64(1, 'm'),
y=df['volume'],
name='Total volume',
mode='lines',
opacity = 0.5,
line=Line(color='blue', width=1.5, shape='linear', dash='solid'),
connectgaps=False,
fill='tonexty',
fillcolor='#f4fbff',
legendgroup='flowmeter',
)
up_press = Scatter(
x=df['duration']/ np.timedelta64(1, 'm'),
y=df['mmh2o_up'],
name='Ups pressure',
mode='lines+markers',
line=Line(color='blue', width= 1.5, shape='linear', dash = 'solid'),
marker=Marker(color='blue', symbol='square-open', opacity=0.6, size=6.5,
line=dict(width=1.5, color='blue')
),
xaxis = 'x',
yaxis='y3',
legendgroup='pressures',
)
int_press = Scatter(
x=df['duration']/ np.timedelta64(1, 'm'),
y=df['mmh2o_int'],
name='Int pressure',
mode='lines+markers',
line=Line(color='green', width= 1.5, shape='linear', dash = 'solid'),
marker=Marker(color='green', symbol='square-open', opacity=0.6, size=6.5,
line=dict(width=1.5, color='green')
),
xaxis = 'x',
yaxis='y3',
legendgroup='pressures',
)
down_press = Scatter(
x=df['duration']/ np.timedelta64(1, 'm'),
y=df['mmh2o_down'],
mode='lines+markers',
line=Line(color='orange', width= 1.5, shape='linear', dash = 'solid'),
marker=Marker(color='orange', symbol='square-open', opacity=0.6, size=6.5,
line=dict(width=1.5, color='orange')
),
name='Dwn pressure',
xaxis='x',
yaxis='y3',
legendgroup='pressures',
)
turbidity = Scatter(
x=df['duration']/ np.timedelta64(1, 'm'),
y=df['turb'],
mode='lines+markers',
line=Line(color='blue', width=1.5, shape='linear', dash='solid'),
marker=Marker(color='blue', symbol='diamond-open', opacity=0.6, size=6.5,
line=dict(width=1.5, color='blue')
),
name='Turbidity',
xaxis='x',
yaxis='y4',
legendgroup='turbidity_meter',
)
traces = [flow, volume, down_press, int_press, up_press, turbidity]
inputs = 'TEST: {}.csv (type - {}), started on: {:%Y-%m-%d, %H:%M}'.format(test_name, test_type_name, started)
layout = Layout(
height=1200,
title = inputs,
titlefont=dict(family='Arial, bold', size=16, color='black'),
xaxis=dict(
autorange=True,
rangemode='nonnegative',
showgrid=True,
showline=True,
linecolor='#adadad',
linewidth=2,
zeroline=False,
zerolinewidth=2,
fixedrange=False,
autotick=True,
tickfont = dict(size=14, color='#646566'),
ticks='inside',
tick0=0,
dtick=1,
ticklen=8,
tickwidth=2,
tickcolor='#adadad',
showticklabels=True,
title='Duration (minutes)',
titlefont=dict(size=15),
showaxeslabels=True,
side= 'top',
mirror='allticks',
),
yaxis=dict(
autorange=True,
rangemode='nonnegative',
showgrid=True,
ticks='inside',
tickfont = dict(size=14, color='#646566'),
tickcolor='#adadad',
tickwidth=2,
showline=True,
linecolor='#adadad',
linewidth=2,
fixedrange=False,
zeroline=True,
title='Volume (liters)',
titlefont=dict(size=15),
domain =[0.5966,0.88],
side = 'right'
),
yaxis2=dict(
autorange=True,
rangemode='nonnegative',
showgrid=True,
showline=True,
linecolor='#adadad',
linewidth=2,
fixedrange=False,
zeroline=False,
title='Flow rate (liters/hour)',
titlefont=dict(size=15),
overlaying='y',
side='left',
ticks='inside',
tickfont = dict(size=14, color='#646566'),
tickcolor='#adadad',
tickwidth=2,
mirror = 'False',
),
yaxis3=dict(
autorange=True,
showline=True,
linecolor='#adadad',
linewidth=2,
fixedrange=False,
zeroline=False,
title='Piezometric pressure (mmH2O)',
titlefont=dict(size=15),
domain=[0.2983, 0.582],
ticks='inside',
tickcolor='#adadad',
tickfont = dict(size=14, color='#646566'),
tickwidth=2,
mirror = 'allticks',
),
yaxis4=dict(
autorange=True,
showline=True,
linecolor='#adadad',
linewidth=2,
fixedrange=False,
zeroline=False,
title='Turbidity (grams/liter)',
titlefont=dict(size=15),
domain = [0, 0.2833],
ticks='inside',
tickfont = dict(size=14, color='#646566'),
tickcolor='#adadad',
tickwidth=2,
mirror='allticks',
),
margin=Margin(
t=50,
l=60,
r=60,
b=20,
pad=1.5 # distance between graph and axis numbers
),
legend = dict(x=-0.1, y=1.005, orientation="h", font=dict(size=14.5)),
#paper_bgcolor='#7f7f7f',
#plot_bgcolor='#f6f6f6',
)
return Figure(data=traces, layout=layout)
external_css = ["https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css",
"https://cdn.rawgit.com/plotly/dash-app-stylesheets/737dc4ab11f7a1a8d6b5645d26f69133d97062ae/dash-wind-streaming.css",
"https://fonts.googleapis.com/css?family=Raleway:400,400i,700,700i",
"https://fonts.googleapis.com/css?family=Product+Sans:400,400i,700,700i"]
for css in external_css:
app.css.append_css({"external_url": css})
# if 'DYNO' in os.environ:
# app.scripts.append_script({
# 'external_url': 'https://cdn.rawgit.com/chriddyp/ca0d8f02a1659981a0ea7f013a378bbd/raw/e79f3f789517deec58f41251f7dbb6bee72c44ab/plotly_ga.js'
# })
if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0', port=os.environ.get('PORT'))