-
Notifications
You must be signed in to change notification settings - Fork 226
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
base: main
Are you sure you want to change the base?
Conversation
for more information, see https://pre-commit.ci
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.
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.
self.figure.savefig(buf, format='pdf', dpi='figure') | ||
self.send({'data': '{"type": "makepdf"}'}, buffers=[buf.getbuffer()]) |
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.
Changing this a little bit will allow generalizing to more than just PDFs.
- Remove the arguments to savefig so that it follows the rcparams
- check the rcparams format and use that to construct the mimetype
- send that mimetype info along to the frontend
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' }); |
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.
With other comment extract the mimetype from the msg
const image_url = url_creator.createObjectURL(blob); | ||
window.open(image_url, '_blank'); |
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'm pretty sure that we need to revoke the object url after downloading
@jkochNU heads up that the pre-commit bot push a commit with styling changes. You can either |
@ianhi Regarding your suggested solution: we should keep in mind that |
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 withsavefig
and then handed over to the frontend inside a new tab.