From a1956900b038c9df83b589ef896606d5ec90f18d Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Fri, 25 Oct 2024 16:38:06 -0400 Subject: [PATCH 1/2] Rename limits_cache to _limits_cache for JSON serializability. --- glue_jupyter/common/state3d.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/glue_jupyter/common/state3d.py b/glue_jupyter/common/state3d.py index 74997214..8f850d8c 100644 --- a/glue_jupyter/common/state3d.py +++ b/glue_jupyter/common/state3d.py @@ -34,7 +34,7 @@ class ViewerState3D(ViewerState): # clip_data = CallbackProperty(False) native_aspect = CallbackProperty(False) - limits_cache = CallbackProperty() + _limits_cache = CallbackProperty() # def _update_priority(self, name): # if name == 'layers': @@ -48,34 +48,34 @@ def __init__(self, **kwargs): super(ViewerState3D, self).__init__(**kwargs) - if self.limits_cache is None: - self.limits_cache = {} + if self._limits_cache is None: + self._limits_cache = {} self.x_lim_helper = StateAttributeLimitsHelper(self, attribute='x_att', lower='x_min', upper='x_max', - cache=self.limits_cache) + cache=self._limits_cache) self.y_lim_helper = StateAttributeLimitsHelper(self, attribute='y_att', lower='y_min', upper='y_max', - cache=self.limits_cache) + cache=self._limits_cache) self.z_lim_helper = StateAttributeLimitsHelper(self, attribute='z_att', lower='z_min', upper='z_max', - cache=self.limits_cache) + cache=self._limits_cache) - # TODO: if limits_cache is re-assigned to a different object, we need to - # update the attribute helpers. However if in future we make limits_cache + # TODO: if _limits_cache is re-assigned to a different object, we need to + # update the attribute helpers. However if in future we make _limits_cache # into a smart dictionary that can call callbacks when elements are # changed then we shouldn't always call this. It'd also be nice to # avoid this altogether and make it more clean. - self.add_callback('limits_cache', nonpartial(self._update_limits_cache)) + self.add_callback('_limits_cache', nonpartial(self._update_limits_cache)) def _update_limits_cache(self): - self.x_lim_helper._cache = self.limits_cache + self.x_lim_helper._cache = self._limits_cache self.x_lim_helper._update_attribute() - self.y_lim_helper._cache = self.limits_cache + self.y_lim_helper._cache = self._limits_cache self.y_lim_helper._update_attribute() - self.z_lim_helper._cache = self.limits_cache + self.z_lim_helper._cache = self._limits_cache self.z_lim_helper._update_attribute() @property From ee9391635853367ffe19261ca6a52926d8cd5291 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Thu, 31 Oct 2024 18:30:01 -0400 Subject: [PATCH 2/2] Add test of 3D viewer state JSON serializability. --- glue_jupyter/common/tests/__init__.py | 0 glue_jupyter/common/tests/test_state3d.py | 35 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 glue_jupyter/common/tests/__init__.py create mode 100644 glue_jupyter/common/tests/test_state3d.py diff --git a/glue_jupyter/common/tests/__init__.py b/glue_jupyter/common/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/glue_jupyter/common/tests/test_state3d.py b/glue_jupyter/common/tests/test_state3d.py new file mode 100644 index 00000000..2c32f660 --- /dev/null +++ b/glue_jupyter/common/tests/test_state3d.py @@ -0,0 +1,35 @@ +import traitlets + +from glue_jupyter.state_traitlets_helpers import GlueState +from glue_jupyter.common.state3d import ViewerState3D + + +class Widget(traitlets.HasTraits): + + state = GlueState() + + latest_json = None + + # The following two methods mimic the behavior of ipywidgets + + @traitlets.observe('state') + def on_state_change(self, change): + to_json = self.trait_metadata('state', 'to_json') + self.latest_json = to_json(self.state, self) + + def set_state_from_json(self, json): + from_json = self.trait_metadata('state', 'from_json') + from_json(json, self) + + +def test_json_serializable(): + widget = Widget() + assert widget.latest_json is None + widget.state = ViewerState3D() + assert widget.latest_json == { + "x_att": None, "x_min": 0, "x_max": 1, + "y_att": None, "y_min": 0, "y_max": 1, + "z_att": None, "z_min": 0, "z_max": 1, + "visible_axes": True, "native_aspect": False, + "layers": [], "title": None, + }