Skip to content

Commit f1d223c

Browse files
chriddyptheengineear
authored andcommitted
first pass at nteract support
1 parent 95fa17a commit f1d223c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased]
6+
### Added
7+
- Support for rendering plots in [nteract](https://nteract.io/)!
8+
See [https://github.com/nteract/nteract/pull/662](https://github.com/nteract/nteract/pull/662)
9+
for the associated PR in nteract.
610

711
### Added
812
- `memoize` decorator added to `plotly.utils`

plotly/offline/offline.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
273273
validate=True, image=None, filename='plot_image', image_width=800,
274274
image_height=600):
275275
"""
276-
Draw plotly graphs inside an IPython notebook without
276+
Draw plotly graphs inside an IPython or Jupter notebook without
277277
connecting to an external server.
278278
To save the chart to Plotly Cloud or Plotly Enterprise, use
279279
`plotly.plotly.iplot`.
@@ -335,7 +335,33 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
335335
figure_or_data, config, validate, '100%', 525, True
336336
)
337337

338-
ipython_display.display(ipython_display.HTML(plot_html))
338+
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
339+
340+
# Display 2 versions of the figure:
341+
# - The raw HTML is the "old-school" style - we just throw in a JS script
342+
# inside the cell. This works for a lot of environments but some JS is
343+
# stripped out in some rendering environments / platforms like
344+
# GitHub, nteract, and eventually Plotly
345+
# - The custom `mime-types` is the new-school way. nteract supports
346+
# rendering plotly graphs this way (see https://github.com/nteract/nteract/pull/662)
347+
# and others will hopefully follow suite.
348+
# Both of these bundles will be saved as part of the notebook. The renderer
349+
# will choose which one to render. So 'text/html' is like a fallback for
350+
# any environment that doesn't have the 'application/json+plotly.v1'
351+
# renderer available.
352+
# The bummer is that we're injecting the figure JSON in here twice, so
353+
# notebooks that are pretty big are about to get a lot bigger.
354+
# The upside is that we're dropping any compatability for older notebook
355+
# versions
356+
data = _json.loads(_json.dumps(figure['data'],
357+
cls=plotly.utils.PlotlyJSONEncoder))
358+
layout = _json.loads(_json.dumps(figure.get('layout', {}),
359+
cls=plotly.utils.PlotlyJSONEncoder))
360+
display_bundle = {
361+
'application/vnd.plotly.v1+json': {'data': data, 'layout': layout},
362+
'text/html': plot_html
363+
}
364+
ipython_display.display(display_bundle, raw=True)
339365

340366
if image:
341367
if image not in __IMAGE_FORMATS:

0 commit comments

Comments
 (0)