-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
402 lines (342 loc) · 11.8 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""
Copyright (C) 2019 - PRESENT Zhengyu Peng
E-mail: zpeng.me@gmail.com
Website: https://zpeng.me
` `
-:. -#:
-//:. -###:
-////:. -#####:
-/:.://:. -###++##:
.. `://:- -###+. :##:
`:/+####+. :##:
.::::::::/+###. :##:
.////-----+##: `:###:
`-//:. :##: `:###/.
`-//:. :##:`:###/.
`-//:+######/.
`-/+####/.
`+##+.
:##:
:##:
:##:
:##:
:##:
.+:
"""
import json
import os
import shutil
# from waitress import serve
from multiprocessing import freeze_support
from flaskwebgui import FlaskUI
import dash
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
from utils import load_config, save_config
from view_callbacks.test_case_view import get_test_case_view_callbacks
from view_callbacks.control_view import get_control_view_callbacks
from view_callbacks.scatter_3d_view import get_scatter_3d_view_callbacks
from view_callbacks.scatter_2d_left_view import get_scatter_2d_left_view_callbacks
from view_callbacks.scatter_2d_right_view import get_scatter_2d_right_view_callbacks
from view_callbacks.heatmap_view import get_heatmap_view_callbacks
from view_callbacks.histogram_view import get_histogram_view_callbacks
from view_callbacks.parcats_view import get_parcats_view_callbacks
from view_callbacks.violin_view import get_violin_view_callbacks
from app_config import APP_TITLE, DATA_PATH
from app_config import SPECIAL_FOLDERS
from app_layout import get_app_layout
app = dash.Dash(
__name__,
meta_tags=[{"name": "viewport", "content": "width=device-width,initial-scale=1"}],
)
app.scripts.config.serve_locally = True
app.css.config.serve_locally = True
app.title = APP_TITLE
app.layout = get_app_layout
@app.callback(
output={
"data_path": Output("data-path-modal", "value"),
},
inputs={"is_modal_open": Input("modal-centered", "is_open")},
)
def on_modal_open(is_modal_open):
"""
Callback function to update the data path when the modal is opened.
Parameters:
- is_modal_open (bool): Whether the modal is open.
Returns:
- dict: A dictionary containing the data path.
"""
if not is_modal_open:
raise PreventUpdate
if os.path.isfile("./config.json"):
config = load_config("./config.json")
else:
config = {"DATA_PATH": DATA_PATH}
save_config(config, "./config.json")
data_path = config.get("DATA_PATH", DATA_PATH)
if os.path.exists("./temp"):
shutil.rmtree("./temp")
os.makedirs("./temp")
return {
"data_path": data_path,
}
@app.callback(
output={
"case_options": Output("case-picker-modal", "options"),
"case_value": Output("case-picker-modal", "value"),
},
inputs={
"data_path": Input("data-path-modal", "value"),
"unused_refresh": Input("refresh-button-modal", "n_clicks"),
},
)
def on_path_change(data_path, unused_refresh):
"""
Callback function to update the case options and value when the data path changes.
Parameters:
- data_path (str): The path to the data directory.
- unused_refresh (int): The number of times the refresh button has been clicked.
Returns:
- dict: A dictionary containing the case options and value.
"""
config = load_config("./config.json")
stored_case = config.get("CASE", "")
options = []
try:
obj = os.scandir(data_path)
except OSError:
return {
"case_options": "",
"case_value": "",
}
for entry in obj:
if entry.is_dir():
# only add the folder with 'info.json'
if os.path.exists(os.path.join(data_path, entry.name, "info.json")):
options.append({"label": entry.name, "value": entry.name})
case_val = options[0]["value"]
# check previously loaded case in the browser's cache
if stored_case:
for _, case in enumerate(options):
if stored_case == case["value"]:
case_val = stored_case
break
return {
"case_options": options,
"case_value": case_val,
}
@app.callback(
output={
"file_value": Output("file-picker-modal", "value"),
"file_options": Output("file-picker-modal", "options"),
},
inputs={
"case_val": Input("case-picker-modal", "value"),
},
state={
"data_path": State("data-path-modal", "value"),
},
)
def on_case_change(case_val, data_path):
"""
Callback function to update the file options and value when the case changes.
Parameters:
- case_val (str): The name of the case.
- data_path (str): The path to the data directory.
Returns:
- dict: A dictionary containing the file options and value.
"""
config = load_config("./config.json")
stored_file = config.get("FILE", "")
if not case_val:
return {
"file_value": "",
"file_options": "",
}
case_dir = os.path.join(data_path, case_val)
data_files = []
for dirpath, dirnames, files in os.walk(case_dir):
dirnames[:] = [d for d in dirnames if d not in SPECIAL_FOLDERS]
for name in files:
if name.lower().endswith(".csv"):
data_files.append(
{
"label": os.path.join(dirpath[len(case_dir) :], name),
"value": json.dumps(
{
"path": dirpath,
"name": name,
"label": os.path.join(dirpath[len(case_dir) :], name),
}
),
}
)
elif name.lower().endswith(".pkl"):
data_files.append(
{
"label": os.path.join(dirpath[len(case_dir) :], name),
"value": json.dumps(
{
"path": dirpath,
"name": name,
"label": os.path.join(dirpath[len(case_dir) :], name),
}
),
}
)
if not data_files:
return {
"file_value": "",
"file_options": "",
}
file_value = data_files[0]["value"]
if stored_file:
for _, file in enumerate(data_files):
if stored_file == file["value"]:
file_value = stored_file
break
config["DATA_PATH"] = data_path
config["CASE"] = case_val
config["FILE"] = file_value
save_config(config, "./config.json")
return {
"file_value": file_value,
"file_options": data_files,
}
@app.callback(
output={
"modal_is_open": Output("modal-centered", "is_open", allow_duplicate=True),
"data_path_str": Output("data-path", "value"),
"test_case_str": Output("test-case", "value"),
"log_file_str": Output("log-file", "value"),
"current_file_update": Output("current-file", "data"),
"add_file_value": Output("file-add", "value"),
"add_file_options": Output("file-add", "options"),
},
inputs={
"unused_ok_modal": Input("ok-modal", "n_clicks"),
},
state={
"data_path": State("data-path-modal", "value"),
"case_val": State("case-picker-modal", "value"),
"file_value": State("file-picker-modal", "value"),
"file_options": State("file-picker-modal", "options"),
"current_file": State("current-file", "data"),
},
prevent_initial_call=True,
)
def on_modal_close(
unused_ok_modal, data_path, case_val, file_value, file_options, current_file
):
"""
Callback function to update the data path, test case, log file,
and current file when the modal is closed.
Parameters:
- unused_ok_modal (int): The number of times the OK button has been clicked.
- data_path (str): The path to the data directory.
- case_val (str): The name of the case.
- file_value (str): The value of the selected file.
- file_options (list): The list of file options.
- current_file (str): The current file.
Returns:
- dict: A dictionary containing the updated values.
"""
if not file_value:
raise PreventUpdate
config = load_config("./config.json")
file_dict = json.loads(file_value)
config["DATA_PATH"] = data_path
config["CASE"] = case_val
config["FILE"] = file_value
save_config(config, "./config.json")
if current_file == file_value:
return {
"modal_is_open": False,
"data_path_str": data_path,
"test_case_str": case_val,
"log_file_str": file_dict["label"],
"current_file_update": dash.no_update,
"add_file_value": dash.no_update,
"add_file_options": dash.no_update,
}
return {
"modal_is_open": False,
"data_path_str": data_path,
"test_case_str": case_val,
"log_file_str": file_dict["label"],
"current_file_update": file_value,
"add_file_value": [],
"add_file_options": file_options,
}
@app.callback(
output={
"modal_is_open": Output("modal-centered", "is_open", allow_duplicate=True),
},
inputs={
"unused_select_modal": Input("select-button", "n_clicks"),
},
prevent_initial_call=True,
)
def open_modal(unused_select_modal):
"""
Callback function to open the modal.
Parameters:
- unused_select_modal (int): The number of times the select button has been clicked.
Returns:
- dict: A dictionary containing the updated value for the modal's is_open property.
"""
return {"modal_is_open": True}
# This clientside callback function disables the interval component based on
# the number of clicks on the play button and stop button. If the play button
# is clicked and the number of play clicks is greater than 0, the interval
# component is disabled. If the stop button is clicked and the number of stop
# clicks is greater than 0, the interval component is enabled. If neither button
# is clicked, the interval component remains unchanged.
app.clientside_callback(
"""
function(play_clicks, stop_clicks) {
const triggered = dash_clientside.callback_context.triggered.map(
t => t.prop_id
);
if (triggered.length > 0) {
if (triggered[0].includes('play-button')) {
if (play_clicks>0){
return false;
}
else {
return window.dash_clientside.no_update;
}
}
if (triggered[0].includes('stop-button')) {
if (stop_clicks>0){
return true;
}
else {
return window.dash_clientside.no_update;
}
}
}
return window.dash_clientside.no_update;
}
""",
Output("interval-component", "disabled"),
Input("play-button", "n_clicks"),
Input("stop-button", "n_clicks"),
)
get_test_case_view_callbacks(app)
get_control_view_callbacks(app)
get_scatter_3d_view_callbacks(app)
get_scatter_2d_left_view_callbacks(app)
get_scatter_2d_right_view_callbacks(app)
get_heatmap_view_callbacks(app)
get_histogram_view_callbacks(app)
get_parcats_view_callbacks(app)
get_violin_view_callbacks(app)
if __name__ == "__main__":
freeze_support()
# app.run_server(debug=True, threaded=True, processes=1, host="0.0.0.0")
# serve(app.server, listen="*:8000")
FlaskUI(
app=app.server, server="flask", port=45678, profile_dir_prefix="sensorview"
).run()