Skip to content

Commit

Permalink
support exporting a plot from an unopened/inactive plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Jul 2, 2024
1 parent c7efcad commit e52b891
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
14 changes: 9 additions & 5 deletions jdaviz/configs/default/plugins/export/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Export(PluginTemplateMixin, ViewerSelectMixin, SubsetSelectMixin,
dataset_format_items = List().tag(sync=True)
dataset_format_selected = Unicode().tag(sync=True)

plugin_plot_selected_widget = Unicode().tag(sync=True) # copy of widget of the selected plugin_plot in case the parent plugin is not opened
plugin_plot_format_items = List().tag(sync=True)
plugin_plot_format_selected = Unicode().tag(sync=True)

Expand Down Expand Up @@ -438,11 +439,14 @@ def export(self, filename=None, show_dialog=None, overwrite=False,
else:
filename = None

with plot._plugin.as_active():
# NOTE: could still take some time for the plot itself to update,
# for now we'll hardcode a short amount of time for the plot to render any updates
time.sleep(0.2)
self.save_figure(plot, filename, filetype, show_dialog=show_dialog)
if not plot._plugin.is_active:
# force an update to the plot. This requires the plot to have set update_callback when instantiated
plot._update()

# create a copy of the widget shown off screen to enable rendering in case one was never created in the parent plugin
self.plugin_plot_selected_widget = f'IPY_MODEL_{plot.model_id}'

self.save_figure(plot, filename, filetype, show_dialog=show_dialog)

elif len(self.plugin_table.selected):
filetype = self.plugin_table_format.selected
Expand Down
13 changes: 10 additions & 3 deletions jdaviz/configs/default/plugins/plot_options/plot_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ def state_attr_for_line_visible(state):
'stretch_params_value', 'stretch_params_sync',
state_filter=is_image)

self.stretch_histogram = Plot(self, name='stretch_hist', viewer_type='histogram')
self.stretch_histogram = Plot(self, name='stretch_hist', viewer_type='histogram',
update_callback=self._update_stretch_histogram)
# Add the stretch bounds tool to the default Plot viewer.
self.stretch_histogram.tools_nested.append(["jdaviz:stretch_bounds"])
self.stretch_histogram._initialize_toolbar(["jdaviz:stretch_bounds"])
Expand Down Expand Up @@ -886,8 +887,7 @@ def _update_stretch_hist_sync(self, msg={}):
@observe('is_active', 'layer_selected', 'viewer_selected',
'stretch_hist_zoom_limits')
@skip_if_no_updates_since_last_active()
@with_spinner('stretch_hist_spinner')
def _update_stretch_histogram(self, msg={}):
def _request_update_stretch_histogram(self, msg={}):
if not hasattr(self, 'viewer'): # pragma: no cover
# plugin hasn't been fully initialized yet
return
Expand All @@ -903,6 +903,13 @@ def _update_stretch_histogram(self, msg={}):
# its type
msg = {}

# NOTE: this method is separate from _update_stretch_histogram so that _update_stretch_histogram
# can be called manually (or from the update_callback on the Plot object itself) without going through
# the skip_if_no_updates_since_last_active check
self._update_stretch_histogram(msg)

@with_spinner('stretch_hist_spinner')
def _update_stretch_histogram(self, msg={}):
if not self.stretch_function_sync.get('in_subscribed_states'): # pragma: no cover
# no (image) viewer with stretch function options
return
Expand Down
9 changes: 8 additions & 1 deletion jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4685,12 +4685,14 @@ class Plot(PluginSubcomponent):
figure = Any().tag(sync=True, **widget_serialization)
toolbar = Any().tag(sync=True, **widget_serialization)

def __init__(self, plugin, name='plot', viewer_type='scatter', app=None, *args, **kwargs):
def __init__(self, plugin, name='plot', viewer_type='scatter', update_callback=None,
app=None, *args, **kwargs):
super().__init__(plugin, 'Plot', *args, **kwargs)
if app is None:
app = jglue()

self._app = app
self._update_callback = update_callback
self._plugin = plugin
self._plot_name = name
self.viewer = app.new_data_viewer(viewer_type, show=False)
Expand Down Expand Up @@ -4748,6 +4750,11 @@ def _remove_data(self, label):

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

def _update(self):
# call the update callback, if it exists, on the parent plugin. This is useful for updating the plot when a plugin is inactive
if self._update_callback is not None:
self._update_callback()

def _update_data(self, label, reset_lims=False, **kwargs):
self._check_valid_components(**kwargs)
if label not in self.app.data_collection:
Expand Down

0 comments on commit e52b891

Please sign in to comment.