-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path12_file_selector.py
130 lines (109 loc) · 4.19 KB
/
12_file_selector.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
"""Dash file parquet visualization app example.
In this use case, we have dropdowns which allows the end-user to select multiple
parquet files, which are visualized using FigureResampler after clicking on a button.
"""
__author__ = "Jonas Van Der Donckt"
from pathlib import Path
from typing import List
import dash_bootstrap_components as dbc
import plotly.graph_objects as go
from dash import Input, Output, State, callback_context, dcc, html, no_update
from dash_extensions.enrich import (
DashProxy,
ServersideOutput,
ServersideOutputTransform,
)
from trace_updater import TraceUpdater
from utils.callback_helpers import get_selector_states, multiple_folder_file_selector
from utils.graph_construction import visualize_multiple_files
from plotly_resampler import FigureResampler
# --------------------------------------Globals ---------------------------------------
app = DashProxy(
__name__,
external_stylesheets=[dbc.themes.LUX],
transforms=[ServersideOutputTransform()],
)
# --------- File selection configurations ---------
name_folder_list = [
{
# the key-string below is the title which will be shown in the dash app
"example data": {"folder": Path(__file__).parent.parent.joinpath("data")},
"other folder": {"folder": Path(__file__).parent.parent.joinpath("data")},
},
# NOTE: A new item om this level creates a new file-selector card.
# { "PC data": { "folder": Path("/home/jonas/data/wesad/empatica/") } }
# TODO: change the folder path above to a location where you have some
# `.parquet` files stored on your machine.
]
# --------- DASH layout logic ---------
def serve_layout() -> dbc.Container:
"""Constructs the app's layout.
Returns
-------
dbc.Container
A Container withholding the layout.
"""
return dbc.Container(
[
dbc.Container(
html.H1("Data loading and visualization dashboard"),
style={"textAlign": "center"},
),
html.Hr(),
dbc.Row(
[
# Add file selection layout (+ assign callbacks)
dbc.Col(multiple_folder_file_selector(app, name_folder_list), md=2),
# Add the graph, the dcc.Store (for serialization) and the
# TraceUpdater (for efficient data updating) components
dbc.Col(
[
dcc.Graph(id="graph-id", figure=go.Figure()),
dcc.Loading(dcc.Store(id="store")),
TraceUpdater(id="trace-updater", gdID="graph-id"),
],
md=10,
),
],
align="center",
),
],
fluid=True,
)
app.layout = serve_layout()
# ------------------------------------ DASH logic -------------------------------------
@app.callback(
[Output("graph-id", "figure"), ServersideOutput("store", "data")],
[Input("plot-button", "n_clicks"), *get_selector_states(len(name_folder_list))],
prevent_initial_call=True,
)
def plot_graph(n_clicks, *folder_list):
it = iter(folder_list)
file_list: List[Path] = []
for folder, files in zip(it, it):
if not all((folder, files)):
continue
else:
for file in files:
file_list.append((Path(folder).joinpath(file)))
ctx = callback_context
if len(ctx.triggered) and "plot-button" in ctx.triggered[0]["prop_id"]:
if len(file_list):
fig: FigureResampler = visualize_multiple_files(file_list)
return fig, fig
else:
return no_update
# --------- Figure update callback ---------
@app.callback(
Output("trace-updater", "updateData"),
Input("graph-id", "relayoutData"),
State("store", "data"), # The server side cached FigureResampler per session
prevent_initial_call=True,
)
def update_fig(relayoutdata, fig):
if fig is None:
return no_update
return fig.construct_update_data(relayoutdata)
# --------------------------------- Running the app ---------------------------------
if __name__ == "__main__":
app.run_server(debug=True, port=9023)