Skip to content

Make using offline.init_notebook_mode() optional #793

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 1 commit into from
Jul 8, 2017
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
36 changes: 18 additions & 18 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,26 +322,13 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image='png')
```
"""
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 ipython:
raise ImportError('`iplot` can only run inside an IPython Notebook.')

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

plot_html, plotdivid, width, height = _plot_html(
figure_or_data, config, validate, '100%', 525, True
)

figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)

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

display_bundle = {
'application/vnd.plotly.v1+json': fig,
'text/html': plot_html,
'text/vnd.plotly.v1+html': plot_html
}
display_bundle = {'application/vnd.plotly.v1+json': fig}

if __PLOTLY_OFFLINE_INITIALIZED:
plot_html, plotdivid, width, height = _plot_html(
figure_or_data, config, validate, '100%', 525, True
)
display_bundle['text/html'] = plot_html
display_bundle['text/vnd.plotly.v1+html'] = plot_html

ipython_display.display(display_bundle, raw=True)

if image:
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 image not in __IMAGE_FORMATS:
raise ValueError('The image parameter must be one of the following'
': {}'.format(__IMAGE_FORMATS)
Expand Down
10 changes: 6 additions & 4 deletions plotly/tests/test_optional/test_offline/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ class PlotlyOfflineTestCase(TestCase):
def setUp(self):
pass

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

@raises(plotly.exceptions.PlotlyError)
def test_iplot_doesnt_work_before_you_call_init_notebook_mode_when_requesting_download(self):
plotly.offline.iplot([{}], image='png')

def test_iplot_works_after_you_call_init_notebook_mode(self):
plotly.offline.init_notebook_mode()
plotly.offline.iplot([{}])

@attr('matplotlib')
def test_iplot_mpl_works_after_you_call_init_notebook_mode(self):
def test_iplot_mpl_works(self):
# Generate matplotlib plot for tests
fig = plt.figure()

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

plotly.offline.init_notebook_mode()
plotly.offline.iplot_mpl(fig)


Expand Down