Skip to content

Commit

Permalink
user API for export plot plugin (#1657)
Browse files Browse the repository at this point in the history
* user API for export plot plugin
* default to png when no extension provided in filename
  • Loading branch information
kecnry authored Sep 30, 2022
1 parent 3ede2fd commit 033ea81
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
6 changes: 3 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 43 additions & 5 deletions jdaviz/configs/default/plugins/export_plot/export_plot.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
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 <imviz-export-plot>` for more details.
Only the following attributes and methods are available through the
:ref:`public plugin API <plugin-apis>`:
* :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 : {'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 and '.' in filename:
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)

0 comments on commit 033ea81

Please sign in to comment.