diff --git a/glue_plotly/viewers/common/tests/__init__.py b/glue_plotly/viewers/common/tests/__init__.py index e69de29..3033646 100644 --- a/glue_plotly/viewers/common/tests/__init__.py +++ b/glue_plotly/viewers/common/tests/__init__.py @@ -0,0 +1 @@ +from .base_viewer_tests import BasePlotlyViewTests # noqa diff --git a/glue_plotly/viewers/common/tests/base_viewer_tests.py b/glue_plotly/viewers/common/tests/base_viewer_tests.py new file mode 100644 index 0000000..3fd125c --- /dev/null +++ b/glue_plotly/viewers/common/tests/base_viewer_tests.py @@ -0,0 +1,22 @@ +from uuid import UUID + + +class BasePlotlyViewTests: + + def setup_method(self, method): + pass + + def teardown_method(self, method): + pass + + def test_unique_class(self): + unique_class = self.viewer.unique_class + prefix = "glue-plotly-" + prefix_len = len(prefix) + assert unique_class.startswith(prefix) + assert len(unique_class) == prefix_len + 32 + + # Test UUID validity by seeing if we can construct a UUID instance + assert UUID(unique_class[prefix_len:]) + + assert unique_class in self.viewer.figure._dom_classes diff --git a/glue_plotly/viewers/common/viewer.py b/glue_plotly/viewers/common/viewer.py index 8d2b12e..6e28012 100644 --- a/glue_plotly/viewers/common/viewer.py +++ b/glue_plotly/viewers/common/viewer.py @@ -37,6 +37,9 @@ def __init__(self, session, state=None): layout = self._create_layout_config() self.figure = go.FigureWidget(layout=layout) + self._unique_class = f"glue-plotly-{uuid4().hex}" + self.figure.add_class(self._unique_class) + self.selection_layer_id = uuid4().hex selection_layer = go.Heatmap(x0=0.5, dx=1, @@ -156,3 +159,8 @@ def apply_roi(self, roi, use_current=False): # TODO: Should we have anything here? def redraw(self): pass + + @property + def unique_class(self): + """This is a unique identifier, based on a v4 UUID, that is assigned to the root widget as a class.""" + return self._unique_class diff --git a/glue_plotly/viewers/histogram/tests/test_viewer.py b/glue_plotly/viewers/histogram/tests/test_viewer.py index c192689..0e6c6b9 100644 --- a/glue_plotly/viewers/histogram/tests/test_viewer.py +++ b/glue_plotly/viewers/histogram/tests/test_viewer.py @@ -3,10 +3,11 @@ from plotly.graph_objects import Bar from glue_plotly.common import DEFAULT_FONT +from glue_plotly.viewers.common.tests import BasePlotlyViewTests from glue_plotly.viewers.histogram import PlotlyHistogramView -class TestHistogramViewer: +class TestHistogramViewer(BasePlotlyViewTests): def setup_method(self, method): self.data = Data(label="histogram", x=[1, 1, 1, 2, 2, 3, 3, 3, 4, 6, 6]) diff --git a/glue_plotly/viewers/scatter/tests/test_viewer.py b/glue_plotly/viewers/scatter/tests/test_viewer.py index efe0b3d..0554022 100644 --- a/glue_plotly/viewers/scatter/tests/test_viewer.py +++ b/glue_plotly/viewers/scatter/tests/test_viewer.py @@ -5,12 +5,14 @@ from plotly.graph_objects import Scatter from glue_plotly.common import DEFAULT_FONT +from glue_plotly.viewers.common.tests import BasePlotlyViewTests from glue_plotly.viewers.scatter import PlotlyScatterView -class TestScatterView: +class TestScatterView(BasePlotlyViewTests): def setup_method(self, method): + super().setup_method(method) self.data = Data(label="histogram", x=[1, 3, 5, 7, 9], y=[2, 4, 6, 8, 10]) self.app = JupyterApplication() self.app.session.data_collection.append(self.data) @@ -33,6 +35,7 @@ def setup_method(self, method): def teardown_method(self, method): self.viewer = None self.app = None + super().teardown_method(method) def test_basic(self): assert len(self.viewer.layers) == 1