|
11 | 11 | import os
|
12 | 12 | import requests
|
13 | 13 |
|
| 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') |
14 | 18 |
|
15 |
| -def download_plotlyjs(download_url, plotlyjs_dir='~/.plotly/plotlyjs'): |
16 |
| - plotlyjs_path = os.path.expanduser( |
17 |
| - os.path.join(*'~/.plotly/plotlyjs'.split('/'))) |
18 | 19 |
|
| 20 | +def download_plotlyjs(download_url): |
19 | 21 | if not os.path.exists(plotlyjs_path):
|
20 | 22 | os.makedirs(plotlyjs_path)
|
21 | 23 |
|
22 |
| - res = requests.get(download_url + '/sourcefiles.json') |
| 24 | + res = requests.get(download_url) |
23 | 25 | res.raise_for_status()
|
24 | 26 |
|
25 |
| - with open(os.path.join(plotlyjs_path, 'sourcefiles.json'), 'w') as f: |
| 27 | + with open(PLOTLY_OFFLINE_BUNDLE, 'w') as f: |
26 | 28 | f.write(res.content)
|
27 | 29 |
|
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 | + ])) |
50 | 38 |
|
51 | 39 |
|
52 | 40 | def init_notebook_mode(plotlyjs_dir='~/.plotly/plotlyjs'):
|
53 | 41 | # TODO: check if ipython is available...?
|
54 | 42 | from IPython.display import HTML, display
|
55 | 43 |
|
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} ' |
61 | 46 | 'is not found.\n'
|
62 | 47 | 'If you have a Plotly Offline license, then try '
|
63 | 48 | 'running plotly.offline.configure_offline(url) '
|
64 | 49 | 'with a licensed download url.\n'
|
65 | 50 | "Don't have a Plotly Offline license?"
|
66 | 51 | 'Contact [email protected] learn more about licensing.\n'
|
67 | 52 |
|
68 |
| - .format(source_path=sourcefiles_path)) |
69 |
| - |
70 |
| - sourcefiles = json.load(open(sourcefiles_path))['files'] |
| 53 | + .format(source_path=PLOTLY_OFFLINE_BUNDLE)) |
71 | 54 |
|
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>')) |
86 | 57 |
|
87 | 58 |
|
88 | 59 | def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
|
@@ -146,4 +117,3 @@ def plot():
|
146 | 117 | """ Configured to work with localhost Plotly graph viewer
|
147 | 118 | """
|
148 | 119 | raise NotImplementedError
|
149 |
| - |
0 commit comments