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

Conversation

jkochNU
Copy link

@jkochNU jkochNU commented Mar 27, 2022

As discussed with @ianhi, this is an attempt to (partially) address #138.
This would add an additional button to the toolbar associated with a figure.canvas. Once clicked, a pdf is generated with savefig and then handed over to the frontend inside a new tab.

image

@github-actions
Copy link
Contributor

Binder 👈 Launch a binder notebook on branch jkochNU/ipympl/toolbar_pdf_button

Copy link
Collaborator

@ianhi ianhi left a comment

Choose a reason for hiding this comment

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

This is a good start! Glad that you managed all the interconnections between python and typescript.

That said I don't think that the correct solution is to limit ourselves to pdfs here. Instead we could have the button mean "Save according to rcparams" and a second button (probably with a different icon) that would do the current behavior of downloading.

For now just adding a new button for saving with rcparams is a good step I think. inline comments should show how to accomplish that.

Comment on lines +460 to +461
self.figure.savefig(buf, format='pdf', dpi='figure')
self.send({'data': '{"type": "makepdf"}'}, buffers=[buf.getbuffer()])
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()])

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

Comment on lines +164 to +165
const image_url = url_creator.createObjectURL(blob);
window.open(image_url, '_blank');
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

@ianhi
Copy link
Collaborator

ianhi commented Mar 28, 2022

@jkochNU heads up that the pre-commit bot push a commit with styling changes. You can either git pull or on your next push do git push -fto override that commit (but then it will happen again to auto style things). I'd also recommend installing it locally with pre-commit install

@jkochNU
Copy link
Author

jkochNU commented Mar 28, 2022

That said I don't think that the correct solution is to limit ourselves to pdfs here. Instead we could have the button mean "Save according to rcparams" and a second button (probably with a different icon) that would do the current behavior of downloading.

@ianhi
Before I make changes in that direction: in my mind, a more flexible solution should do the following: upon clicking the new button, open a native file dialog box. There, the export file type could default to the one in rcParams, but the file type dropdown should allow for a variety of choices supported by savefig. I've looked into this a bit more, and it is less clear to me now that this is as simple as I was hoping for.

Regarding your suggested solution: we should keep in mind that rcParams already defaults to png format. So unless the user makes an explicit change, displaying a separate button saving to the rcParams format will typically just result in yet another button saving to png. We could make the presence of the additional button depend on whether the rcParams format differs from png. The button would remain missing, though, if the rcParams format was changed midway...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants