Skip to content

Commit

Permalink
filter out empty tables
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Mar 19, 2024
1 parent cc7679c commit 7886ba3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def test_markers_cubeviz(cubeviz_helper, spectrum1d_cube):

mp = cubeviz_helper.plugins['Markers']
mp.keep_active = True
exp = cubeviz_helper.plugins['Export']

# no marks yet, so table does not yet appear in export plugin
assert "Markers:table" not in exp.table.choices

# test event in flux viewer
label_mouseover._viewer_mouse_event(fv,
Expand Down Expand Up @@ -125,7 +129,6 @@ def test_markers_cubeviz(cubeviz_helper, spectrum1d_cube):
assert len(_get_markers_from_viewer(sv).x) == 2

# appears as option in export plugin and exports successfully
exp = cubeviz_helper.plugins['Export']
assert "Markers:table" in exp.table.choices
exp.table = "Markers:table"
exp.export()
Expand Down
16 changes: 15 additions & 1 deletion jdaviz/core/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'AstrowidgetMarkersChangedMessage', 'MarkersPluginUpdate',
'CanvasRotationChangedMessage',
'GlobalDisplayUnitChanged', 'ChangeRefDataMessage',
'PluginTableAddedMessage']
'PluginTableAddedMessage', 'PluginTableModifiedMessage']


class NewViewerMessage(Message):
Expand Down Expand Up @@ -434,3 +434,17 @@ def table(self):
@property
def plugin(self):
return self.sender._plugin


class PluginTableModifiedMessage(Message):
'''Message generated when the items in a plugin table are changed'''
def __init__(self, sender):
super().__init__(sender)

@property
def table(self):
return self.sender

@property
def plugin(self):
return self.sender._plugin
16 changes: 13 additions & 3 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
ViewerAddedMessage, ViewerRemovedMessage,
ViewerRenamedMessage, SnackbarMessage,
AddDataToViewerMessage, ChangeRefDataMessage,
PluginTableAddedMessage)
PluginTableAddedMessage, PluginTableModifiedMessage)

from jdaviz.core.marks import (LineAnalysisContinuum,
LineAnalysisContinuumCenter,
Expand Down Expand Up @@ -2385,7 +2385,7 @@ class PluginTableSelect(SelectPluginComponent):

def __init__(self, plugin, items, selected,
multiselect=None,
filters=[],
filters=['not_empty_table'],
default_text=None, manual_options=[],
default_mode='first'):
"""
Expand Down Expand Up @@ -2417,12 +2417,14 @@ def __init__(self, plugin, items, selected,
default_text=default_text, manual_options=manual_options,
default_mode=default_mode)
self.hub.subscribe(self, PluginTableAddedMessage, handler=self._on_tables_changed)
self.hub.subscribe(self, PluginTableModifiedMessage, handler=self._on_tables_changed)
self._on_tables_changed()

@observe('filters')
def _on_tables_changed(self, *args):
manual_items = [{'label': label} for label in self.manual_options]
self.items = manual_items + [{'label': k} for k in self.plugin.app._plugin_tables.keys()]
self.items = manual_items + [{'label': k} for k, v in self.plugin.app._plugin_tables.items()
if self._is_valid_item(v._obj)]
self._apply_default_selection()
# future improvement: only clear cache if the selected data entry was changed?
self._clear_cache(*self._cached_properties)
Expand All @@ -2431,6 +2433,12 @@ def _on_tables_changed(self, *args):
def selected_obj(self):
return self.plugin.app._jdaviz_helper.plugin_tables.get(self.selected)

def _is_valid_item(self, table):
def not_empty_table(table):
return len(table.items) > 0

return super()._is_valid_item(table, locals())


class PluginTableSelectMixin(VuetifyTemplate, HubListener):
table_items = List().tag(sync=True)
Expand Down Expand Up @@ -4189,6 +4197,7 @@ def float_precision(column, item):

# clean data to show in the UI
self.items = self.items + [{k: json_safe(k, v) for k, v in item.items()}]
self._plugin.session.hub.broadcast(PluginTableAddedMessage(sender=self))

def __len__(self):
return len(self.items)
Expand All @@ -4199,6 +4208,7 @@ def clear_table(self):
"""
self.items = []
self._qtable = None
self._plugin.session.hub.broadcast(PluginTableModifiedMessage(sender=self))

def vue_clear_table(self, data=None):
# if the plugin (or via the TableMixin) has its own clear_table implementation,
Expand Down

0 comments on commit 7886ba3

Please sign in to comment.