8
8
import json
9
9
import os
10
10
import uuid
11
-
12
- import requests
11
+ from pkg_resources import resource_string
12
+ import warnings
13
13
14
14
from plotly import session , tools , utils
15
15
from plotly .exceptions import PlotlyError
16
16
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
-
22
17
23
18
__PLOTLY_OFFLINE_INITIALIZED = False
24
19
25
20
26
21
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
32
28
33
- with open (PLOTLY_OFFLINE_BUNDLE , 'wb' ) as f :
34
- f .write (res .content )
35
29
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
45
34
46
35
47
36
def init_notebook_mode ():
@@ -55,21 +44,16 @@ def init_notebook_mode():
55
44
raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
56
45
from IPython .display import HTML , display
57
46
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
-
67
- .format (source_path = PLOTLY_OFFLINE_BUNDLE ))
68
-
69
47
global __PLOTLY_OFFLINE_INITIALIZED
70
48
__PLOTLY_OFFLINE_INITIALIZED = True
71
49
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>' ))
73
57
74
58
75
59
def iplot (figure_or_data , show_link = True , link_text = 'Export to plot.ly' ):
@@ -186,4 +170,3 @@ def plot():
186
170
""" Configured to work with localhost Plotly graph viewer
187
171
"""
188
172
raise NotImplementedError
189
-
0 commit comments