Skip to content

Commit 146fa24

Browse files
authored
Merge pull request #793 from lgeiger/remove-init_notebook_mode-check
Make using offline.init_notebook_mode() optional
2 parents 65b5c3f + a108b9f commit 146fa24

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

Diff for: plotly/offline/offline.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -322,26 +322,13 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
322322
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image='png')
323323
```
324324
"""
325-
if not __PLOTLY_OFFLINE_INITIALIZED:
326-
raise PlotlyError('\n'.join([
327-
'Plotly Offline mode has not been initialized in this notebook. '
328-
'Run: ',
329-
'',
330-
'import plotly',
331-
'plotly.offline.init_notebook_mode() '
332-
'# run at the start of every ipython notebook',
333-
]))
334325
if not ipython:
335326
raise ImportError('`iplot` can only run inside an IPython Notebook.')
336327

337328
config = dict(config) if config else {}
338329
config.setdefault('showLink', show_link)
339330
config.setdefault('linkText', link_text)
340331

341-
plot_html, plotdivid, width, height = _plot_html(
342-
figure_or_data, config, validate, '100%', 525, True
343-
)
344-
345332
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
346333

347334
# Though it can add quite a bit to the display-bundle size, we include
@@ -358,14 +345,27 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
358345
if frames:
359346
fig['frames'] = frames
360347

361-
display_bundle = {
362-
'application/vnd.plotly.v1+json': fig,
363-
'text/html': plot_html,
364-
'text/vnd.plotly.v1+html': plot_html
365-
}
348+
display_bundle = {'application/vnd.plotly.v1+json': fig}
349+
350+
if __PLOTLY_OFFLINE_INITIALIZED:
351+
plot_html, plotdivid, width, height = _plot_html(
352+
figure_or_data, config, validate, '100%', 525, True
353+
)
354+
display_bundle['text/html'] = plot_html
355+
display_bundle['text/vnd.plotly.v1+html'] = plot_html
356+
366357
ipython_display.display(display_bundle, raw=True)
367358

368359
if image:
360+
if not __PLOTLY_OFFLINE_INITIALIZED:
361+
raise PlotlyError('\n'.join([
362+
'Plotly Offline mode has not been initialized in this notebook. '
363+
'Run: ',
364+
'',
365+
'import plotly',
366+
'plotly.offline.init_notebook_mode() '
367+
'# run at the start of every ipython notebook',
368+
]))
369369
if image not in __IMAGE_FORMATS:
370370
raise ValueError('The image parameter must be one of the following'
371371
': {}'.format(__IMAGE_FORMATS)

Diff for: plotly/tests/test_optional/test_offline/test_offline.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,26 @@ class PlotlyOfflineTestCase(TestCase):
2929
def setUp(self):
3030
pass
3131

32-
@raises(plotly.exceptions.PlotlyError)
33-
def test_iplot_doesnt_work_before_you_call_init_notebook_mode(self):
32+
def test_iplot_works_without_init_notebook_mode(self):
3433
plotly.offline.iplot([{}])
3534

35+
@raises(plotly.exceptions.PlotlyError)
36+
def test_iplot_doesnt_work_before_you_call_init_notebook_mode_when_requesting_download(self):
37+
plotly.offline.iplot([{}], image='png')
38+
3639
def test_iplot_works_after_you_call_init_notebook_mode(self):
3740
plotly.offline.init_notebook_mode()
3841
plotly.offline.iplot([{}])
3942

4043
@attr('matplotlib')
41-
def test_iplot_mpl_works_after_you_call_init_notebook_mode(self):
44+
def test_iplot_mpl_works(self):
4245
# Generate matplotlib plot for tests
4346
fig = plt.figure()
4447

4548
x = [10, 20, 30]
4649
y = [100, 200, 300]
4750
plt.plot(x, y, "o")
4851

49-
plotly.offline.init_notebook_mode()
5052
plotly.offline.iplot_mpl(fig)
5153

5254

0 commit comments

Comments
 (0)