Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user API for export plot plugin #1657

Merged
merged 4 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes people just give the name without suffix because they are used to other software attaching suffix for them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the code to auto populate the filename should be its own private function, so at least we can test that part. The only thing we cannot test is viewer.figure.save_png() call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fine, this logic should handle that scenario as expected, right? I guess the only problem would be if they did filename='blah.randomextension' which would raise a NotImplementedError instead of saving to blah.randomextension.png.

Should I instead only apply the filetype if its either png or svg and otherwise default to png? But then filename='blah.jpeg' would silently write to blah.jpeg.png.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent fallback means they still get the image, better than error from trying to save to an unsupported extension? Unless you want upstream to deal with that instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always prefer (helpful) errors than unexpected magic/fallbacks myself 🤷

Copy link
Contributor

@pllim pllim Sep 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I don't give file extension to Photoshop, it doesn't crash. It saves my stuff in some default format. I think users might be looking for similar established behaviors. 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (default to png when no extension is provided in the filename by checking for '.' in filename)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, would mimetypes be useful for your purpose? https://docs.python.org/3/library/mimetypes.html

In [10]: mimetypes.guess_type('dummy_gwcs.png')
Out[10]: ('image/png', None)

In [11]: mimetypes.guess_type("test_spec")
Out[11]: (None, None)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting idea. I think it might be overkill here since we're passing to one of two backend calls... but maybe if we generalize that in the future to avoid the file dialog, this would be a good direction to take.

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)