Skip to content
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

Plotly fails to load in html docs #400

Open
wellcaffeinated opened this issue Feb 26, 2020 · 6 comments
Open

Plotly fails to load in html docs #400

wellcaffeinated opened this issue Feb 26, 2020 · 6 comments

Comments

@wellcaffeinated
Copy link

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?

@mgeier
Copy link
Member

mgeier commented Feb 27, 2020

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 ...

@wellcaffeinated
Copy link
Author

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.

@mgeier
Copy link
Member

mgeier commented Feb 29, 2020

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?

@miohtama
Copy link

miohtama commented Jul 21, 2021

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
Copy link

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)

@mgeier
Copy link
Member

mgeier commented Jul 25, 2021

@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 #572 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants