Skip to content

V1.10.0 - Inject plotly.js into the output cell on every init_notebook_mode call #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

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


## [1.9.13] - 2016-05-19
### Fixed
- Fixed issue in offline mode related to the inability to reload plotly.js on page refresh and extra init_notebook_mode calls.
Expand Down
35 changes: 20 additions & 15 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import plotly
from plotly import tools, utils
from plotly.exceptions import PlotlyError

try:
import IPython
Expand All @@ -28,6 +29,7 @@
except ImportError:
_matplotlib_imported = False

__PLOTLY_OFFLINE_INITIALIZED = False

def download_plotlyjs(download_url):
warnings.warn('''
Expand All @@ -50,16 +52,11 @@ def init_notebook_mode():
yet. This is an idempotent method and can and should be called from any
offline methods that require plotly.js to be loaded into the notebook dom.
"""
warnings.warn('''
`init_notebook_mode` is deprecated and will be removed in the
next release. Notebook mode is now automatically initialized when
notebook methods are invoked, so it is no
longer necessary to manually initialize.
''', DeprecationWarning)

if not _ipython_imported:
raise ImportError('`iplot` can only run inside an IPython Notebook.')

global __PLOTLY_OFFLINE_INITIALIZED
# Inject plotly.js into the output cell
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing

if not __PLOTLY_OFFLINE_INITIALIZED:

no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually, that's the heart of the fix. Before, we had this check:

if not __PLOTLY_OFFLINE_INITIALIZED:
    return

which meant that if you ran this function twice, we return None the second time which cleared the output cell (which previously had plotly.js in it). Now, we just inject plotly.js into the output cell every time.

Copy link
Contributor

@theengineear theengineear May 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*__* oh, sweet! sorry, def confused by that on the first read.

script_inject = (
''
'<script type=\'text/javascript\'>'
Expand All @@ -68,14 +65,14 @@ def init_notebook_mode():
'{script}'
'}});'
'require([\'plotly\'], function(Plotly) {{'
'console.log(Plotly);'
'window.Plotly = Plotly;'
'}});'
'}}'
'</script>'
'').format(script=get_plotlyjs())

display(HTML(script_inject))
__PLOTLY_OFFLINE_INITIALIZED = True


def _plot_html(figure_or_data, show_link, link_text,
Expand Down Expand Up @@ -177,13 +174,22 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',

Example:
```
from plotly.offline import iplot

from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
```
"""

init_notebook_mode()
if not __PLOTLY_OFFLINE_INITIALIZED:
raise PlotlyError('\n'.join([
'Plotly Offline mode has not been initialized in this notebook. '
'Run: ',
'',
'import plotly',
'plotly.offline.init_notebook_mode() '
'# run at the start of every ipython notebook',
]))
if not tools._ipython_imported:
raise ImportError('`iplot` can only run inside an IPython Notebook.')

plot_html, plotdivid, width, height = _plot_html(
figure_or_data, show_link, link_text, validate,
Expand Down Expand Up @@ -415,19 +421,18 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,

Example:
```
from plotly.offline import iplot_mpl
from plotly.offline import init_notebook_mode, iplot_mpl
import matplotlib.pyplot as plt

fig = plt.figure()
x = [10, 15, 20, 25, 30]
y = [100, 250, 200, 150, 300]
plt.plot(x, y, "o")

init_notebook_mode()
iplot_mpl(fig)
```
"""
init_notebook_mode()

plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
return iplot(plotly_plot, show_link, link_text, validate)

Expand Down
4 changes: 2 additions & 2 deletions plotly/tests/test_optional/test_offline/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class PlotlyOfflineTestCase(TestCase):
def setUp(self):
pass

def test_iplot_works_wihout_calling_init_notebook_mode(self):
@raises(plotly.exceptions.PlotlyError)
def test_iplot_doesnt_work_before_you_call_init_notebook_mode(self):
plotly.offline.iplot([{}])

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

2 changes: 1 addition & 1 deletion plotly/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.9.13'
__version__ = '1.10.0'