Skip to content

Commit d04c73c

Browse files
committed
Merge pull request matplotlib#3974 from blink1073/nbagg-save
Add Save Tool to NbAgg Figure
2 parents 53c2708 + f170d4c commit d04c73c

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

lib/matplotlib/backends/backend_nbagg.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def connection_info():
9292
'forward': 'fa fa-arrow-right icon-arrow-right',
9393
'zoom_to_rect': 'fa fa-square-o icon-check-empty',
9494
'move': 'fa fa-arrows icon-move',
95+
'download': 'fa fa-icon-save icon-save',
9596
None: None
9697
}
9798

@@ -102,7 +103,8 @@ class NavigationIPy(NavigationToolbar2WebAgg):
102103
toolitems = [(text, tooltip_text,
103104
_FONT_AWESOME_CLASSES[image_file], name_of_method)
104105
for text, tooltip_text, image_file, name_of_method
105-
in NavigationToolbar2.toolitems
106+
in (NavigationToolbar2.toolitems +
107+
(('Download', 'Download plot', 'download', 'download'),))
106108
if image_file in _FONT_AWESOME_CLASSES]
107109

108110

lib/matplotlib/backends/backend_webagg_core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def release_zoom(self, event):
329329

330330
def save_figure(self, *args):
331331
"""Save the current figure"""
332-
warnings.warn('"Save figure" not implemented in Web Backend')
332+
self.canvas.send_event('save')
333333

334334

335335
class FigureManagerWebAgg(backend_bases.FigureManagerBase):

lib/matplotlib/backends/web_backend/mpl.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ mpl.figure.prototype.send_draw_message = function() {
283283
}
284284
}
285285

286+
287+
mpl.figure.prototype.handle_save = function(fig, msg) {
288+
var format_dropdown = fig.format_dropdown;
289+
var format = format_dropdown.options[format_dropdown.selectedIndex].value;
290+
fig.ondownload(fig, format);
291+
}
292+
293+
286294
mpl.figure.prototype.handle_resize = function(fig, msg) {
287295
var size = msg['size'];
288296
if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {
@@ -472,9 +480,7 @@ mpl.figure.prototype.key_event = function(event, name) {
472480

473481
mpl.figure.prototype.toolbar_button_onclick = function(name) {
474482
if (name == 'download') {
475-
var format_dropdown = this.format_dropdown;
476-
var format = format_dropdown.options[format_dropdown.selectedIndex].value;
477-
this.ondownload(this, format);
483+
this.handle_save(this, null);
478484
} else {
479485
this.send_message("toolbar_button", {name: name});
480486
}

lib/matplotlib/backends/web_backend/nbagg_mpl.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ mpl.mpl_figure_comm = function(comm, msg) {
3030
var element = $("#" + id);
3131
var ws_proxy = comm_websocket_adapter(comm)
3232

33+
function ondownload(figure, format) {
34+
window.open(figure.imageObj.src);
35+
}
36+
3337
var fig = new mpl.figure(id, ws_proxy,
34-
function() { },
38+
ondownload,
3539
element.get(0));
3640

3741
// Call onopen now - mpl needs it, as it is assuming we've passed it a real
@@ -142,6 +146,11 @@ mpl.figure.prototype._canvas_extra_style = function(el){
142146
}
143147

144148

149+
mpl.figure.prototype.handle_save = function(fig, msg) {
150+
fig.ondownload(fig, null);
151+
}
152+
153+
145154
mpl.find_output_cell = function(html_output) {
146155
// Return the cell and output element which can be found *uniquely* in the notebook.
147156
// Note - this is a bit hacky, but it is done because the "notebook_saving.Notebook"

0 commit comments

Comments
 (0)