Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eaa2169

Browse files
author
Kevin Yan
committed
Merge pull request plotly#469 from plotly/offline-fixes
V1.10.0 - Inject plotly.js into the output cell on every `init_notebook_mode` call
2 parents fbf3b74 + 16dcd24 commit eaa2169

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased]
66

7+
## [1.10.0] - 2016-05-19
8+
### Fixed
9+
Version 1.9.13 fixed an issue in offline mode where if you ran `init_notebook_mode`
10+
more than once the function would skip importing (because it saw that it had
11+
already imported the library) but then accidentally clear plotly.js from the DOM.
12+
This meant that if you ran `init_notebook_mode` more than once, your graphs would
13+
not appear when you refreshed the page.
14+
Version 1.9.13 solved this issue by injecting plotly.js with every iplot call.
15+
While this works, it also injects the library excessively, causing notebooks
16+
to have multiple versions of plotly.js inline in the DOM, potentially making
17+
notebooks with many `iplot` calls very large.
18+
Version 1.10.0 brings back the requirement to call `init_notebook_mode` before
19+
making an `iplot` call. It makes `init_notebook_mode` idempotent: you can call
20+
it multiple times without worrying about losing your plots on refresh.
21+
22+
723
## [1.9.13] - 2016-05-19
824
### Fixed
925
- Fixed issue in offline mode related to the inability to reload plotly.js on page refresh and extra init_notebook_mode calls.

plotly/offline/offline.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import plotly
1616
from plotly import tools, utils
17+
from plotly.exceptions import PlotlyError
1718

1819
try:
1920
import IPython
@@ -28,6 +29,7 @@
2829
except ImportError:
2930
_matplotlib_imported = False
3031

32+
__PLOTLY_OFFLINE_INITIALIZED = False
3133

3234
def download_plotlyjs(download_url):
3335
warnings.warn('''
@@ -50,16 +52,11 @@ def init_notebook_mode():
5052
yet. This is an idempotent method and can and should be called from any
5153
offline methods that require plotly.js to be loaded into the notebook dom.
5254
"""
53-
warnings.warn('''
54-
`init_notebook_mode` is deprecated and will be removed in the
55-
next release. Notebook mode is now automatically initialized when
56-
notebook methods are invoked, so it is no
57-
longer necessary to manually initialize.
58-
''', DeprecationWarning)
59-
6055
if not _ipython_imported:
6156
raise ImportError('`iplot` can only run inside an IPython Notebook.')
6257

58+
global __PLOTLY_OFFLINE_INITIALIZED
59+
# Inject plotly.js into the output cell
6360
script_inject = (
6461
''
6562
'<script type=\'text/javascript\'>'
@@ -68,14 +65,14 @@ def init_notebook_mode():
6865
'{script}'
6966
'}});'
7067
'require([\'plotly\'], function(Plotly) {{'
71-
'console.log(Plotly);'
7268
'window.Plotly = Plotly;'
7369
'}});'
7470
'}}'
7571
'</script>'
7672
'').format(script=get_plotlyjs())
7773

7874
display(HTML(script_inject))
75+
__PLOTLY_OFFLINE_INITIALIZED = True
7976

8077

8178
def _plot_html(figure_or_data, show_link, link_text,
@@ -177,13 +174,22 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
177174
178175
Example:
179176
```
180-
from plotly.offline import iplot
181-
177+
from plotly.offline import init_notebook_mode, iplot
178+
init_notebook_mode()
182179
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
183180
```
184181
"""
185-
186-
init_notebook_mode()
182+
if not __PLOTLY_OFFLINE_INITIALIZED:
183+
raise PlotlyError('\n'.join([
184+
'Plotly Offline mode has not been initialized in this notebook. '
185+
'Run: ',
186+
'',
187+
'import plotly',
188+
'plotly.offline.init_notebook_mode() '
189+
'# run at the start of every ipython notebook',
190+
]))
191+
if not tools._ipython_imported:
192+
raise ImportError('`iplot` can only run inside an IPython Notebook.')
187193

188194
plot_html, plotdivid, width, height = _plot_html(
189195
figure_or_data, show_link, link_text, validate,
@@ -415,19 +421,18 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
415421
416422
Example:
417423
```
418-
from plotly.offline import iplot_mpl
424+
from plotly.offline import init_notebook_mode, iplot_mpl
419425
import matplotlib.pyplot as plt
420426
421427
fig = plt.figure()
422428
x = [10, 15, 20, 25, 30]
423429
y = [100, 250, 200, 150, 300]
424430
plt.plot(x, y, "o")
425431
432+
init_notebook_mode()
426433
iplot_mpl(fig)
427434
```
428435
"""
429-
init_notebook_mode()
430-
431436
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
432437
return iplot(plotly_plot, show_link, link_text, validate)
433438

plotly/tests/test_optional/test_offline/test_offline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class PlotlyOfflineTestCase(TestCase):
2929
def setUp(self):
3030
pass
3131

32-
def test_iplot_works_wihout_calling_init_notebook_mode(self):
32+
@raises(plotly.exceptions.PlotlyError)
33+
def test_iplot_doesnt_work_before_you_call_init_notebook_mode(self):
3334
plotly.offline.iplot([{}])
3435

3536
def test_iplot_works_after_you_call_init_notebook_mode(self):
@@ -86,4 +87,3 @@ def test_default_mpl_plot_generates_expected_html(self):
8687
self.assertTrue(PLOTLYJS in html) # and the source code
8788
# and it's an <html> doc
8889
self.assertTrue(html.startswith('<html>') and html.endswith('</html>'))
89-

plotly/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.9.13'
1+
__version__ = '1.10.0'

0 commit comments

Comments
 (0)