Skip to content

Commit e60a5dd

Browse files
committed
put download scripts into a function
1 parent 6ea4e6d commit e60a5dd

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

plotly/offline/offline.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,37 @@ def get_plotlyjs():
4848
plotlyjs = resource_string('plotly', path).decode('utf-8')
4949
return plotlyjs
5050

51+
def image_download_script(type):
52+
53+
if type == 'iplot':
54+
check_start = 'if(document.readyState == \'complete\') {{'
55+
check_end = '}}'
56+
elif type == 'plot':
57+
check_start = ''
58+
check_end = ''
59+
60+
return(
61+
('<script>'
62+
'function downloadimage(format, height, width,'
63+
' filename) {{'
64+
'var p = document.getElementById(\'{plot_id}\');'
65+
'Plotly.downloadImage(p, {{format: format, height: height, '
66+
'width: width, filename: filename}});'
67+
'}};' +
68+
check_start +
69+
'if(confirm(\'Do you want to save this image as '
70+
'{filename}.{format}?\\n\\n'
71+
'For higher resolution images and more export options, '
72+
'consider making requests to our image servers. Type: '
73+
'help(py.image) for more details.'
74+
'\')) {{'
75+
'downloadimage(\'{format}\', {height}, {width}, '
76+
'\'{filename}\');}}' +
77+
check_end +
78+
'</script>'
79+
)
80+
)
81+
5182

5283
def init_notebook_mode(connected=False):
5384
"""
@@ -269,14 +300,23 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
269300
'}};'
270301
'if(document.readyState == \'complete\') {{'
271302
'if(confirm(\'Do you want to save this image as '
272-
'{filename}.{format}?\')) {{'
303+
'{filename}.{format}?\\n\\n\\n\\n'
304+
'For higher resolution images and more export options, '
305+
'consider making requests to our image servers. Type: '
306+
'help(py.image) for more details.'
307+
'\')) {{'
273308
'downloadimage(\'{format}\', {height}, {width}, '
274309
'\'{filename}\');}}'
275310
'}}'
276311
'</script>'
277312
).format(format=image, width=image_width,
278313
height=image_height, filename=filename,
279314
plot_id=plotdivid)
315+
script = image_download_script('iplot').format(format=image,
316+
width=image_width,
317+
height=image_height,
318+
filename=filename,
319+
plot_id=plotdivid)
280320
# allow time for the plot to draw
281321
import time
282322
time.sleep(1)
@@ -412,6 +452,13 @@ def plot(figure_or_data,
412452
).format(format=image, width=image_width,
413453
height=image_height,
414454
filename=image_filename, plot_id=plotdivid)
455+
456+
script = image_download_script('plot')
457+
script = script.format(format=image,
458+
width=image_width,
459+
height=image_height,
460+
filename=image_filename,
461+
plot_id=plotdivid)
415462
else:
416463
script = ''
417464

0 commit comments

Comments
 (0)