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

Skip to content

Commit 096e36c

Browse files
committed
bundle it up!
1 parent 500c9ab commit 096e36c

File tree

1 file changed

+20
-50
lines changed

1 file changed

+20
-50
lines changed

plotly/offline.py

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,49 @@
1111
import os
1212
import requests
1313

14+
PLOTLY_OFFLINE_DIRECTORY = plotlyjs_path = os.path.expanduser(
15+
os.path.join(*'~/.plotly/plotlyjs'.split('/')))
16+
PLOTLY_OFFLINE_BUNDLE = os.path.join(PLOTLY_OFFLINE_DIRECTORY,
17+
'plotlyofflinebundle.js')
1418

15-
def download_plotlyjs(download_url, plotlyjs_dir='~/.plotly/plotlyjs'):
16-
plotlyjs_path = os.path.expanduser(
17-
os.path.join(*'~/.plotly/plotlyjs'.split('/')))
1819

20+
def download_plotlyjs(download_url):
1921
if not os.path.exists(plotlyjs_path):
2022
os.makedirs(plotlyjs_path)
2123

22-
res = requests.get(download_url + '/sourcefiles.json')
24+
res = requests.get(download_url)
2325
res.raise_for_status()
2426

25-
with open(os.path.join(plotlyjs_path, 'sourcefiles.json'), 'w') as f:
27+
with open(PLOTLY_OFFLINE_BUNDLE, 'w') as f:
2628
f.write(res.content)
2729

28-
download_queue = json.loads(res.content)['files']
29-
for fn in download_queue:
30-
file_url = download_url + '/' + fn
31-
print('Downloading {}'.format(file_url))
32-
res = requests.get(file_url)
33-
res.raise_for_status()
34-
35-
file_path = os.path.join(plotlyjs_path, *fn.split('/'))
36-
file_dir = os.path.dirname(file_path)
37-
if not os.path.exists(file_dir):
38-
os.makedirs(file_dir)
39-
40-
with open(file_path, 'w') as f:
41-
print('Copying into {}'.format(file_path))
42-
f.write(res.content)
43-
44-
print('\n\n'
45-
'Success! Now start an IPython notebook, '
46-
'initialize offline mode with\n'
47-
'plotly.offline.init_notebook_mode()\n'
48-
'and make your first offline graph:\n'
49-
'plotly.offline.iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])\n')
30+
print('\n'.join([
31+
'Success! Now start an IPython notebook and run the following ' +
32+
'code to make your first offline graph:',
33+
'',
34+
'import plotly',
35+
'plotly.offline.init_notebook_mode() # initialize offline mode',
36+
'plotly.offline.iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])'
37+
]))
5038

5139

5240
def init_notebook_mode(plotlyjs_dir='~/.plotly/plotlyjs'):
5341
# TODO: check if ipython is available...?
5442
from IPython.display import HTML, display
5543

56-
# Split and join the install path for cross-OS directories
57-
plotlyjs_dir = os.path.expanduser(os.path.join(*'~/.plotly/plotlyjs'.split('/')))
58-
sourcefiles_path = os.path.join(plotlyjs_dir, 'sourcefiles.json')
59-
if not os.path.exists(sourcefiles_path):
60-
raise Exception('Plotly Offline configuration file at {source_path} '
44+
if not os.path.exists(PLOTLY_OFFLINE_BUNDLE):
45+
raise Exception('Plotly Offline source file at {source_path} '
6146
'is not found.\n'
6247
'If you have a Plotly Offline license, then try '
6348
'running plotly.offline.configure_offline(url) '
6449
'with a licensed download url.\n'
6550
"Don't have a Plotly Offline license?"
6651
'Contact [email protected] learn more about licensing.\n'
6752
'Questions? [email protected].'
68-
.format(source_path=sourcefiles_path))
69-
70-
sourcefiles = json.load(open(sourcefiles_path))['files']
53+
.format(source_path=PLOTLY_OFFLINE_BUNDLE))
7154

72-
# Include all of the files in the dependencies folder
73-
scriptpaths = [os.path.join(plotlyjs_dir, *sourcefile.split('/'))
74-
for sourcefile in sourcefiles]
75-
76-
unrequirejs = ('<script type="text/javascript">'
77-
'require=requirejs=define=undefined;</script>')
78-
79-
display(HTML(
80-
unrequirejs +
81-
'\n'.join([
82-
'<script type="text/javascript">' + open(script).read() +
83-
'</script>' for script in scriptpaths
84-
])
85-
))
55+
display(HTML('<script type="text/javascript">' +
56+
open(PLOTLY_OFFLINE_BUNDLE).read() + '</script>'))
8657

8758

8859
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
@@ -146,4 +117,3 @@ def plot():
146117
""" Configured to work with localhost Plotly graph viewer
147118
"""
148119
raise NotImplementedError
149-

0 commit comments

Comments
 (0)