Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8d11a8

Browse files
committed
ship plotly.js with the package
1 parent 244f4e5 commit b8d11a8

File tree

3 files changed

+64
-47
lines changed

3 files changed

+64
-47
lines changed

plotly/offline/offline.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,29 @@
88
import json
99
import os
1010
import uuid
11-
12-
import requests
11+
from pkg_resources import resource_string
12+
import warnings
1313

1414
from plotly import session, tools, utils
1515
from plotly.exceptions import PlotlyError
1616

17-
PLOTLY_OFFLINE_DIRECTORY = plotlyjs_path = os.path.expanduser(
18-
os.path.join(*'~/.plotly/plotlyjs'.split('/')))
19-
PLOTLY_OFFLINE_BUNDLE = os.path.join(PLOTLY_OFFLINE_DIRECTORY,
20-
'plotly-ipython-offline-bundle.js')
21-
2217

2318
__PLOTLY_OFFLINE_INITIALIZED = False
2419

2520

2621
def download_plotlyjs(download_url):
27-
if not os.path.exists(PLOTLY_OFFLINE_DIRECTORY):
28-
os.makedirs(PLOTLY_OFFLINE_DIRECTORY)
29-
30-
res = requests.get(download_url)
31-
res.raise_for_status()
22+
warnings.warn('''
23+
`download_plotlyjs` is deprecated and will be removed in the
24+
next release. plotly.js is shipped with this module, it is no
25+
longer necessary to download this bundle separately.
26+
''', DeprecationWarning)
27+
pass
3228

33-
with open(PLOTLY_OFFLINE_BUNDLE, 'wb') as f:
34-
f.write(res.content)
3529

36-
print('\n'.join([
37-
'Success! Now start an IPython notebook and run the following ' +
38-
'code to make your first offline graph:',
39-
'',
40-
'import plotly',
41-
'plotly.offline.init_notebook_mode() '
42-
'# run at the start of every ipython notebook',
43-
'plotly.offline.iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])'
44-
]))
30+
def get_plotlyjs():
31+
path = os.path.join('offline', 'plotly.min.js')
32+
plotlyjs = resource_string('plotly', path).decode('utf-8')
33+
return plotlyjs
4534

4635

4736
def init_notebook_mode():
@@ -55,21 +44,16 @@ def init_notebook_mode():
5544
raise ImportError('`iplot` can only run inside an IPython Notebook.')
5645
from IPython.display import HTML, display
5746

58-
if not os.path.exists(PLOTLY_OFFLINE_BUNDLE):
59-
raise PlotlyError('Plotly Offline source file at {source_path} '
60-
'is not found.\n'
61-
'If you have a Plotly Offline license, then try '
62-
'running plotly.offline.download_plotlyjs(url) '
63-
'with a licensed download url.\n'
64-
"Don't have a Plotly Offline license? "
65-
'Contact [email protected] learn more about licensing.\n'
66-
'Questions? [email protected].'
67-
.format(source_path=PLOTLY_OFFLINE_BUNDLE))
68-
6947
global __PLOTLY_OFFLINE_INITIALIZED
7048
__PLOTLY_OFFLINE_INITIALIZED = True
7149
display(HTML('<script type="text/javascript">' +
72-
open(PLOTLY_OFFLINE_BUNDLE).read() + '</script>'))
50+
# ipython's includes `require` as a global, which
51+
# conflicts with plotly.js. so, unrequire it.
52+
'require=requirejs=define=undefined;' +
53+
'</script>' +
54+
'<script type="text/javascript">' +
55+
get_plotlyjs() +
56+
'</script>'))
7357

7458

7559
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
@@ -186,4 +170,3 @@ def plot():
186170
""" Configured to work with localhost Plotly graph viewer
187171
"""
188172
raise NotImplementedError
189-

0 commit comments

Comments
 (0)