Skip to content

Commit 27857b7

Browse files
authored
Merge pull request #114 from predict-idlab/tweaks
🔧 Tweaks - improve code quality, fix type-checking bug when IPywidgets is not installed & loosen up plotly-version
2 parents 5352bb2 + 4d2ead8 commit 27857b7

File tree

14 files changed

+73
-524
lines changed

14 files changed

+73
-524
lines changed

examples/dash_apps/01_minimal_global.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
1414
"""
1515

16-
import dash
1716
import numpy as np
1817
import plotly.graph_objects as go
19-
from dash import Input, Output, dcc, html
18+
from dash import Input, Output, dcc, html, Dash, no_update, callback_context
2019

2120
from plotly_resampler import FigureResampler
2221
from trace_updater import TraceUpdater
@@ -27,7 +26,7 @@
2726

2827

2928
# --------------------------------------Globals ---------------------------------------
30-
app = dash.Dash(__name__)
29+
app = Dash(__name__)
3130
fig: FigureResampler = FigureResampler()
3231
# NOTE: in this example, this reference to a FigureResampler is essential to preserve
3332
# throughout the whole dash app! If your dash app wants to create a new go.Figure(),
@@ -55,7 +54,7 @@
5554
prevent_initial_call=True,
5655
)
5756
def plot_graph(n_clicks):
58-
ctx = dash.callback_context
57+
ctx = callback_context
5958
if len(ctx.triggered) and "plot-button" in ctx.triggered[0]["prop_id"]:
6059
# Note how the replace method is used here on the global figure object
6160
global fig
@@ -64,7 +63,7 @@ def plot_graph(n_clicks):
6463
fig.add_trace(go.Scattergl(name="exp"), hf_x=x, hf_y=noisy_sin * 1.000002 ** x)
6564
return fig
6665
else:
67-
raise dash.exceptions.PreventUpdate()
66+
return no_update
6867

6968

7069
# Register the graph update callbacks to the layout

examples/dash_apps/02_minimal_cache.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
1010
"""
1111

12-
import dash
1312
import numpy as np
1413
import plotly.graph_objects as go
15-
from dash import Input, Output, State, dcc, html
14+
from dash import Input, Output, State, dcc, html, no_update, callback_context
1615
from dash_extensions.enrich import (
1716
DashProxy,
1817
ServersideOutput,
@@ -52,7 +51,7 @@
5251
memoize=True,
5352
)
5453
def plot_graph(n_clicks):
55-
ctx = dash.callback_context
54+
ctx = callback_context
5655
if len(ctx.triggered) and "plot-button" in ctx.triggered[0]["prop_id"]:
5756
fig: FigureResampler = FigureResampler(go.Figure())
5857

@@ -62,7 +61,7 @@ def plot_graph(n_clicks):
6261

6362
return fig, fig
6463
else:
65-
raise dash.exceptions.PreventUpdate()
64+
return no_update
6665

6766

6867
@app.callback(
@@ -74,7 +73,7 @@ def plot_graph(n_clicks):
7473
)
7574
def update_fig(relayoutdata, fig):
7675
if fig is None:
77-
raise dash.exceptions.PreventUpdate()
76+
return no_update
7877
return fig.construct_update_data(relayoutdata)
7978

8079

examples/dash_apps/03_minimal_cache_dynamic.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
import numpy as np
2020
import plotly.graph_objects as go
21-
import dash
22-
from dash import MATCH, Input, Output, State, dcc, html
21+
from dash import MATCH, Input, Output, State, dcc, html, no_update
2322
from dash_extensions.enrich import (
2423
DashProxy,
2524
ServersideOutput,
@@ -106,7 +105,7 @@ def construct_display_graph(n_clicks) -> FigureResampler:
106105
def update_fig(relayoutdata: dict, fig: FigureResampler):
107106
if fig is not None:
108107
return fig.construct_update_data(relayoutdata)
109-
raise dash.exceptions.PreventUpdate()
108+
return no_update
110109

111110

112111
# --------------------------------- Running the app ---------------------------------

examples/dash_apps/11_sine_generator.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
import numpy as np
1818
import plotly.graph_objects as go
19-
import dash
2019
import dash_bootstrap_components as dbc
21-
from dash import MATCH, Input, Output, State, dcc, html
20+
from dash import MATCH, Input, Output, State, dcc, html, no_update, callback_context
2221
from dash_extensions.enrich import (
2322
DashProxy,
2423
ServersideOutput,
@@ -116,16 +115,16 @@
116115
)
117116
def add_or_remove_graph(add_graph, remove_graph, n, exp, gc_children):
118117
if (add_graph is None or n is None or exp is None) and (remove_graph is None):
119-
raise dash.exceptions.PreventUpdate()
118+
return no_update
120119

121120
# Transform the graph data to a figure
122121
gc_children = [] if gc_children is None else gc_children
123122

124123
# Check if we need to remove a graph
125-
clicked_btns = [p["prop_id"] for p in dash.callback_context.triggered]
124+
clicked_btns = [p["prop_id"] for p in callback_context.triggered]
126125
if any("remove-graph" in btn_name for btn_name in clicked_btns):
127126
if not len(gc_children):
128-
raise dash.exceptions.PreventUpdate()
127+
return no_update
129128
return gc_children[:-1]
130129

131130
# No graph needs to be removed -> create a new graph
@@ -193,7 +192,7 @@ def construct_display_graph(n, exp, n_added_graphs) -> FigureResampler:
193192
def update_fig(relayoutdata: dict, fig: FigureResampler):
194193
if fig is not None:
195194
return fig.construct_update_data(relayoutdata)
196-
raise dash.exceptions.PreventUpdate()
195+
return no_update
197196

198197

199198
# --------------------------------- Running the app ---------------------------------

examples/dash_apps/12_file_selector.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
from pathlib import Path
1111
from typing import List
1212

13-
import dash
1413
import dash_bootstrap_components as dbc
1514
import plotly.graph_objects as go
16-
from dash import Input, Output, State, dcc, html
15+
from dash import Input, Output, State, dcc, html, no_update, callback_context
1716

1817
from dash_extensions.enrich import (
1918
DashProxy,
@@ -105,13 +104,13 @@ def plot_graph(n_clicks, *folder_list):
105104
for file in files:
106105
file_list.append((Path(folder).joinpath(file)))
107106

108-
ctx = dash.callback_context
107+
ctx = callback_context
109108
if len(ctx.triggered) and "plot-button" in ctx.triggered[0]["prop_id"]:
110109
if len(file_list):
111110
fig: FigureResampler = visualize_multiple_files(file_list)
112111
return fig, fig
113112
else:
114-
raise dash.exceptions.PreventUpdate()
113+
return no_update
115114

116115

117116
# --------- Figure update callback ---------
@@ -123,7 +122,7 @@ def plot_graph(n_clicks, *folder_list):
123122
)
124123
def update_fig(relayoutdata, fig):
125124
if fig is None:
126-
raise dash.exceptions.PreventUpdate()
125+
return no_update
127126
return fig.construct_update_data(relayoutdata)
128127

129128

examples/dash_apps/13_coarse_fine.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
__author__ = "Jonas Van Der Donckt"
1515

16-
import dash
1716
import dash_bootstrap_components as dbc
1817
import plotly.graph_objects as go
1918
from pathlib import Path
2019
from typing import List
21-
from dash import Input, Output, State, dcc, html
20+
from dash import Input, Output, State, dcc, html, no_update, callback_context
2221

2322
from dash_extensions.enrich import (
2423
DashProxy,
@@ -132,7 +131,7 @@ def construct_plot_graph(n_clicks, *folder_list):
132131
for file in files:
133132
file_list.append((Path(folder).joinpath(file)))
134133

135-
ctx = dash.callback_context
134+
ctx = callback_context
136135
if len(ctx.triggered) and "plot-button" in ctx.triggered[0]["prop_id"]:
137136
if len(file_list):
138137
# Create two graphs, a dynamic plotly-resampler graph and a coarse graph
@@ -159,7 +158,7 @@ def construct_plot_graph(n_clicks, *folder_list):
159158

160159
return coarse_fig, dynamic_fig, dynamic_fig
161160
else:
162-
raise dash.exceptions.PreventUpdate()
161+
return no_update
163162

164163

165164
# Register the graph update callbacks to the layout
@@ -172,17 +171,17 @@ def construct_plot_graph(n_clicks, *folder_list):
172171
)
173172
def update_dynamic_fig(coarse_grained_relayout, fine_grained_relayout, fr_fig):
174173
if fr_fig is None: # When the figure does not exist -> do nothing
175-
return dash.no_update
174+
return no_update
176175

177-
ctx = dash.callback_context
176+
ctx = callback_context
178177
trigger_id = ctx.triggered[0].get("prop_id", "").split(".")[0]
179178

180179
if trigger_id == "plotly-resampler-graph":
181180
return fr_fig.construct_update_data(fine_grained_relayout)
182181
elif trigger_id == "coarse-graph":
183182
return fr_fig.construct_update_data(coarse_grained_relayout)
184183

185-
return dash.no_update
184+
return no_update
186185

187186

188187
# --------------------------------- Running the app ---------------------------------

plotly_resampler/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
from google.colab import output
2828

2929
output.enable_custom_widget_manager()
30-
except ImportError:
30+
except (ImportError, ModuleNotFoundError):
3131
pass

plotly_resampler/aggregation/aggregators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
try:
2020
# The efficient c version of the LTTB algorithm
2121
from .algorithms.lttb_c import LTTB_core_c as LTTB_core
22-
except:
22+
except (ImportError, ModuleNotFoundError):
2323
import warnings
2424
warnings.warn("Could not import lttbc; will use a (slower) python alternative.")
2525
from .algorithms.lttb_py import LTTB_core_py as LTTB_core

plotly_resampler/figure_resampler/figure_resampler.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import base64
1818
import dash
1919
import plotly.graph_objects as go
20-
from dash import Dash
2120
from flask_cors import cross_origin
2221
from jupyter_dash import JupyterDash
2322
from plotly.basedatatypes import BaseFigure
@@ -320,7 +319,7 @@ def __init__(
320319
self.data[idx].update(graph_dict["data"][idx])
321320

322321
# The FigureResampler needs a dash app
323-
self._app: JupyterDash | Dash | None = None
322+
self._app: JupyterDash | dash.Dash | None = None
324323
self._port: int | None = None
325324
self._host: str | None = None
326325

plotly_resampler/figure_resampler/figure_resampler_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ def _parse_get_trace_props(
697697
# not will be transformed to an array of ints (0, 1))
698698
try:
699699
hf_y = pd.to_numeric(hf_y, errors="raise")
700-
except:
700+
except ValueError:
701701
hf_y = hf_y.astype("str")
702702

703703
# orjson encoding doesn't like to encode with uint8 & uint16 dtype

0 commit comments

Comments
 (0)