Embed plotly graph in a Sphinx doc Embed plotly graph in a Sphinx doc python python

Embed plotly graph in a Sphinx doc


I can see two solutions to embed your notebook cells with plotly figures in a sphinx documentation.

  1. Convert the notebook to html using nbconvert or nbsphinx. Be sure to use the notebook renderer in order to include the plotly javascript bundle (maybe this was the reason why your figures did not display): see https://plot.ly/python/renderers/ for the documentation on plotly renderers. Then you can include the html in your sphinx doc with various solutions (see Include standalone HTML page in sphinx document).
  2. Use sphinx-gallery (https://sphinx-gallery.github.io/) with its plotly scraper. With sphinx-gallery you write your doc page as a python script which is converted to rest by the sphinx-gallery extension of sphinx. In order to configure the scraper see https://sphinx-gallery.github.io/advanced.html#integrate-custom-scrapers-with-sphinx-gallery. We have also a minimal repository showing how to use plotly and sphinx-gallery together in https://github.com/plotly/plotly-sphinx-gallery.https://github.com/plotly/plotly-sphinx-gallery


The graph itself is an HTML inside jupyter notebook file.You can open notebook.ipnb file in any text editor, find where HTML of the graph starts, copy all this html into html file ans save it , say a.html, and then just insert raw html in RST doc.

.. raw::     :file: a.html


From Python, you can generate the HTML code to embed Plotly graphs with the plotly.tools.get_embed function.

import plotly.tools as tlstls.get_embed('https://plot.ly/~chris/1638')