Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions plotly_resampler/figure_resampler/figure_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
__author__ = "Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost"

import warnings
from typing import Tuple
from typing import Tuple, List

import dash
import plotly.graph_objects as go
Expand Down Expand Up @@ -41,6 +41,7 @@ def __init__(
show_mean_aggregation_size: bool = True,
convert_traces_kwargs: dict | None = None,
verbose: bool = False,
show_dash_kwargs: dict | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no (generated) documentation for this arg as the documentation from the superclass its __init__ is used

):
# `pr_props`` is a variable to store properties of a plotly-resampler figure
# This variable will only be set when loading a pickled plotly-resampler figure
Expand Down Expand Up @@ -85,6 +86,8 @@ def __init__(
# A single trace dict or a list of traces
f.add_traces(figure)

self._show_dash_kwargs = show_dash_kwargs if show_dash_kwargs is not None else {}

super().__init__(
f,
convert_existing_traces,
Expand Down Expand Up @@ -155,6 +158,8 @@ def show_dash(
See more https://dash.plotly.com/dash-core-components/graph
**kwargs: dict
Additional app.run_server() kwargs. e.g.: port
Note that these kwargs take precedence over the ones passed to the
constructor via the ``show_dash_kwargs`` argument.

"""
graph_properties = {} if graph_properties is None else graph_properties
Expand All @@ -175,14 +180,19 @@ def show_dash(

# 2. Run the app
if (
self.layout.height is not None
and mode == "inline"
mode == "inline"
and "height" not in kwargs
):
# If figure height is specified -> re-use is for inline dash app height
kwargs["height"] = self.layout.height + 18
# If app height is not specified -> re-use figure height for inline dash app
# Note: default layout height is 450 (whereas default app height is 650)
# See: https://plotly.com/python/reference/layout/#layout-height
fig_height = self.layout.height if self.layout.height is not None else 450
kwargs["height"] = fig_height + 18

# kwargs take precedence over the show_dash_kwargs
kwargs = {**self._show_dash_kwargs, **kwargs}

# store the app information, so it can be killed
# Store the app information, so it can be killed
self._app = app
self._host = kwargs.get("host", "127.0.0.1")
self._port = kwargs.get("port", "8050")
Expand Down Expand Up @@ -238,3 +248,11 @@ def register_update_graph_callback(
dash.dependencies.Input(graph_id, "relayoutData"),
prevent_initial_call=True,
)(self.construct_update_data)

def _get_pr_props_keys(self) -> List[str]:
# Add the additional plotly-resampler properties of this class
return super()._get_pr_props_keys() + ["_show_dash_kwargs"]

def _ipython_display_(self):
# To display the figure inline as a dash app
self.show_dash(mode="inline")
30 changes: 20 additions & 10 deletions plotly_resampler/figure_resampler/figure_resampler_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,25 @@ def _re_matches(regex: re.Pattern, strings: Iterable[str]) -> List[str]:
return sorted(matches)

## Magic methods (to use plotly.py words :grin:)

def _get_pr_props_keys(self) -> List[str]:
"""Returns the keys (i.e., the names) of the plotly-resampler properties.

Note
----
This method is used to serialize the object in the `__reduce__` method.

"""
return [
"_hf_data",
"_global_n_shown_samples",
"_print_verbose",
"_show_mean_aggregation_size",
"_prefix",
"_suffix",
"_global_downsampler",
]

def __reduce__(self):
"""Overwrite the reduce method (which is used to support deep copying and
pickling).
Expand All @@ -1288,15 +1307,6 @@ def __reduce__(self):

# Add the plotly-resampler properties
props["pr_props"] = {}
pr_keys = [
"_hf_data",
"_global_n_shown_samples",
"_print_verbose",
"_show_mean_aggregation_size",
"_prefix",
"_suffix",
"_global_downsampler",
]
for k in pr_keys:
for k in self._get_pr_props_keys():
props["pr_props"][k] = getattr(self, k)
return (self.__class__, (props,)) # (props,) to comply with plotly magic
7 changes: 5 additions & 2 deletions tests/test_registering.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ def test_registering_plotly_express_and_kwargs(registering_cleanup):
assert len(fig.data) == 1
assert len(fig.data[0].y) == 500

register_plotly_resampler(default_n_shown_samples=50)
register_plotly_resampler(
default_n_shown_samples=50, show_dash_kwargs=dict(mode="inline", port=8051)
)
fig = px.scatter(y=np.arange(500))
assert isinstance(fig, FigureResampler)
assert fig._show_dash_kwargs == dict(mode="inline", port=8051)
assert len(fig.data) == 1
assert len(fig.data[0].y) == 50
assert len(fig.hf_data) == 1
Expand All @@ -138,6 +141,7 @@ def test_registering_plotly_express_and_kwargs(registering_cleanup):
register_plotly_resampler()
fig = px.scatter(y=np.arange(5000))
assert isinstance(fig, FigureResampler)
assert fig._show_dash_kwargs == dict()
assert len(fig.data) == 1
assert len(fig.data[0].y) == 1000
assert len(fig.hf_data) == 1
Expand Down Expand Up @@ -201,4 +205,3 @@ def test_compasibility_when_registered(registering_cleanup):
assert len(f.data[0].y) == 1000
assert len(f.hf_data) == 1
assert len(f.hf_data[0]["y"]) == 1005

Loading