Skip to content

Commit

Permalink
prevent clearing plot selection
Browse files Browse the repository at this point in the history
* when the plot was being updated, a plot update message was being sent when clearing the old data, briefly resetting the choices to an empty list, which cleared the selection.  The broadcast now is not duplicated and so the choices remains intact
  • Loading branch information
kecnry committed Jul 9, 2024
1 parent f440064 commit b89689a
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ def _apply_default_selection(self, skip_if_current_valid=True):
self.selected = self._default_text if self._default_text else default_empty
else:
self.selected = default_empty
self._clear_cache(*self._cached_properties)

def _is_valid_item(self, item, filter_callables={}):
for valid_filter in self.filters:
Expand Down Expand Up @@ -2632,9 +2633,7 @@ def _on_tables_changed(self, *args):
manual_items = [{'label': label} for label in self.manual_options]
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)
self._apply_default_selection(skip_if_current_valid=True)

@cached_property
def selected_obj(self):
Expand Down Expand Up @@ -2747,16 +2746,14 @@ def _on_plots_changed(self, *args):
manual_items = [{'label': label} for label in self.manual_options]
self.items = manual_items + [{'label': k} for k, v in self.plugin.app._plugin_plots.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._apply_default_selection(skip_if_current_valid=True)
self._clear_cache(*self._cached_properties)

@cached_property
def selected_obj(self):
return self.plugin.app._jdaviz_helper.plugin_plots.get(self.selected)

def _is_valid_item(self, plot):

def not_empty_plot(plot):
# checks plot.figure.marks to determine if figure is of an empty plot
# not sure if this is a foolproof way to do this?
Expand Down Expand Up @@ -4717,7 +4714,6 @@ def __init__(self, plugin, name='plot', viewer_type='scatter', update_callback=N
self._initialize_toolbar()

plugin.session.hub.broadcast(PluginPlotAddedMessage(sender=self))
plugin.session.hub.broadcast(PluginPlotModifiedMessage(sender=self))

def _initialize_toolbar(self, default_tool_priority=[]):
self.toolbar = NestedJupyterToolbar(self.viewer, self.tools_nested, default_tool_priority)
Expand All @@ -4743,12 +4739,13 @@ def _check_valid_components(self, **kwargs):
# https://github.com/astrofrog/fast-histogram/issues/60
raise ValueError("histogram requires data entries with length > 1")

def _remove_data(self, label):
def _remove_data(self, label, broadcast=True):
dc_entry = self.app.data_collection[label]
self.viewer.remove_data(dc_entry)
self.app.data_collection.remove(dc_entry)

self._plugin.session.hub.broadcast(PluginPlotModifiedMessage(sender=self))
if broadcast:
self._plugin.session.hub.broadcast(PluginPlotModifiedMessage(sender=self))

Check warning on line 4748 in jdaviz/core/template_mixin.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/core/template_mixin.py#L4748

Added line #L4748 was not covered by tests

def _update(self):
# call the update callback, if it exists, on the parent plugin.
Expand Down Expand Up @@ -4782,8 +4779,8 @@ def _update_data(self, label, reset_lims=False, **kwargs):
style_state = self.layers[label].state.as_dict()
else:
style_state = {}
self._remove_data(label)
self._add_data(label, **kwargs)
self._remove_data(label, broadcast=False)
self._add_data(label, broadcast=False, **kwargs)
self.update_style(label, **style_state)
if reset_lims:
self.viewer.state.reset_limits()
Expand Down Expand Up @@ -4819,7 +4816,7 @@ def update_style(self, label, **kwargs):

self._plugin.session.hub.broadcast(PluginPlotModifiedMessage(sender=self))

def _add_data(self, label, **kwargs):
def _add_data(self, label, broadcast=True, **kwargs):
self._check_valid_components(**kwargs)
data = Data(label=label, **kwargs)
dc = self.app.data_collection
Expand All @@ -4834,7 +4831,8 @@ def _add_data(self, label, **kwargs):
dc.add_link(links)
self.viewer.add_data(dc_entry)

self._plugin.session.hub.broadcast(PluginPlotModifiedMessage(sender=self))
if broadcast:
self._plugin.session.hub.broadcast(PluginPlotModifiedMessage(sender=self))

def _refresh_marks(self):
# ensure all marks are drawn
Expand Down

0 comments on commit b89689a

Please # to comment.