@@ -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