Skip to content

Commit

Permalink
Merge pull request #81 from Carifio24/unique-class
Browse files Browse the repository at this point in the history
Add unique class to each viewer instance
  • Loading branch information
Carifio24 authored Jul 17, 2024
2 parents cd51f67 + 732d682 commit 7b53729
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions glue_plotly/viewers/common/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .base_viewer_tests import BasePlotlyViewTests # noqa
22 changes: 22 additions & 0 deletions glue_plotly/viewers/common/tests/base_viewer_tests.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions glue_plotly/viewers/common/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion glue_plotly/viewers/histogram/tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
5 changes: 4 additions & 1 deletion glue_plotly/viewers/scatter/tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 7b53729

Please sign in to comment.