1010import os
1111import requests
1212
13- import plotly .plotly as py
1413from plotly import utils
14+ from plotly .utils import PLOTLY_OFFLINE_DIRECTORY , PLOTLY_OFFLINE_BUNDLE
1515from plotly import tools
16- from plotly .exceptions import PlotlyError
16+ from plotly .exceptions import PlotlyOfflineNotFound
1717
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' )
2218
2319__PLOTLY_OFFLINE_INITIALIZED = False
2420
2521
2622def 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 )
2925
3026 res = requests .get (download_url )
3127 res .raise_for_status ()
3228
33- with open (_PLOTLY_OFFLINE_BUNDLE , 'wb' ) as f :
29+ with open (PLOTLY_OFFLINE_BUNDLE , 'wb' ) as f :
3430 f .write (res .content )
3531
32+ _init_notebook_mode ()
33+
3634 print ('\n ' .join ([
3735 'Success! Now start an IPython notebook and run the following ' +
3836 'code to make your first offline graph:' ,
3937 '' ,
4038 'import plotly' ,
41- 'plotly.offline.init_notebook_mode() '
42- '# run at the start of every ipython notebook' ,
4339 'plotly.offline.iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])'
4440 ]))
4541
4642
47- def init_notebook_mode ():
43+ def _init_notebook_mode ():
4844 """
4945 Initialize Plotly Offline mode in an IPython Notebook.
5046 Run this function at the start of an IPython notebook
@@ -55,21 +51,13 @@ def init_notebook_mode():
5551 raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
5652 from IPython .display import HTML , display
5753
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 ))
54+ if not os .path .exists (PLOTLY_OFFLINE_BUNDLE ):
55+ raise PlotlyOfflineNotFound
6856
6957 global __PLOTLY_OFFLINE_INITIALIZED
7058 __PLOTLY_OFFLINE_INITIALIZED = True
7159 display (HTML ('<script type="text/javascript">' +
72- open (_PLOTLY_OFFLINE_BUNDLE ).read () + '</script>' ))
60+ open (PLOTLY_OFFLINE_BUNDLE ).read () + '</script>' ))
7361
7462
7563def 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'):
9381
9482 Example:
9583 ```
96- from plotly.offline import init_notebook_mode, iplot
97- init_notebook_mode()
84+ from plotly.offline import iplot
9885
9986 iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
10087 ```
10188 """
10289 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
11191 if not tools ._ipython_imported :
11292 raise ImportError ('`iplot` can only run inside an IPython Notebook.' )
11393
@@ -142,8 +122,8 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly'):
142122 if show_link is False :
143123 link_text = ''
144124
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' )
147127 if (plotly_platform_url != 'https://plot.ly' and
148128 link_text == 'Export to plot.ly' ):
149129
@@ -185,3 +165,12 @@ def plot():
185165 """ Configured to work with localhost Plotly graph viewer
186166 """
187167 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