From 9a2215a3196f22eccabce8a7185d442e621747bb Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Tue, 30 Jan 2024 09:06:01 -0500 Subject: [PATCH] support creating new instances of plugins, independent of tray * move logic for assigning default viewer references from the app-method to the plugin itself, by searching the registry for a match * create "new" convenience method on plugin * allows for running results from a saved plugin state without altering the user-facing instance of the plugin --- jdaviz/app.py | 23 +---------------------- jdaviz/core/template_mixin.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/jdaviz/app.py b/jdaviz/app.py index a9e16eea2c..74fea17cc4 100644 --- a/jdaviz/app.py +++ b/jdaviz/app.py @@ -2509,29 +2509,8 @@ def compose_viewer_area(viewer_area_items): for name in config.get('tray', []): tray = tray_registry.members.get(name) - tray_registry_options = tray.get('viewer_reference_name_kwargs', {}) - - # Optional keyword arguments are required to initialize some - # tray items. These kwargs specify the viewer reference names that are - # assumed to be present in the configuration. - optional_tray_kwargs = dict() - - # If viewer reference names need to be passed to the tray item - # constructor, pass the names into the constructor in the format - # that the tray items expect. - for opt_attr, [opt_kwarg, get_name_kwargs] in tray_registry_options.items(): - opt_value = getattr( - self, opt_attr, self._get_first_viewer_reference_name(**get_name_kwargs) - ) - - if opt_value is None: - continue - - optional_tray_kwargs[opt_kwarg] = opt_value - tray_item_instance = tray.get('cls')( - app=self, **optional_tray_kwargs - ) + tray_item_instance = tray.get('cls')(app=self) # store a copy of the tray name in the instance so it can be accessed by the # plugin itself tray_item_label = tray.get('label') diff --git a/jdaviz/core/template_mixin.py b/jdaviz/core/template_mixin.py index f4ea62dd38..4aaa36b36e 100644 --- a/jdaviz/core/template_mixin.py +++ b/jdaviz/core/template_mixin.py @@ -52,6 +52,7 @@ from jdaviz.core.region_translators import regions2roi, _get_region_from_spatial_subset from jdaviz.core.tools import ICON_DIR from jdaviz.core.user_api import UserApiWrapper, PluginUserApi +from jdaviz.core.registries import tray_registry from jdaviz.style_registry import PopoutStyleWrapper from jdaviz.utils import get_subset_type, is_wcs_only, is_not_wcs_only, _wcs_only_label @@ -199,6 +200,9 @@ def __init__(self, *args, **kwargs): self.hub.subscribe(self, ViewerRemovedMessage, handler=lambda msg: self._remove_viewer_callbacks(msg.viewer_id)) + def new(self): + return self.__class__(app=self.app) + @property def app(self): """ @@ -356,7 +360,7 @@ class PluginTemplateMixin(TemplateMixin): previews_temp_disabled = Bool(False).tag(sync=True) # noqa use along-side @with_temp_disable() and previews_last_time = Float(0).tag(sync=True) - def __init__(self, **kwargs): + def __init__(self, app, **kwargs): self._viewer_callbacks = {} # _inactive_thread: thread checking for alive pings to control plugin_opened self._inactive_thread = None @@ -372,7 +376,27 @@ def __init__(self, **kwargs): # in repeated toggling of is_active. To use, decorate any method that observes traitlet # changes (including is_active) with @skip_if_no_updates_since_last_active() self._methods_skip_since_last_active = [] - super().__init__(**kwargs) + + # get default viewer names from the helper, according to the requirements of the plugin + for registry_name, tray_item in tray_registry.members.items(): + if tray_item['cls'] == self.__class__: + # If viewer reference names need to be passed to the tray item + # constructor, pass the names into the constructor in the format + # that the tray items expect. + tray_registry_options = tray_item.get('viewer_reference_name_kwargs', {}) + for opt_attr, [opt_kwarg, get_name_kwargs] in tray_registry_options.items(): + opt_value = getattr( + self, opt_attr, app._get_first_viewer_reference_name(**get_name_kwargs) + ) + + if opt_value is None: + continue + + kwargs.setdefault(opt_kwarg, opt_value) + + break + + super().__init__(app=app, **kwargs) @property def user_api(self):