Skip to content

Commit 4f0bb34

Browse files
committed
first pass at rmving load_plotly
1 parent 767d01c commit 4f0bb34

File tree

5 files changed

+50
-219
lines changed

5 files changed

+50
-219
lines changed

plotly/exceptions.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"""
1818
import sys
1919
import six
20+
from utils import PLOTLY_OFFLINE_BUNDLE
21+
2022

2123
if sys.version[:3] == '2.6':
2224
import simplejson as json
@@ -29,7 +31,6 @@ class PlotlyError(Exception):
2931
pass
3032

3133

32-
3334
class InputError(PlotlyError):
3435
pass
3536

@@ -63,6 +64,25 @@ def __str__(self):
6364
return self.message
6465

6566

67+
## Offline Errors ##
68+
class PlotlyOfflineNotFound(PlotlyError):
69+
def __init__(self):
70+
self.message = ('Plotly Offline source file at {source_path} '
71+
'is not found.\n'
72+
'If you have a Plotly Offline license, '
73+
'then try running: \n'
74+
'import plotly\n'
75+
'plotly.offline.download_plotlyjs(url)\n'
76+
'with a licensed download url.\n'
77+
"Don't have a Plotly Offline license? "
78+
'Contact [email protected] learn more about licensing.\n'
79+
'Questions? [email protected].'
80+
.format(source_path=PLOTLY_OFFLINE_BUNDLE))
81+
82+
def __str__(self):
83+
return self.message
84+
85+
6686
## Grid Errors ##
6787

6888
COLUMN_NOT_YET_UPLOADED_MESSAGE = (

plotly/offline.py

Lines changed: 0 additions & 182 deletions
This file was deleted.

plotly/offline/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
"""
66
from . offline import (
77
download_plotlyjs,
8-
init_notebook_mode,
98
iplot
109
)

plotly/offline/offline.py

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,37 @@
1010
import os
1111
import requests
1212

13-
import plotly.plotly as py
1413
from plotly import utils
14+
from plotly.utils import PLOTLY_OFFLINE_DIRECTORY, PLOTLY_OFFLINE_BUNDLE
1515
from 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

2622
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)
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-
'Questions? [email protected].'
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

7563
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'):
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

plotly/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,8 @@ def is_source_key(key):
356356
return True
357357
else:
358358
return False
359+
360+
PLOTLY_OFFLINE_DIRECTORY = plotlyjs_path = os.path.expanduser(
361+
os.path.join(*'~/.plotly/plotlyjs'.split('/')))
362+
PLOTLY_OFFLINE_BUNDLE = os.path.join(PLOTLY_OFFLINE_DIRECTORY,
363+
'plotly-ipython-offline-bundle.js')

0 commit comments

Comments
 (0)