-
Notifications
You must be signed in to change notification settings - Fork 76
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 aNotImplementedError
instead of saving toblah.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 toblah.jpeg.png
.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 🤷
There was a problem hiding this comment.
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. 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me
There was a problem hiding this comment.
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
)There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.