31
31
_matplotlib_imported = False
32
32
33
33
__PLOTLY_OFFLINE_INITIALIZED = False
34
+ __SERVE_PLOTLYJS_FROM_DIRECTORY = False
34
35
35
36
36
37
def download_plotlyjs (download_url ):
@@ -42,15 +43,29 @@ def download_plotlyjs(download_url):
42
43
pass
43
44
44
45
45
- def get_plotlyjs ( outsource_plotly = False ):
46
- if outsource_plotly :
46
+ def get_plotlyjs ():
47
+ global __SERVE_PLOTLYJS_FROM_DIRECTORY
48
+ if __SERVE_PLOTLYJS_FROM_DIRECTORY :
47
49
plotlyjs = '</script><script src="plotly.min.js">'
48
50
else :
49
51
path = os .path .join ('offline' , 'plotly.min.js' )
50
52
plotlyjs = resource_string ('plotly' , path ).decode ('utf-8' )
51
53
return plotlyjs
52
54
53
55
56
+ def serve_plotlyjs_from_directory ():
57
+ """
58
+ Sets global plotly.offline.__SERVE_PLOTLYJS_FROM_DIRECTORY to True,
59
+ which will trigger the separation of the html and the plotly.min.js
60
+ into two files. This will reduce the overall space required
61
+ if muliple plots are created in the same directory, since only one
62
+ copy of plotly.min.js will be stored.
63
+ """
64
+ global __SERVE_PLOTLYJS_FROM_DIRECTORY
65
+ __SERVE_PLOTLYJS_FROM_DIRECTORY = True
66
+ return
67
+
68
+
54
69
def init_notebook_mode (connected = False ):
55
70
"""
56
71
Initialize plotly.js in the browser if it hasn't been loaded into the DOM
@@ -247,7 +262,7 @@ def plot(figure_or_data,
247
262
include_plotlyjs = True ,
248
263
filename = 'temp-plot.html' ,
249
264
auto_open = True ,
250
- outsource_plotly = False ):
265
+ ):
251
266
""" Create a plotly graph locally as an HTML document or string.
252
267
253
268
Example:
@@ -293,10 +308,6 @@ def plot(figure_or_data,
293
308
auto_open (default=True) -- If True, open the saved file in a
294
309
web browser after saving.
295
310
This argument only applies if `output_type` is 'file'.
296
- outsource_plotly (default=False) -- If True, the javascript library will
297
- be copied into a separate file, thereby reducing the file size for the
298
- actual plat significantly. This is handy if several plots are created
299
- in the same folder.
300
311
"""
301
312
if output_type not in ['div' , 'file' ]:
302
313
raise ValueError (
@@ -328,15 +339,19 @@ def plot(figure_or_data,
328
339
os .path .dirname (__file__ ), 'plotly.min.js'
329
340
)
330
341
# src_path = resource_string('plotly', path)
331
- dest_path = os .path .join ( os .path .dirname ( filename ), 'plotly.min.js' )
332
- if os .path .exists ( dest_path ) is False :
333
- shutil .copy ( src_path , dest_path )
342
+ global __SERVE_PLOTLYJS_FROM_DIRECTORY
343
+ if __SERVE_PLOTLYJS_FROM_DIRECTORY :
344
+ dest_path = os .path .join (
345
+ os .path .dirname ( filename ), 'plotly.min.js'
346
+ )
347
+ if os .path .exists ( dest_path ) is False :
348
+ shutil .copy ( src_path , dest_path )
334
349
335
350
with open (filename , 'w' ) as f :
336
351
if include_plotlyjs :
337
352
plotly_js_script = '' .join ([
338
353
'<script type="text/javascript">' ,
339
- get_plotlyjs (outsource_plotly = outsource_plotly ),
354
+ get_plotlyjs (),
340
355
'</script>' ,
341
356
])
342
357
else :
@@ -363,7 +378,7 @@ def plot(figure_or_data,
363
378
return '' .join ([
364
379
'<div>' ,
365
380
'<script type="text/javascript">' ,
366
- get_plotlyjs (outsource_plotly = outsource_plotly ),
381
+ get_plotlyjs (),
367
382
'</script>' ,
368
383
plot_html ,
369
384
'</div>'
0 commit comments