nbsphinx
nbsphinx copied to clipboard
Plotly fails to load in html docs
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?
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 ...
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.
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?
This is what I did on my custom Sphinx and ReadTheDocs to get Plotly charts working.
-
Grab a copy of
plotly.jsandrequire.jsand have them in your theme_staticfolder -
Add to
<head>in_layout.htmlof 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.
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)
@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.