1010import uuid
1111import warnings
1212from pkg_resources import resource_string
13+ import time
1314import webbrowser
1415
1516import plotly
@@ -48,14 +49,26 @@ def get_plotlyjs():
4849 plotlyjs = resource_string ('plotly' , path ).decode ('utf-8' )
4950 return plotlyjs
5051
51- def image_download_script (type ):
52+ def get_image_download_script (caller ):
53+ '''
54+ This function will return a script that will download an image of a Plotly
55+ plot.
5256
53- if type == 'iplot' :
57+ Keyword Arguments:
58+ caller ('plot', 'iplot') -- specifies which function made the call for the
59+ download script. If `iplot`, then an extra condition is added into the
60+ download script to ensure that download prompts aren't iniitated on
61+ page reloads.
62+ '''
63+
64+ if caller == 'iplot' :
5465 check_start = 'if(document.readyState == \' complete\' ) {{'
5566 check_end = '}}'
56- elif type == 'plot' :
67+ elif caller == 'plot' :
5768 check_start = ''
5869 check_end = ''
70+ else :
71+ raise ValueError ('caller should only be one of `iplot` or `plot`' )
5972
6073 return (
6174 ('<script>'
@@ -258,7 +271,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
258271 init_notebook_mode()
259272 iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
260273 # We can also download an image of the plot by setting the image to the
261- format you want ie: `image='png'`
274+ format you want. e.g. `image='png'`
262275 iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image='png')
263276 ```
264277 """
@@ -282,18 +295,20 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
282295
283296 if image :
284297 if image not in __IMAGE_FORMATS :
298+ img_n = len (__IMAGE_FORMATS )
285299 raise ValueError ('The image parameter takes only the '
286- 'following format types: `png`, `jpeg`, '
287- '`webp`, `svg`' )
288- # if the check passes then download script injection will commence.
289- # Write script to download image of the plot
300+ 'following format types: `{}`, `{}`, '
301+ '`{}`, `{}`' .format (* [__IMAGE_FORMATS [i ]
302+ for i in range (img_n )]
303+ )
304+ )
305+ # if image is given, and is a valid format, we will download the image
290306 script = image_download_script ('iplot' ).format (format = image ,
291307 width = image_width ,
292308 height = image_height ,
293309 filename = filename ,
294310 plot_id = plotdivid )
295311 # allow time for the plot to draw
296- import time
297312 time .sleep (1 )
298313 # inject code to download an image of the plot
299314 display (HTML (script ))
@@ -402,9 +417,13 @@ def plot(figure_or_data,
402417
403418 if image :
404419 if image not in __IMAGE_FORMATS :
420+ img_n = len (__IMAGE_FORMATS )
405421 raise ValueError ('The image parameter takes only the '
406- 'following format types: `png`, `jpeg`, '
407- '`webp`, `svg`' )
422+ 'following format types: `{}`, `{}`, '
423+ '`{}`, `{}`' .format (* [__IMAGE_FORMATS [i ]
424+ for i in range (img_n )]
425+ )
426+ )
408427 # if the check passes then download script is injected.
409428 # write the download script:
410429 script = image_download_script ('plot' )
0 commit comments