Skip to content

Commit

Permalink
support creating new instances of plugins, independent of tray
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kecnry committed Mar 11, 2024
1 parent 2e7d80c commit 9a2215a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
23 changes: 1 addition & 22 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
28 changes: 26 additions & 2 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -356,7 +360,7 @@ class PluginTemplateMixin(TemplateMixin):
previews_temp_disabled = Bool(False).tag(sync=True) # noqa use along-side @with_temp_disable() and <plugin-previews-temp-disabled :previews_temp_disabled.sync="previews_temp_disabled" :previews_last_time="previews_last_time" :show_live_preview.sync="show_live_preview"/>
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
Expand All @@ -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):
Expand Down

0 comments on commit 9a2215a

Please sign in to comment.