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

button for canvas toolbar which enables creating a pdf of the figure #443

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion ipympl/backend_nbagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ def _default_toolitems(self):
'move': 'arrows',
'download': 'floppy-o',
'export': 'file-picture-o',
'make_pdf': 'file-pdf',
}

download_item = ('Download', 'Download plot', 'download', 'save_figure')
make_pdf = ('Create pdf', 'Create pdf from figure', 'make_pdf', 'makepdf')

toolitems = NavigationToolbar2.toolitems + (download_item,)
toolitems = NavigationToolbar2.toolitems + (download_item,) + (make_pdf,)

return [
(text, tooltip, icons[icon_name], method_name)
Expand Down Expand Up @@ -275,6 +277,9 @@ def _handle_message(self, object, content, buffers):
Canvas.current_dpi_ratio = content['dpi_ratio']
self.manager.handle_json(content)

elif content['type'] == 'toolbar_button' and content['name'] == 'makepdf':
self._send_savefig_pdf()

else:
self.manager.handle_json(content)

Expand Down Expand Up @@ -450,6 +455,11 @@ def handle_key(key):

handle_key_press = handle_key_release = _handle_key

def _send_savefig_pdf(self):
buf = io.BytesIO()
self.figure.savefig(buf, format='pdf', dpi='figure')
self.send({'data': '{"type": "makepdf"}'}, buffers=[buf.getbuffer()])
Comment on lines +460 to +461
Copy link
Collaborator

Choose a reason for hiding this comment

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

Changing this a little bit will allow generalizing to more than just PDFs.

  1. Remove the arguments to savefig so that it follows the rcparams
  2. check the rcparams format and use that to construct the mimetype
  3. send that mimetype info along to the frontend
Suggested change
self.figure.savefig(buf, format='pdf', dpi='figure')
self.send({'data': '{"type": "makepdf"}'}, buffers=[buf.getbuffer()])
self.figure.savefig(buf)
format = matplotlib.rcParams['savefig.format']
# some code intepreting the mimetype
# e..g application/pdf, image/svg+xml, image/png etc
self.send({'data': {"type": "download-fig", "format":format, "mimetype":mimetype}}, buffers=[buf.getbuffer()])



class FigureManager(FigureManagerWebAgg):
if matplotlib.__version__ < "3.6":
Expand Down
8 changes: 8 additions & 0 deletions src/mpl_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ export class MPLCanvasModel extends DOMWidgetModel {
document.body.removeChild(save);
}

handle_makepdf(msg: any, dataviews: any) {
const url_creator = window.URL || window.webkitURL;
const buffer = new Uint8Array(dataviews[0].buffer);
const blob = new Blob([buffer], { type: 'application/pdf' });
Copy link
Collaborator

Choose a reason for hiding this comment

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

With other comment extract the mimetype from the msg

const image_url = url_creator.createObjectURL(blob);
window.open(image_url, '_blank');
Comment on lines +164 to +165
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm pretty sure that we need to revoke the object url after downloading

}

handle_resize(msg: { [index: string]: any }) {
this.resize_canvas();
this.offscreen_context.drawImage(this.image, 0, 0);
Expand Down