Skip to content

Commit 530b299

Browse files
committed
Added offline kwarg outsource_plotly to allow for both kind of plotting, i.e. regular plotly with 1.2MB javascript in each plot, or having plotly.min.js in a seperate file
1 parent 9e2ff2d commit 530b299

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

plotly/offline/offline.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ def download_plotlyjs(download_url):
4242
pass
4343

4444

45-
def get_plotlyjs():
46-
# path = os.path.join('offline', 'plotly.min.js')
47-
# plotlyjs = resource_string('plotly', path).decode('utf-8')
48-
plotlyjs = '</script><script src="plotly.min.js">'
45+
def get_plotlyjs( outsource_plotly = False ):
46+
if outsource_plotly:
47+
plotlyjs = '</script><script src="plotly.min.js">'
48+
else:
49+
path = os.path.join('offline', 'plotly.min.js')
50+
plotlyjs = resource_string('plotly', path).decode('utf-8')
4951
return plotlyjs
5052

5153

@@ -244,7 +246,8 @@ def plot(figure_or_data,
244246
validate=True, output_type='file',
245247
include_plotlyjs=True,
246248
filename='temp-plot.html',
247-
auto_open=True):
249+
auto_open=True,
250+
outsource_plotly=False):
248251
""" Create a plotly graph locally as an HTML document or string.
249252
250253
Example:
@@ -290,6 +293,10 @@ def plot(figure_or_data,
290293
auto_open (default=True) -- If True, open the saved file in a
291294
web browser after saving.
292295
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.
293300
"""
294301
if output_type not in ['div', 'file']:
295302
raise ValueError(
@@ -329,7 +336,7 @@ def plot(figure_or_data,
329336
if include_plotlyjs:
330337
plotly_js_script = ''.join([
331338
'<script type="text/javascript">',
332-
get_plotlyjs(),
339+
get_plotlyjs(outsource_plotly=outsource_plotly),
333340
'</script>',
334341
])
335342
else:
@@ -356,7 +363,7 @@ def plot(figure_or_data,
356363
return ''.join([
357364
'<div>',
358365
'<script type="text/javascript">',
359-
get_plotlyjs(),
366+
get_plotlyjs(outsource_plotly=outsource_plotly),
360367
'</script>',
361368
plot_html,
362369
'</div>'

0 commit comments

Comments
 (0)