From e3599781f49b8ce9611ff51649d294880c18114b Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 7 Sep 2022 09:18:03 -0400 Subject: [PATCH 1/4] user API for export plot plugin --- .../plugins/export_plot/export_plot.py | 47 +++++++++++++++++-- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/jdaviz/configs/default/plugins/export_plot/export_plot.py b/jdaviz/configs/default/plugins/export_plot/export_plot.py index b8fd8a960f..2dfab757f8 100644 --- a/jdaviz/configs/default/plugins/export_plot/export_plot.py +++ b/jdaviz/configs/default/plugins/export_plot/export_plot.py @@ -1,23 +1,60 @@ from jdaviz.core.registries import tray_registry from jdaviz.core.template_mixin import PluginTemplateMixin, ViewerSelectMixin +from jdaviz.core.user_api import PluginUserApi __all__ = ['ExportViewer'] @tray_registry('g-export-plot', label="Export Plot") class ExportViewer(PluginTemplateMixin, ViewerSelectMixin): + """ + See the :ref:`Export Plot Plugin Documentation ` for more details. + + Only the following attributes and methods are available through the + :ref:`public plugin API `: + + * :meth:`~jdaviz.core.template_mixin.PluginTemplateMixin.show` + * :meth:`~jdaviz.core.template_mixin.PluginTemplateMixin.open_in_tray` + * :meth:`save_figure` + """ template_file = __file__, "export_plot.vue" + @property + def user_api(self): + return PluginUserApi(self, expose=('save_figure',)) + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - def vue_save_figure(self, filetype): + def save_figure(self, filename=None, filetype=None): """ - Callback for save figure events in the front end viewer toolbars. Uses - the bqplot.Figure save methods. + Save the figure to an image with a provided filename or through an interactive save dialog. + + Parameters + ---------- + filename : str or None + Filename to autopopulate the save dialog. + filetype : str or None + Filetype (png or svg). If None, will default based on ``filename`` or to 'png'. """ + if filetype is None: + if filename is not None: + filetype = filename.split('.')[-1] + else: + # default to png + filetype = 'png' + viewer = self.viewer.selected_obj if filetype == "png": - viewer.figure.save_png() + viewer.figure.save_png(filename) elif filetype == "svg": - viewer.figure.save_svg() + viewer.figure.save_svg(filename) + else: + raise NotImplementedError(f"filetype={filetype} not supported") + + def vue_save_figure(self, filetype): + """ + Callback for save figure events in the front end viewer toolbars. Uses + the bqplot.Figure save methods. + """ + self.save_figure(filetype=filetype) From 3e98e5f6befe8fd84ac27c342350df1fdeb71f66 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Fri, 30 Sep 2022 10:06:57 -0400 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: P. L. Lim <2090236+pllim@users.noreply.github.com> --- jdaviz/configs/default/plugins/export_plot/export_plot.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/jdaviz/configs/default/plugins/export_plot/export_plot.py b/jdaviz/configs/default/plugins/export_plot/export_plot.py index 2dfab757f8..1bc1b10245 100644 --- a/jdaviz/configs/default/plugins/export_plot/export_plot.py +++ b/jdaviz/configs/default/plugins/export_plot/export_plot.py @@ -32,10 +32,11 @@ def save_figure(self, filename=None, filetype=None): Parameters ---------- - filename : str or None + filename : str or `None` Filename to autopopulate the save dialog. - filetype : str or None - Filetype (png or svg). If None, will default based on ``filename`` or to 'png'. + filetype : {'png', 'svg', `None`} + Filetype (PNG or SVG). If `None`, will default based on filename or to PNG. + """ if filetype is None: if filename is not None: From 023b18dbfe0a67742c98540b050cc65e14021b82 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Fri, 30 Sep 2022 11:15:11 -0400 Subject: [PATCH 3/4] default to png when no extension provided in filename --- jdaviz/configs/default/plugins/export_plot/export_plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jdaviz/configs/default/plugins/export_plot/export_plot.py b/jdaviz/configs/default/plugins/export_plot/export_plot.py index 1bc1b10245..b4dc25f0b3 100644 --- a/jdaviz/configs/default/plugins/export_plot/export_plot.py +++ b/jdaviz/configs/default/plugins/export_plot/export_plot.py @@ -39,7 +39,7 @@ def save_figure(self, filename=None, filetype=None): """ if filetype is None: - if filename is not None: + if filename is not None and '.' in filename: filetype = filename.split('.')[-1] else: # default to png From d3c8b0b1aca7d492947ae65e577f394ab886d4c8 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Fri, 30 Sep 2022 11:35:01 -0400 Subject: [PATCH 4/4] update change log entry --- CHANGES.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index b910c3441a..c7530eaf48 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -12,9 +12,9 @@ New Features in Jy [#1564] - User-friendly API access to plugins, with exposed functionality for: line analysis, gaussian - smooth, moment maps, compass, collapse, metadata, slice, plot options, model fitting, and links - control. - [#1401, #1642, #1643, #1636, #1641, #1634, #1635, #1637, #1658, #1640] + smooth, moment maps, compass, collapse, metadata, slice, plot options, model fitting, links + control, and export plot. + [#1401, #1642, #1643, #1636, #1641, #1634, #1635, #1637, #1658, #1640, #1657] - Line Lists show which medium the catalog wavelengths were measured in, in accordance to the metadata entry. Lists without medium information