nbsphinx icon indicating copy to clipboard operation
nbsphinx copied to clipboard

Plotly fails to load in html docs

Open wellcaffeinated opened this issue 5 years ago • 6 comments

This is related to #128 but a different problem. Currently plotly seems to be calling a non-existent method require.undef which is causing the plots not to load.

<script type="text/javascript">
window.PlotlyConfig = {MathJaxConfig: 'local'};
if (window.MathJax) {MathJax.Hub.Config({SVG: {font: "STIX-Web"}});}
if (typeof require !== 'undefined') {
require.undef("plotly"); // <===== FAILS HERE
requirejs.config({
    paths: {
        'plotly': ['https://cdn.plot.ly/plotly-latest.min']
    }
});
require(['plotly'], function(Plotly) {
    window._Plotly = Plotly;
});
}
</script></div>

Additionally I tried manually commenting out that line, and the require callback that initializes the plot (not shown here) fails to load. I'm not sure why this isn't working. Has anyone else seen this problem?

wellcaffeinated avatar Feb 26 '20 22:02 wellcaffeinated

Can you please provide a minimal example to reproduce this problem?

Does it work on nbviewer?

Does it work with nbconvert?

Same procedure as in #128 ...

mgeier avatar Feb 27 '20 11:02 mgeier

Sure, here's a simple example rendered by sphinx: http://spdcalc.org/spdcalc/examples/minimal_debug.html

Here's the github link: https://github.com/kshalm/spdcalc/blob/v2.0/python/docs/examples/minimal_debug.ipynb

The plot doesn't render in github. But it shows up on nbviewer: https://nbviewer.jupyter.org/github/kshalm/spdcalc/blob/v2.0/python/docs/examples/minimal_debug.ipynb

It does not work with nbconvert.

wellcaffeinated avatar Feb 27 '20 17:02 wellcaffeinated

Thanks for the details!

There seems to be some interaction with the RTD theme, they seem to define a require() function for some reason? Can you please try a different Sphinx theme?

mgeier avatar Feb 29 '20 14:02 mgeier

This is what I did on my custom Sphinx and ReadTheDocs to get Plotly charts working.

  1. Grab a copy of plotly.js and require.js and have them in your theme _static folder

  2. Add to <head> in _layout.html of the Sphinx theme:

  {#

    Plotly uses require.js for asynchronous JavaScript loading.

    Here we will define the require.js config and tell
    nbsphinx embedded <script> tags from where to load Plotly.

  #}
  <script type="text/javascript" src="{{ pathto('_static/require.js', 1) }}"></script>
  <script>
    require.config({
      paths: {
          plotly: '{{ pathto("_static/plotly", 1) }}'
      }
    });
  </script>

This will tell require.js config where to load Plotly. The configuration must be in <head> section because embedded Plotly charts are executing with inline <script> in the document.

For the full example, see the source code here.

miohtama avatar Jul 21 '21 09:07 miohtama

Also make sure you use "offline" plotly renderer in your notebooks.

import plotly.graph_objects as go
from plotly.offline import iplot

fig = go.Figure(data=[go.Candlestick(x=eth_usdc_pair['timestamp'],
                open=eth_usdc_pair['open'],
                high=eth_usdc_pair['high'],
                low=eth_usdc_pair['low'],
                close=eth_usdc_pair['close'])])
iplot(fig)

miohtama avatar Jul 21 '21 10:07 miohtama

@wellcaffeinated It looks like Plotly assumes MathJax version 2, but Sphinx (and nbsphinx) have recently switched to version 3. You can switch back to MathJax 2 as shown in https://github.com/spatialaudio/nbsphinx/issues/572#issuecomment-853389268.

mgeier avatar Jul 25 '21 17:07 mgeier