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 graphs not showing in documentation #128

Closed
bluprince13 opened this issue Oct 2, 2017 · 20 comments · Fixed by #302
Closed

Plotly graphs not showing in documentation #128

bluprince13 opened this issue Oct 2, 2017 · 20 comments · Fixed by #302

Comments

@bluprince13
Copy link

Graphs generated using plotly are not showing in the documentation. Matplotlib plots show up just fine.

@mgeier
Copy link
Member

mgeier commented Oct 7, 2017 via email

@bluprince13
Copy link
Author

bluprince13 commented Oct 15, 2017

https://gist.github.com/bluprince13/1a53aabe99b5a4a1ec2007a90889115b

The plots don't render on Github either.

@mgeier
Copy link
Member

mgeier commented Oct 17, 2017

OK, thanks for the example.

Just for the record, it seems to work on nbviewer: http://nbviewer.jupyter.org/gist/bluprince13/1a53aabe99b5a4a1ec2007a90889115b

Does it work with nbconvert?

I don't know how this is supposed to work.
Your example has two output cells. The first one has two alternative contents of the type text/vnd.plotly.v1+html and text/html, respectively. The second output cell also has two alternative contents with the same two types.
nbsphinx includes the text/html part in both cases, because it doesn't know the other type.

When loading the Sphinx-generated HTML page, I get those errors in the JS Console:

ReferenceError: define is not defined
ReferenceError: require is not defined

@bluprince13
Copy link
Author

I asked a question on stackoverflow on this as well. No responses yet.

@bluprince13
Copy link
Author

I can confirm that this does work with nbconvert 5.3.1. So, why is the Sphinx generated documentation not showing it ...

@mgeier
Copy link
Member

mgeier commented Oct 19, 2017

Thanks for checking it out with nbconvert.
Since it is working there, I only had to check what they are doing and I found the problem: require.js is not available!

Since nobody has missed it before, I don't think it's worth including it by default in nbsphinx.

For now, you can use this in your conf.py:

def setup(app):
    app.add_javascript('https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js')

... but probably there is a better solution?
Any ideas?

@bluprince13
Copy link
Author

bluprince13 commented Oct 20, 2017

Hmmm...did you check if it solves the problem? It didn't work for me, but I am not sure if there was some other factor at play.

In terms of a solution, is it feasible to get nbsphinx to generate a meaningful warning if there are missing scripts?

@mgeier
Copy link
Member

mgeier commented Oct 21, 2017

Yes, I checked.

No, I don't think that's possible.

@astrojuanlu
Copy link

astrojuanlu commented Nov 15, 2017

I have tried this workaround, but still I get the ReferenceError: requirejs is not defined.

Code: poliastro/poliastro@master...Juanlu001:master
Build: https://readthedocs.org/projects/juanlu-poliastro/builds/6272017/
Result: http://juanlu-poliastro.readthedocs.io/en/latest/examples/Plotting%20in%203D.html (edit: this might be unavailable for future readers)

I suspect this is because the scripts are loaded at the bottom of the page, after require.js is requested by the plotly graphs.

@astrojuanlu
Copy link

I see you're tracking this here readthedocs/sphinx_rtd_theme#328

@mgeier
Copy link
Member

mgeier commented Nov 17, 2017

Did you try it with a different HTML theme?

@astrojuanlu
Copy link

I can confirm locally that using a different theme fixes the problem, so this is not a nbsphinx issue.

@mgeier
Copy link
Member

mgeier commented Nov 18, 2017

@Juanlu001 OK, thanks! Did you try if readthedocs/sphinx_rtd_theme#467 fixes the problem?

@astrojuanlu
Copy link

Yes it does! 😁 see readthedocs/sphinx_rtd_theme#467 (comment)

@astrojuanlu
Copy link

readthedocs/sphinx_rtd_theme#467 was closed because it was too complex, but now the issue is being tackled at readthedocs/sphinx_rtd_theme#545.

@astrojuanlu
Copy link

Some more info in readthedocs/readthedocs.org#4367 (comment)

@mgeier
Copy link
Member

mgeier commented Dec 11, 2018

readthedocs/sphinx_rtd_theme#545 has finally been merged into the master branch, now we only have to wait for a new release ...

@manycoding
Copy link
Contributor

manycoding commented May 2, 2019

@mgeier I would opt for adding require.js to nbsphinx by default.
I have been tackling this issue (which now seems obvious) for some time, mostly because there are a lot of things involved and you don't know where is the problem - jupyter, plotly, sphinx, nbsphinx, rtd, or your code :)

@mgeier
Copy link
Member

mgeier commented May 3, 2019

@manycoding I guess if nbconvert and nbviewer include require.js, it makes sense to have it in nbsphinx, too.

Would you like to make a PR?

@miohtama
Copy link

We had this issue for both sphinx_rtd_theme and furo theme. Because how Plotly and Sphinx themes using require.js, the default generated HTML output does not work within Sphinx context. You get various loading errors with require.js or other files.

Here is our solution:

  • Make sure your JavaScript loads at <head> in your theme. If it loads at the end of the <body> no workaround is going t work.
  • Server require.js locally from Sphinx _static folder
  • Configure require by using custom.js

Example conf.py:

html_js_files = [
    "require.min.js",  # Add to your _static
    "custom.js",
]

Example custom.js:

requirejs.config({
    paths: {
        base: '/static/base',
        plotly: 'https://cdn.plot.ly/plotly-2.12.1.min.js?noext',
    },
});

This way

  • Notebook correctly renders interactive diagrams when running in Jupyter
  • Notebook correctly renders interactive diagrams when viewed as static HTML files
  • Notebook correctly renders interactive diagrams when viewed on ReadTheDocs

See the example open-source documentation here and the example notebook.

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

Successfully merging a pull request may close this issue.

5 participants