10
10
import uuid
11
11
import warnings
12
12
from pkg_resources import resource_string
13
+ import time
13
14
import webbrowser
14
15
15
16
import plotly
@@ -48,14 +49,26 @@ def get_plotlyjs():
48
49
plotlyjs = resource_string ('plotly' , path ).decode ('utf-8' )
49
50
return plotlyjs
50
51
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.
52
56
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' :
54
65
check_start = 'if(document.readyState == \' complete\' ) {{'
55
66
check_end = '}}'
56
- elif type == 'plot' :
67
+ elif caller == 'plot' :
57
68
check_start = ''
58
69
check_end = ''
70
+ else :
71
+ raise ValueError ('caller should only be one of `iplot` or `plot`' )
59
72
60
73
return (
61
74
('<script>'
@@ -258,7 +271,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
258
271
init_notebook_mode()
259
272
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
260
273
# 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'`
262
275
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image='png')
263
276
```
264
277
"""
@@ -282,18 +295,20 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
282
295
283
296
if image :
284
297
if image not in __IMAGE_FORMATS :
298
+ img_n = len (__IMAGE_FORMATS )
285
299
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
290
306
script = image_download_script ('iplot' ).format (format = image ,
291
307
width = image_width ,
292
308
height = image_height ,
293
309
filename = filename ,
294
310
plot_id = plotdivid )
295
311
# allow time for the plot to draw
296
- import time
297
312
time .sleep (1 )
298
313
# inject code to download an image of the plot
299
314
display (HTML (script ))
@@ -402,9 +417,13 @@ def plot(figure_or_data,
402
417
403
418
if image :
404
419
if image not in __IMAGE_FORMATS :
420
+ img_n = len (__IMAGE_FORMATS )
405
421
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
+ )
408
427
# if the check passes then download script is injected.
409
428
# write the download script:
410
429
script = image_download_script ('plot' )
0 commit comments