30
30
_matplotlib_imported = False
31
31
32
32
__PLOTLY_OFFLINE_INITIALIZED = False
33
+ __PLOTLY_USE_CDN = False
34
+
33
35
34
36
def download_plotlyjs (download_url ):
35
37
warnings .warn ('''
@@ -46,7 +48,7 @@ def get_plotlyjs():
46
48
return plotlyjs
47
49
48
50
49
- def init_notebook_mode ():
51
+ def init_notebook_mode (connected = False ):
50
52
"""
51
53
Initialize plotly.js in the browser if it hasn't been loaded into the DOM
52
54
yet. This is an idempotent method and can and should be called from any
@@ -56,20 +58,40 @@ def init_notebook_mode():
56
58
raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
57
59
58
60
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 ())
73
95
74
96
display (HTML (script_inject ))
75
97
__PLOTLY_OFFLINE_INITIALIZED = True
@@ -126,7 +148,7 @@ def _plot_html(figure_or_data, show_link, link_text,
126
148
127
149
optional_line1 = ('require(["plotly"], function(Plotly) {{ '
128
150
if global_requirejs else '' )
129
- optional_line2 = '}});' if global_requirejs else ''
151
+ optional_line2 = ( '}});' if global_requirejs else '' )
130
152
131
153
plotly_html_div = (
132
154
''
0 commit comments