-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
211 lines (182 loc) · 6.47 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
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from Organization import Organization
import plotly.graph_objs as go
from Device import VARIABLES
from random import choice
import os
Org = Organization()
devices_df = Org.devices_df
initial_device = choice(Org.Devices)
initial_name = initial_device.name
initial_variable = {
'L0': 'PARdw',
'L1': 'PARdw',
'L1_hourly': 'PARdw',
'L1_daily': 'precip'
}
initial_measure = 'L1_daily'
df = Org.device(name=initial_name).get_data(
var_list=[initial_variable[initial_measure]],
measure=initial_measure,
limit=1000).dropna()
data = [go.Scatter(
# Here we are initializing the data with a temporal x-axis.
x=df.index,
y=df[initial_variable[initial_measure]],
)]
def generate_table(dataframe, max_rows=60):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
[html.Tr([
html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))]
)
app = dash.Dash(__name__)
server = app.server
server.secret_key = os.environ.get('SECRET_KEY', None)
app.config.supress_callback_exceptions = True
app.layout = html.Div(children=[
html.H1(children='WSC Arable Mark Dashboard'),
html.Div([ # Viz output for Marks
html.Div([
html.Div([ # Dropdown selection of device.
html.Div(children='''Device Name'''),
dcc.Dropdown(
id='Device',
options=[
{'label': x.name, 'value': x.name} for x in Org.Devices], # NOQA
value=initial_name
)
], style={'width': '30%', 'display': 'inline-block'}), # NOQA
html.Div([ # Dropdown selection of measure
html.Div(children="Measure"),
dcc.Dropdown(
id='Measure',
options=[
{'label': i, 'value': i} for i in VARIABLES.keys()],
value=initial_measure
)
], style={'width': '30%', 'display': 'inline-block'}),
html.Div([ # Dropdown selection of device variable.
html.Div(children='''Variable'''),
dcc.Dropdown(id='Variable')
], style={'width': '30%', 'float': 'right', 'display': 'inline-block'}) # NOQA
]),
]),
html.Div([
dcc.Graph(
id='mark-graph',
figure={
'data': data
}
)
]),
html.Div([ # Input options for refining the device table display.
html.Div([ # Interactive text input for ID selection
html.Div(children='''Device Name'''),
dcc.Input(id='Name', value='A000', type="text")
], style={'width': '48%', 'display': 'inline-block'}),
html.Div([ # Dropdown selection of device state.
html.Div(children='''Device State'''),
dcc.Dropdown(
id='State',
options=[
{'label': i, 'value': i} for i in devices_df['state'].unique()], # NOQA
value='Active'
)
], style={'width': '48%', 'float': 'right', 'display': 'inline-block'})
]),
html.Div(id='test'),
# Table of proposals by PI
html.Table(id='device-table'),
])
@app.callback(
dash.dependencies.Output(
component_id='Variable', component_property='options'
),
[dash.dependencies.Input(
component_id='Measure', component_property='value'
)]
)
def set_variable_options(selected_measure):
return [{'label': i, 'value': i} for i in VARIABLES[selected_measure]]
@app.callback(
dash.dependencies.Output(
component_id='Variable', component_property='value'),
[dash.dependencies.Input(
component_id='Measure', component_property='value'
)]
)
def set_variable_value(current_measure):
return initial_variable[current_measure]
@app.callback(
Output(component_id='mark-graph', component_property='figure'),
[
Input(component_id='Device', component_property='value'),
Input(component_id='Measure', component_property='value'),
Input(component_id='Variable', component_property='value')
])
def update_mark_graph(name, measure, variable):
# Catch cases where we've updated measure and no variable has been set yet.
# This generally only happens on initial page load.
if not variable:
variable = initial_variable[measure]
# Gather the data and update the plot.
this_df = Org.device(name=name).get_data(
var_list=[variable], measure=measure, limit=1000).dropna()
this_data = [go.Scatter(
x=this_df.index,
y=this_df[variable]
)]
return {
'data': this_data,
'layout': go.Layout(
title='Arable Mark {id}, {measure} data, {var}'.format(
id=name, measure=measure, var=variable),
xaxis={'title': 'Time'},
yaxis={'type': 'linear', 'title': variable},
)
}
@app.callback(
Output(component_id='device-table', component_property='children'),
[
Input(component_id='Name', component_property='value'),
Input(component_id='State', component_property='value')
]
)
def update_mark_search(name, state):
this_df = devices_df.loc[
(devices_df['name'].str.contains(name)) &
(devices_df['state'] == state)]
# this_df = devices
# return generate_table(devices)
return generate_table(this_df.filter(
items=[
# 'firmware',
# 'flags',
# 'id',
'last_deploy',
'last_post',
'last_seen',
'location',
'model',
'name',
# 'reported_fw',
'signal_strength',
'state',
'sync_interval'
]
).sort_values(
['last_seen', 'signal_strength'],
ascending=[False, False]),
)
app.css.append_css(
{"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
if __name__ == '__main__':
app.run_server(debug=True)