Skip to content

Commit ad728a3

Browse files
committed
fixed the download on refresh
1 parent 8eae6bd commit ad728a3

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

plotly/offline/offline.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -238,31 +238,27 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
238238

239239
display(HTML(plot_html))
240240

241-
# Use the plot id to download the image now:
242-
243-
script = ('<script id=\'download-script-{plot_id}\'>'
244-
'var completed = document.getElementById(\'downloaded-plot-{plot_id}\');'
245-
'alert(completed);'
246-
'if(!completed) {{'
247-
'console.log(completed);'
248-
'var div_id = document.getElementById(\'{plot_id}\');'
249-
'setTimeout(function() {{Plotly.downloadImage(div_id, {{format: \'{format}\', '
250-
'height: {height}, width: {width}, filename: \'{filename}\'}})'
251-
'.then(function(){{'
252-
'var complete = document.createElement(\'span\');'
253-
'complete.id = \'downloaded-plot-{plot_id}\';'
254-
'var thisScript = document.getElementById(\'download-script-{plot_id}\');'
255-
'console.log(complete);'
256-
'thisScript.parentNode.insertBefore(complete, thisScript.nextSibling);'
257-
'}})'
258-
';}}, 500);'
241+
# Write script to download image of the plot
242+
script = ('<script>'
243+
'function downloadimage(format, height, width,'
244+
' filename) {{'
245+
'var p = document.getElementById(\'{plot_id}\');'
246+
'Plotly.downloadImage(p, {{format: format, height: height, '
247+
'width: width, filename: filename}});'
248+
'}};'
249+
'if(document.readyState == \'complete\') {{'
250+
'downloadimage(\'{format}\', {height}, {width}, \'{filename}\');'
259251
'}}'
260252
'</script>'
261253
).format(format=format, width=_width, height=_height,
262254
filename=filename, plot_id=plot_id)
263-
255+
# allow time for the plot to draw
256+
import time
257+
time.sleep(1)
258+
#inject code to download an image of the plot
264259
display(HTML(script))
265260

261+
266262
def plot(figure_or_data,
267263
show_link=True, link_text='Export to plot.ly',
268264
validate=True, output_type='file',

0 commit comments

Comments
 (0)