Skip to content

Commit 6886a61

Browse files
authored
Merge pull request #97 from predict-idlab/figresampler_display_improvements
🌅 FigureResampler display improvements
2 parents 4d1c888 + b80675a commit 6886a61

File tree

4 files changed

+160
-32
lines changed

4 files changed

+160
-32
lines changed

plotly_resampler/figure_resampler/figure_resampler.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
__author__ = "Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost"
1212

1313
import warnings
14-
from typing import Tuple
14+
from typing import Tuple, List
1515

1616
import dash
1717
import plotly.graph_objects as go
@@ -41,6 +41,7 @@ def __init__(
4141
show_mean_aggregation_size: bool = True,
4242
convert_traces_kwargs: dict | None = None,
4343
verbose: bool = False,
44+
show_dash_kwargs: dict | None = None,
4445
):
4546
# `pr_props`` is a variable to store properties of a plotly-resampler figure
4647
# This variable will only be set when loading a pickled plotly-resampler figure
@@ -85,6 +86,8 @@ def __init__(
8586
# A single trace dict or a list of traces
8687
f.add_traces(figure)
8788

89+
self._show_dash_kwargs = show_dash_kwargs if show_dash_kwargs is not None else {}
90+
8891
super().__init__(
8992
f,
9093
convert_existing_traces,
@@ -155,6 +158,8 @@ def show_dash(
155158
See more https://dash.plotly.com/dash-core-components/graph
156159
**kwargs: dict
157160
Additional app.run_server() kwargs. e.g.: port
161+
Note that these kwargs take precedence over the ones passed to the
162+
constructor via the ``show_dash_kwargs`` argument.
158163
159164
"""
160165
graph_properties = {} if graph_properties is None else graph_properties
@@ -175,14 +180,19 @@ def show_dash(
175180

176181
# 2. Run the app
177182
if (
178-
self.layout.height is not None
179-
and mode == "inline"
183+
mode == "inline"
180184
and "height" not in kwargs
181185
):
182-
# If figure height is specified -> re-use is for inline dash app height
183-
kwargs["height"] = self.layout.height + 18
186+
# If app height is not specified -> re-use figure height for inline dash app
187+
# Note: default layout height is 450 (whereas default app height is 650)
188+
# See: https://plotly.com/python/reference/layout/#layout-height
189+
fig_height = self.layout.height if self.layout.height is not None else 450
190+
kwargs["height"] = fig_height + 18
191+
192+
# kwargs take precedence over the show_dash_kwargs
193+
kwargs = {**self._show_dash_kwargs, **kwargs}
184194

185-
# store the app information, so it can be killed
195+
# Store the app information, so it can be killed
186196
self._app = app
187197
self._host = kwargs.get("host", "127.0.0.1")
188198
self._port = kwargs.get("port", "8050")
@@ -238,3 +248,11 @@ def register_update_graph_callback(
238248
dash.dependencies.Input(graph_id, "relayoutData"),
239249
prevent_initial_call=True,
240250
)(self.construct_update_data)
251+
252+
def _get_pr_props_keys(self) -> List[str]:
253+
# Add the additional plotly-resampler properties of this class
254+
return super()._get_pr_props_keys() + ["_show_dash_kwargs"]
255+
256+
def _ipython_display_(self):
257+
# To display the figure inline as a dash app
258+
self.show_dash(mode="inline")

plotly_resampler/figure_resampler/figure_resampler_interface.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,25 @@ def _re_matches(regex: re.Pattern, strings: Iterable[str]) -> List[str]:
12731273
return sorted(matches)
12741274

12751275
## Magic methods (to use plotly.py words :grin:)
1276+
1277+
def _get_pr_props_keys(self) -> List[str]:
1278+
"""Returns the keys (i.e., the names) of the plotly-resampler properties.
1279+
1280+
Note
1281+
----
1282+
This method is used to serialize the object in the `__reduce__` method.
1283+
1284+
"""
1285+
return [
1286+
"_hf_data",
1287+
"_global_n_shown_samples",
1288+
"_print_verbose",
1289+
"_show_mean_aggregation_size",
1290+
"_prefix",
1291+
"_suffix",
1292+
"_global_downsampler",
1293+
]
1294+
12761295
def __reduce__(self):
12771296
"""Overwrite the reduce method (which is used to support deep copying and
12781297
pickling).
@@ -1288,15 +1307,6 @@ def __reduce__(self):
12881307

12891308
# Add the plotly-resampler properties
12901309
props["pr_props"] = {}
1291-
pr_keys = [
1292-
"_hf_data",
1293-
"_global_n_shown_samples",
1294-
"_print_verbose",
1295-
"_show_mean_aggregation_size",
1296-
"_prefix",
1297-
"_suffix",
1298-
"_global_downsampler",
1299-
]
1300-
for k in pr_keys:
1310+
for k in self._get_pr_props_keys():
13011311
props["pr_props"][k] = getattr(self, k)
13021312
return (self.__class__, (props,)) # (props,) to comply with plotly magic

tests/test_registering.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,12 @@ def test_registering_plotly_express_and_kwargs(registering_cleanup):
127127
assert len(fig.data) == 1
128128
assert len(fig.data[0].y) == 500
129129

130-
register_plotly_resampler(default_n_shown_samples=50)
130+
register_plotly_resampler(
131+
default_n_shown_samples=50, show_dash_kwargs=dict(mode="inline", port=8051)
132+
)
131133
fig = px.scatter(y=np.arange(500))
132134
assert isinstance(fig, FigureResampler)
135+
assert fig._show_dash_kwargs == dict(mode="inline", port=8051)
133136
assert len(fig.data) == 1
134137
assert len(fig.data[0].y) == 50
135138
assert len(fig.hf_data) == 1
@@ -138,6 +141,7 @@ def test_registering_plotly_express_and_kwargs(registering_cleanup):
138141
register_plotly_resampler()
139142
fig = px.scatter(y=np.arange(5000))
140143
assert isinstance(fig, FigureResampler)
144+
assert fig._show_dash_kwargs == dict()
141145
assert len(fig.data) == 1
142146
assert len(fig.data[0].y) == 1000
143147
assert len(fig.hf_data) == 1
@@ -201,4 +205,3 @@ def test_compasibility_when_registered(registering_cleanup):
201205
assert len(f.data[0].y) == 1000
202206
assert len(f.hf_data) == 1
203207
assert len(f.hf_data[0]["y"]) == 1005
204-

0 commit comments

Comments
 (0)