Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8ef7055

Browse filesBrowse files
committed
1 parent 5847ac6 commit 8ef7055
Copy full SHA for 8ef7055

File tree

Expand file treeCollapse file tree

1 file changed

+36
-14
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+36
-14
lines changed

‎plotly/offline/offline.py

Copy file name to clipboardExpand all lines: plotly/offline/offline.py
+36-14Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ def download_plotlyjs(download_url):
4040
]))
4141

4242

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+
4358
def _init_notebook_mode():
4459
"""
4560
Initialize Plotly Offline mode in an IPython Notebook.
@@ -49,15 +64,14 @@ def _init_notebook_mode():
4964
"""
5065
if not tools._ipython_imported:
5166
raise ImportError('`iplot` can only run inside an IPython Notebook.')
52-
from IPython.display import HTML, display
67+
from IPython.display import display
5368

5469
if not os.path.exists(PLOTLY_OFFLINE_BUNDLE):
5570
raise PlotlyOfflineNotFound
5671

5772
global __PLOTLY_OFFLINE_INITIALIZED
5873
__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()))
6175

6276

6377
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
@@ -166,15 +180,23 @@ def plot():
166180
"""
167181
raise NotImplementedError
168182

169-
170183
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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.