@@ -40,6 +40,21 @@ def download_plotlyjs(download_url):
40
40
]))
41
41
42
42
43
+ class JSSourceFiles (object ):
44
+ def __init__ (self , text ):
45
+ self .text = text
46
+
47
+ def _repr_html_ (self ):
48
+ return ('<script type="text/javascript>' +
49
+ self .text + '</script>' )
50
+
51
+ def __repr__ (self ):
52
+ return ''
53
+
54
+ def __str__ (self ):
55
+ return ''
56
+
57
+
43
58
def _init_notebook_mode ():
44
59
"""
45
60
Initialize Plotly Offline mode in an IPython Notebook.
@@ -49,15 +64,14 @@ def _init_notebook_mode():
49
64
"""
50
65
if not tools ._ipython_imported :
51
66
raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
52
- from IPython .display import HTML , display
67
+ from IPython .display import display
53
68
54
69
if not os .path .exists (PLOTLY_OFFLINE_BUNDLE ):
55
70
raise PlotlyOfflineNotFound
56
71
57
72
global __PLOTLY_OFFLINE_INITIALIZED
58
73
__PLOTLY_OFFLINE_INITIALIZED = True
59
- display (HTML ('<script type="text/javascript">' +
60
- open (PLOTLY_OFFLINE_BUNDLE ).read () + '</script>' ))
74
+ display (JSSourceFiles (open (PLOTLY_OFFLINE_BUNDLE ).read ()))
61
75
62
76
63
77
def iplot (figure_or_data , show_link = True , link_text = 'Export to plot.ly' ):
@@ -166,15 +180,23 @@ def plot():
166
180
"""
167
181
raise NotImplementedError
168
182
169
-
170
183
try :
171
- _init_notebook_mode ()
172
- except :
173
- # No big deal. The user just hasn't called download_plotlyjs yet.
174
- # Only bubble up the PlotlyOfflineNotFound error when they attempt
175
- # to create a plot and don't have the source files.
176
- # Since this is run on every `import plotly`, catch all of the errors
177
- # here - we'll bubble up the errors in `iplot` if the user intends on
178
- # using offline mode. Otherwise, no sense in erroring on running a
179
- # a function that the user wasn't even going to run.
180
- pass
184
+ # http://stackoverflow.com/questions/5376837/how-can-i-do-an-if-run-from-ipython-test-in-python
185
+ ipython_environment = __IPYTHON__
186
+ ipython_environment = True
187
+ except NameError :
188
+ ipython_environment = False
189
+
190
+
191
+ if ipython_environment :
192
+ try :
193
+ _init_notebook_mode ()
194
+ except :
195
+ # No big deal. The user just hasn't called download_plotlyjs yet.
196
+ # Only bubble up the PlotlyOfflineNotFound error when they attempt
197
+ # to create a plot and don't have the source files.
198
+ # Since this is run on every `import plotly`, catch all of the errors
199
+ # here - we'll bubble up the errors in `iplot` if the user intends on
200
+ # using offline mode. Otherwise, no sense in erroring on running a
201
+ # a function that the user wasn't even going to run.
202
+ pass
0 commit comments