10
10
import os
11
11
import requests
12
12
13
- import plotly .plotly as py
14
13
from plotly import utils
14
+ from plotly .utils import PLOTLY_OFFLINE_DIRECTORY , PLOTLY_OFFLINE_BUNDLE
15
15
from plotly import tools
16
- from plotly .exceptions import PlotlyError
16
+ from plotly .exceptions import PlotlyOfflineNotFound
17
17
18
- _PLOTLY_OFFLINE_DIRECTORY = plotlyjs_path = os .path .expanduser (
19
- os .path .join (* '~/.plotly/plotlyjs' .split ('/' )))
20
- _PLOTLY_OFFLINE_BUNDLE = os .path .join (_PLOTLY_OFFLINE_DIRECTORY ,
21
- 'plotly-ipython-offline-bundle.js' )
22
18
23
19
__PLOTLY_OFFLINE_INITIALIZED = False
24
20
25
21
26
22
def download_plotlyjs (download_url ):
27
- if not os .path .exists (plotlyjs_path ):
28
- os .makedirs (plotlyjs_path )
23
+ if not os .path .exists (PLOTLY_OFFLINE_DIRECTORY ):
24
+ os .makedirs (PLOTLY_OFFLINE_DIRECTORY )
29
25
30
26
res = requests .get (download_url )
31
27
res .raise_for_status ()
32
28
33
- with open (_PLOTLY_OFFLINE_BUNDLE , 'wb' ) as f :
29
+ with open (PLOTLY_OFFLINE_BUNDLE , 'wb' ) as f :
34
30
f .write (res .content )
35
31
32
+ _init_notebook_mode ()
33
+
36
34
print ('\n ' .join ([
37
35
'Success! Now start an IPython notebook and run the following ' +
38
36
'code to make your first offline graph:' ,
39
37
'' ,
40
38
'import plotly' ,
41
- 'plotly.offline.init_notebook_mode() '
42
- '# run at the start of every ipython notebook' ,
43
39
'plotly.offline.iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])'
44
40
]))
45
41
46
42
47
- def init_notebook_mode ():
43
+ def _init_notebook_mode ():
48
44
"""
49
45
Initialize Plotly Offline mode in an IPython Notebook.
50
46
Run this function at the start of an IPython notebook
@@ -55,21 +51,13 @@ def init_notebook_mode():
55
51
raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
56
52
from IPython .display import HTML , display
57
53
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 sales@plot.ly learn more about licensing.\n '
66
- 'Questions? support@plot.ly.'
67
- .format (source_path = _PLOTLY_OFFLINE_BUNDLE ))
54
+ if not os .path .exists (PLOTLY_OFFLINE_BUNDLE ):
55
+ raise PlotlyOfflineNotFound
68
56
69
57
global __PLOTLY_OFFLINE_INITIALIZED
70
58
__PLOTLY_OFFLINE_INITIALIZED = True
71
59
display (HTML ('<script type="text/javascript">' +
72
- open (_PLOTLY_OFFLINE_BUNDLE ).read () + '</script>' ))
60
+ open (PLOTLY_OFFLINE_BUNDLE ).read () + '</script>' ))
73
61
74
62
75
63
def iplot (figure_or_data , show_link = True , link_text = 'Export to plot.ly' ):
@@ -93,21 +81,13 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
93
81
94
82
Example:
95
83
```
96
- from plotly.offline import init_notebook_mode, iplot
97
- init_notebook_mode()
84
+ from plotly.offline import iplot
98
85
99
86
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
100
87
```
101
88
"""
102
89
if not __PLOTLY_OFFLINE_INITIALIZED :
103
- raise PlotlyError ('\n ' .join ([
104
- 'Plotly Offline mode has not been initialized in this notebook. '
105
- 'Run: ' ,
106
- '' ,
107
- 'import plotly' ,
108
- 'plotly.offline.init_notebook_mode() '
109
- '# run at the start of every ipython notebook' ,
110
- ]))
90
+ raise PlotlyOfflineNotFound
111
91
if not tools ._ipython_imported :
112
92
raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
113
93
@@ -142,8 +122,8 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
142
122
if show_link is False :
143
123
link_text = ''
144
124
145
- plotly_platform_url = py . get_config ().get ('plotly_domain' ,
146
- 'https://plot.ly' )
125
+ plotly_platform_url = tools . get_config_file ().get ('plotly_domain' ,
126
+ 'https://plot.ly' )
147
127
if (plotly_platform_url != 'https://plot.ly' and
148
128
link_text == 'Export to plot.ly' ):
149
129
@@ -185,3 +165,12 @@ def plot():
185
165
""" Configured to work with localhost Plotly graph viewer
186
166
"""
187
167
raise NotImplementedError
168
+
169
+
170
+ try :
171
+ _init_notebook_mode ()
172
+ except PlotlyOfflineNotFound :
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
+ pass
0 commit comments