Skip to content

Commit 7b53729

Browse files
authored
Merge pull request #81 from Carifio24/unique-class
Add unique class to each viewer instance
2 parents cd51f67 + 732d682 commit 7b53729

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .base_viewer_tests import BasePlotlyViewTests # noqa
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from uuid import UUID
2+
3+
4+
class BasePlotlyViewTests:
5+
6+
def setup_method(self, method):
7+
pass
8+
9+
def teardown_method(self, method):
10+
pass
11+
12+
def test_unique_class(self):
13+
unique_class = self.viewer.unique_class
14+
prefix = "glue-plotly-"
15+
prefix_len = len(prefix)
16+
assert unique_class.startswith(prefix)
17+
assert len(unique_class) == prefix_len + 32
18+
19+
# Test UUID validity by seeing if we can construct a UUID instance
20+
assert UUID(unique_class[prefix_len:])
21+
22+
assert unique_class in self.viewer.figure._dom_classes

glue_plotly/viewers/common/viewer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def __init__(self, session, state=None):
3737
layout = self._create_layout_config()
3838
self.figure = go.FigureWidget(layout=layout)
3939

40+
self._unique_class = f"glue-plotly-{uuid4().hex}"
41+
self.figure.add_class(self._unique_class)
42+
4043
self.selection_layer_id = uuid4().hex
4144
selection_layer = go.Heatmap(x0=0.5,
4245
dx=1,
@@ -156,3 +159,8 @@ def apply_roi(self, roi, use_current=False):
156159
# TODO: Should we have anything here?
157160
def redraw(self):
158161
pass
162+
163+
@property
164+
def unique_class(self):
165+
"""This is a unique identifier, based on a v4 UUID, that is assigned to the root widget as a class."""
166+
return self._unique_class

glue_plotly/viewers/histogram/tests/test_viewer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from plotly.graph_objects import Bar
44

55
from glue_plotly.common import DEFAULT_FONT
6+
from glue_plotly.viewers.common.tests import BasePlotlyViewTests
67
from glue_plotly.viewers.histogram import PlotlyHistogramView
78

89

9-
class TestHistogramViewer:
10+
class TestHistogramViewer(BasePlotlyViewTests):
1011

1112
def setup_method(self, method):
1213
self.data = Data(label="histogram", x=[1, 1, 1, 2, 2, 3, 3, 3, 4, 6, 6])

glue_plotly/viewers/scatter/tests/test_viewer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
from plotly.graph_objects import Scatter
66

77
from glue_plotly.common import DEFAULT_FONT
8+
from glue_plotly.viewers.common.tests import BasePlotlyViewTests
89
from glue_plotly.viewers.scatter import PlotlyScatterView
910

1011

11-
class TestScatterView:
12+
class TestScatterView(BasePlotlyViewTests):
1213

1314
def setup_method(self, method):
15+
super().setup_method(method)
1416
self.data = Data(label="histogram", x=[1, 3, 5, 7, 9], y=[2, 4, 6, 8, 10])
1517
self.app = JupyterApplication()
1618
self.app.session.data_collection.append(self.data)
@@ -33,6 +35,7 @@ def setup_method(self, method):
3335
def teardown_method(self, method):
3436
self.viewer = None
3537
self.app = None
38+
super().teardown_method(method)
3639

3740
def test_basic(self):
3841
assert len(self.viewer.layers) == 1

0 commit comments

Comments
 (0)