From 38ca560b9c5ef46f59f3adcdd1cba9f181be4218 Mon Sep 17 00:00:00 2001 From: "Pey Lian Lim (Github)" <2090236+pllim@users.noreply.github.com> Date: Fri, 30 Sep 2022 14:38:57 -0400 Subject: [PATCH] Disable Line Lists when there is no data --- CHANGES.rst | 3 +++ .../configs/default/plugins/line_lists/line_lists.py | 10 ++++++++++ .../plugins/line_lists/tests/test_line_lists.py | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 77a4e87b54..9b9026b8c8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -114,6 +114,9 @@ Mosviz Specviz ^^^^^^^ +- Line Lists plugin now disabled if no data is loaded instead of letting user + load a list list and crash. [#1691] + Specviz2d ^^^^^^^^^ diff --git a/jdaviz/configs/default/plugins/line_lists/line_lists.py b/jdaviz/configs/default/plugins/line_lists/line_lists.py index f1c3341163..550e750ce7 100644 --- a/jdaviz/configs/default/plugins/line_lists/line_lists.py +++ b/jdaviz/configs/default/plugins/line_lists/line_lists.py @@ -114,6 +114,14 @@ def __init__(self, *args, **kwargs): self._viewer.state.add_callback("x_max", lambda x_max: self._on_spectrum_viewer_limits_changed()) + self._disable_if_no_data() + + def _disable_if_no_data(self): + if len(self.app.data_collection) == 0: + self.disabled_msg = 'Line Lists unavailable when no data is loaded' + else: + self.disabled_msg = '' + def _on_viewer_data_changed(self, msg=None): """ Callback method for when data is added or removed from a viewer, or @@ -131,6 +139,8 @@ def _on_viewer_data_changed(self, msg=None): msg : `glue.core.Message` The glue message passed to this callback method. """ + self._disable_if_no_data() + self._viewer_id = self.app._viewer_item_by_reference( 'spectrum-viewer').get('id') diff --git a/jdaviz/configs/default/plugins/line_lists/tests/test_line_lists.py b/jdaviz/configs/default/plugins/line_lists/tests/test_line_lists.py index 560861beb7..e15ece58de 100644 --- a/jdaviz/configs/default/plugins/line_lists/tests/test_line_lists.py +++ b/jdaviz/configs/default/plugins/line_lists/tests/test_line_lists.py @@ -45,9 +45,15 @@ def test_line_lists(specviz_helper): def test_redshift(specviz_helper, spectrum1d): + # Also test that plugin is disabled before data is loaded. + plg = specviz_helper.plugins['Line Lists'] + assert plg._obj.disabled_msg + label = "Test 1D Spectrum" specviz_helper.load_spectrum(spectrum1d, data_label=label) + assert not plg._obj.disabled_msg + lt = QTable() lt['linename'] = ['O III', 'Halpha'] lt['rest'] = [5007, 6563]*u.AA