Skip to content

Commit 419098a

Browse files
author
Kevin Yan
committed
Merge pull request #475 from plotly/cdn-options
Allow for the option of a CDN in offline mode
2 parents 543147c + 8822ff4 commit 419098a

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

plotly/offline/offline.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
_matplotlib_imported = False
3131

3232
__PLOTLY_OFFLINE_INITIALIZED = False
33+
__PLOTLY_USE_CDN = False
34+
3335

3436
def download_plotlyjs(download_url):
3537
warnings.warn('''
@@ -46,7 +48,7 @@ def get_plotlyjs():
4648
return plotlyjs
4749

4850

49-
def init_notebook_mode():
51+
def init_notebook_mode(connected=False):
5052
"""
5153
Initialize plotly.js in the browser if it hasn't been loaded into the DOM
5254
yet. This is an idempotent method and can and should be called from any
@@ -56,20 +58,40 @@ def init_notebook_mode():
5658
raise ImportError('`iplot` can only run inside an IPython Notebook.')
5759

5860
global __PLOTLY_OFFLINE_INITIALIZED
59-
# Inject plotly.js into the output cell
60-
script_inject = (
61-
''
62-
'<script type=\'text/javascript\'>'
63-
'if(!window.Plotly){{'
64-
'define(\'plotly\', function(require, exports, module) {{'
65-
'{script}'
66-
'}});'
67-
'require([\'plotly\'], function(Plotly) {{'
68-
'window.Plotly = Plotly;'
69-
'}});'
70-
'}}'
71-
'</script>'
72-
'').format(script=get_plotlyjs())
61+
global __PLOTLY_USE_CDN
62+
63+
__PLOTLY_USE_CDN = connected
64+
65+
if connected:
66+
# Inject plotly.js into the output cell
67+
script_inject = (
68+
''
69+
'<script>'
70+
'requirejs.config({'
71+
'paths: { '
72+
'\'plotly\': [\'https://cdn.plot.ly/plotly-latest.min\']},'
73+
'});'
74+
'if(!window.Plotly) {{'
75+
'require([\'plotly\'],'
76+
'function(plotly) {window.Plotly=plotly;});'
77+
'}}'
78+
'</script>'
79+
)
80+
else:
81+
# Inject plotly.js into the output cell
82+
script_inject = (
83+
''
84+
'<script type=\'text/javascript\'>'
85+
'if(!window.Plotly){{'
86+
'define(\'plotly\', function(require, exports, module) {{'
87+
'{script}'
88+
'}});'
89+
'require([\'plotly\'], function(Plotly) {{'
90+
'window.Plotly = Plotly;'
91+
'}});'
92+
'}}'
93+
'</script>'
94+
'').format(script=get_plotlyjs())
7395

7496
display(HTML(script_inject))
7597
__PLOTLY_OFFLINE_INITIALIZED = True
@@ -126,7 +148,7 @@ def _plot_html(figure_or_data, show_link, link_text,
126148

127149
optional_line1 = ('require(["plotly"], function(Plotly) {{ '
128150
if global_requirejs else '')
129-
optional_line2 = '}});' if global_requirejs else ''
151+
optional_line2 = ('}});' if global_requirejs else '')
130152

131153
plotly_html_div = (
132154
''

0 commit comments

Comments
 (0)