Skip to content

Commit ee3b430

Browse files
author
Christian Fufezan
committed
Added serve_plotlyjs_from_directory() instead of kwarg to separate html and js into two files
1 parent 530b299 commit ee3b430

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

plotly/offline/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
iplot,
1111
iplot_mpl,
1212
plot,
13-
plot_mpl
13+
plot_mpl,
14+
serve_plotlyjs_from_directory
1415
)

plotly/offline/offline.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
_matplotlib_imported = False
3232

3333
__PLOTLY_OFFLINE_INITIALIZED = False
34+
__SERVE_PLOTLYJS_FROM_DIRECTORY = False
3435

3536

3637
def download_plotlyjs(download_url):
@@ -42,15 +43,29 @@ def download_plotlyjs(download_url):
4243
pass
4344

4445

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:
4749
plotlyjs = '</script><script src="plotly.min.js">'
4850
else:
4951
path = os.path.join('offline', 'plotly.min.js')
5052
plotlyjs = resource_string('plotly', path).decode('utf-8')
5153
return plotlyjs
5254

5355

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+
5469
def init_notebook_mode(connected=False):
5570
"""
5671
Initialize plotly.js in the browser if it hasn't been loaded into the DOM
@@ -247,7 +262,7 @@ def plot(figure_or_data,
247262
include_plotlyjs=True,
248263
filename='temp-plot.html',
249264
auto_open=True,
250-
outsource_plotly=False):
265+
):
251266
""" Create a plotly graph locally as an HTML document or string.
252267
253268
Example:
@@ -293,10 +308,6 @@ def plot(figure_or_data,
293308
auto_open (default=True) -- If True, open the saved file in a
294309
web browser after saving.
295310
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.
300311
"""
301312
if output_type not in ['div', 'file']:
302313
raise ValueError(
@@ -328,15 +339,19 @@ def plot(figure_or_data,
328339
os.path.dirname(__file__), 'plotly.min.js'
329340
)
330341
# 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 )
334349

335350
with open(filename, 'w') as f:
336351
if include_plotlyjs:
337352
plotly_js_script = ''.join([
338353
'<script type="text/javascript">',
339-
get_plotlyjs(outsource_plotly=outsource_plotly),
354+
get_plotlyjs(),
340355
'</script>',
341356
])
342357
else:
@@ -363,7 +378,7 @@ def plot(figure_or_data,
363378
return ''.join([
364379
'<div>',
365380
'<script type="text/javascript">',
366-
get_plotlyjs(outsource_plotly=outsource_plotly),
381+
get_plotlyjs(),
367382
'</script>',
368383
plot_html,
369384
'</div>'

0 commit comments

Comments
 (0)