Skip to content

Commit d2b9880

Browse files
committed
added option to download an image of the most recent plot
1 parent ef9f4ba commit d2b9880

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

plotly/offline/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
iplot,
1111
iplot_mpl,
1212
plot,
13-
plot_mpl
13+
plot_mpl,
14+
downloadImage,
1415
)

plotly/offline/offline.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,39 @@ def enable_mpl_offline(resize=False, strip_style=False,
508508
formatter.for_type(matplotlib.figure.Figure,
509509
lambda fig: iplot_mpl(fig, resize, strip_style, verbose,
510510
show_link, link_text, validate))
511+
512+
def downloadImage(format='png', height=600, width=800,
513+
filename='newplot'):
514+
515+
script = ('<script>'
516+
'function downloadimage(format, width, height,'
517+
' filename) {{'
518+
'var elementsList = document.querySelectorAll(\'.code_cell\');'
519+
'var selected = null;'
520+
'var new_list = new Array();'
521+
'for(var i=0; i < elementsList.length; i++) {{'
522+
'if(elementsList[i].classList.contains(\'selected\')) {{'
523+
'selected = i;'
524+
'break;'
525+
'}};'
526+
'var temp = elementsList[i].getElementsByClassName'
527+
'(\'plot-container plotly\');'
528+
'if (temp.length > 0) {{'
529+
'new_list.push(elementsList[i].getElementsByClassName'
530+
'(\'plot-container plotly\')[0]);'
531+
'}}'
532+
'}}'
533+
'if (new_list.length>0) {{'
534+
'var pre_div = new_list.slice(-1)[0];'
535+
'var p = document.getElementById(pre_div.parentElement.id);'
536+
'Plotly.downloadImage(p, {{format: format, height: height, '
537+
'width: width, filename: `filename}});'
538+
'}}'
539+
'}}'
540+
'downloadimage(\'{format}\', {height}, {width}, \'{filename}\');'
541+
'alert(\'ran\');'
542+
'</script>'
543+
).format(format=format, height=height, width=width,
544+
filename=filename)
545+
546+
display(HTML(script))

0 commit comments

Comments
 (0)