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

Matplotlib figures are not shown on the compiled documentation. #788

Open
Vital-Fernandez opened this issue Apr 18, 2024 · 7 comments
Open

Comments

@Vital-Fernandez
Copy link

Hi I have been using nbsphinx for a couple of years for my library to compile the tutorials from notebooks.

I had to reinstall python and now the figures are not longer showed on the sphinx documentation (they show fine on the jupyter notebooks with %matplotlib widget).

If I run the command:

python -c "import sys; print('\n',sys.version); import ipympl; print('ipympl version:', ipympl.__version__)" && jupyter --version && jupyter nbextension list && jupyter labextension list

I get

ipympl version: 0.9.3
Selected Jupyter core packages...
IPython : 8.23.0
ipykernel : 6.29.4
ipywidgets : 8.1.2
jupyter_client : 8.6.1
jupyter_core : 5.7.2
jupyter_server : 2.13.0
jupyterlab : 4.1.6
nbclient : 0.10.0
nbconvert : 7.16.3
nbformat : 5.10.4
notebook : 7.1.2
qtconsole : 5.5.1
traitlets : 5.14.2
Known nbextensions:
config dir: /home/vital/anaconda3/etc/jupyter/nbconfig
notebook section
jupyter-matplotlib/extension enabled
- Validating: OK
nbextensions_configurator/config_menu/main enabled
- Validating: problems found:
- require? X nbextensions_configurator/config_menu/main
jupyterlab-plotly/extension enabled
- Validating: OK
pydeck/extension enabled
- Validating: problems found:
- require? X pydeck/extension
jupyter-js-widgets/extension enabled
- Validating: OK
contrib_nbextensions_help_item/main enabled
- Validating: OK
tree section
nbextensions_configurator/tree_tab/main enabled
- Validating: problems found:
- require? X nbextensions_configurator/tree_tab/main
JupyterLab v4.1.6
/home/vital/PycharmProjects/lime/venv/share/jupyter/labextensions
jupyter-matplotlib v0.11.3 enabled OK
jupyterlab_pygments v0.3.0 enabled OK (python, jupyterlab_pygments)
@jupyter-widgets/jupyterlab-manager v5.0.10 enabled OK (python, jupyterlab_widgets)
@jupyter-notebook/lab-extension v7.1.2 enabled OK

I am using ubuntu 22.04.04 with python 3.11 and these are some of the packages I have installed:

docutils 0.20.1
ipympl 0.9.3
ipython 8.23.0
jupyter 1.0.0
jupyter_client 8.6.1
jupyter-console 6.6.3
jupyter-contrib-core 0.4.2
jupyter-contrib-nbextensions 0.7.0
jupyter_core 5.7.2
jupyter-events 0.10.0
jupyter-highlight-selected-word 0.2.0
jupyter-lsp 2.2.5
jupyter-nbextensions-configurator 0.6.3
jupyter_server 2.13.0
jupyter_server_terminals 0.5.3
jupyterlab 4.1.6
jupyterlab_pygments 0.3.0
jupyterlab_server 2.26.0
jupyterlab_widgets 3.0.10
matplotlib 3.7.2
matplotlib-inline 0.1.6
mplcursors 0.5.2
nbclient 0.10.0
nbconvert 7.16.3
nbformat 5.10.4
nbsphinx 0.9.2
notebook 7.1.2
notebook_shim 0.2.4
numpy 1.24.4
pip 24.0
scipy 1.10.1
Sphinx 7.2.6
sphinx-rtd-theme 2.0.0
sphinxcontrib-applehelp 1.0.8
sphinxcontrib-devhelp 1.0.6
sphinxcontrib-htmlhelp 2.0.5
sphinxcontrib-jquery 4.1
sphinxcontrib-jsmath 1.0.1
sphinxcontrib-qthelp 1.0.7
sphinxcontrib-serializinghtml 1.1.10

Thank you very much for any advice.

@mgeier
Copy link
Member

mgeier commented May 5, 2024

Thanks for the report, can you please provide a minimal reproducible example?

I don't have any experience with the widget backend, I assume it worked before your re-installation?

@mgeier
Copy link
Member

mgeier commented May 5, 2024

I have created #794 to test this, and it seems to work. What is different in your case?

@vincefn
Copy link

vincefn commented May 25, 2024

Hi @mgeier - I'm facing the same issue. I have attached a zip file with
an example pre-processed notebook, the conf.py and index.rst to reproduce this, and the html produced directly by nbconvert.

Notes:

  • this happens only when the notebook is already rendered - if I let nbsphinx render the notebook, then the figure appears correctly
  • nbconvert correctly renders the page with the figure (using jupyter nbconvert --to html ipympl-lab-test.ipynb)
  • if you open the notebook in jupyter-lab, you'll probably see it it does not show the png rendering (instead Error displaying widget: model not found) - some issue in jupyter-lab not using the embedded png as an alternate representation of the graph. Since nbconvert works I expect it should also work with nbsphinx

The minimal test environment was created using mambaforge with just mamba create -n ipympl-lab python=3.12 ipympl matplotlib jupyterlab sphinx nbsphinx.

Here's the full list of the environment (here on a mac)
(ipympl-lab) vincent@vincent tmp % jupyter --version
Selected Jupyter core packages...
IPython          : 8.24.0
ipykernel        : 6.29.3
ipywidgets       : 8.1.2
jupyter_client   : 8.6.2
jupyter_core     : 5.7.2
jupyter_server   : 2.14.0
jupyterlab       : 4.2.1
nbclient         : 0.10.0
nbconvert        : 7.16.4
nbformat         : 5.10.4
notebook         : not installed
qtconsole        : not installed
traitlets        : 5.14.3
(ipympl-lab) vincent@vincent tmp % jupyter labextension list
JupyterLab v4.2.1
/Users/vincent/mambaforge/envs/ipympl-lab/share/jupyter/labextensions
        jupyterlab_pygments v0.3.0 enabled OK (python, jupyterlab_pygments)
        jupyter-matplotlib v0.11.4 enabled OK
        @jupyter-widgets/jupyterlab-manager v5.0.10 enabled OK (python, jupyterlab_widgets)

(ipympl-lab) vincent@vincent tmp % conda list
# packages in environment at /Users/vincent/mambaforge/envs/ipympl-lab:
#
# Name                    Version                   Build  Channel
alabaster                 0.7.16             pyhd8ed1ab_0    conda-forge
anyio                     4.3.0              pyhd8ed1ab_0    conda-forge
appnope                   0.1.4              pyhd8ed1ab_0    conda-forge
argon2-cffi               23.1.0             pyhd8ed1ab_0    conda-forge
argon2-cffi-bindings      21.2.0          py312h02f2b3b_4    conda-forge
arrow                     1.3.0              pyhd8ed1ab_0    conda-forge
asttokens                 2.4.1              pyhd8ed1ab_0    conda-forge
async-lru                 2.0.4              pyhd8ed1ab_0    conda-forge
attrs                     23.2.0             pyh71513ae_0    conda-forge
babel                     2.14.0             pyhd8ed1ab_0    conda-forge
beautifulsoup4            4.12.3             pyha770c72_0    conda-forge
bleach                    6.1.0              pyhd8ed1ab_0    conda-forge
brotli                    1.1.0                hb547adb_1    conda-forge
brotli-bin                1.1.0                hb547adb_1    conda-forge
brotli-python             1.1.0           py312h9f69965_1    conda-forge
bzip2                     1.0.8                h93a5062_5    conda-forge
ca-certificates           2024.2.2             hf0a4a13_0    conda-forge
cached-property           1.5.2                hd8ed1ab_1    conda-forge
cached_property           1.5.2              pyha770c72_1    conda-forge
certifi                   2024.2.2           pyhd8ed1ab_0    conda-forge
cffi                      1.16.0          py312h8e38eb3_0    conda-forge
charset-normalizer        3.3.2              pyhd8ed1ab_0    conda-forge
colorama                  0.4.6              pyhd8ed1ab_0    conda-forge
comm                      0.2.2              pyhd8ed1ab_0    conda-forge
contourpy                 1.2.1           py312h0fef576_0    conda-forge
cycler                    0.12.1             pyhd8ed1ab_0    conda-forge
debugpy                   1.8.1           py312h20a0b95_0    conda-forge
decorator                 5.1.1              pyhd8ed1ab_0    conda-forge
defusedxml                0.7.1              pyhd8ed1ab_0    conda-forge
docutils                  0.21.2             pyhd8ed1ab_0    conda-forge
entrypoints               0.4                pyhd8ed1ab_0    conda-forge
exceptiongroup            1.2.0              pyhd8ed1ab_2    conda-forge
executing                 2.0.1              pyhd8ed1ab_0    conda-forge
fonttools                 4.52.1          py312h7e5086c_0    conda-forge
fqdn                      1.5.1              pyhd8ed1ab_0    conda-forge
freetype                  2.12.1               hadb7bae_2    conda-forge
h11                       0.14.0             pyhd8ed1ab_0    conda-forge
h2                        4.1.0              pyhd8ed1ab_0    conda-forge
hpack                     4.0.0              pyh9f0ad1d_0    conda-forge
httpcore                  1.0.5              pyhd8ed1ab_0    conda-forge
httpx                     0.27.0             pyhd8ed1ab_0    conda-forge
hyperframe                6.0.1              pyhd8ed1ab_0    conda-forge
idna                      3.7                pyhd8ed1ab_0    conda-forge
imagesize                 1.4.1              pyhd8ed1ab_0    conda-forge
importlib-metadata        7.1.0              pyha770c72_0    conda-forge
importlib_metadata        7.1.0                hd8ed1ab_0    conda-forge
importlib_resources       6.4.0              pyhd8ed1ab_0    conda-forge
ipykernel                 6.29.3             pyh3cd1d5f_0    conda-forge
ipympl                    0.9.4              pyhd8ed1ab_0    conda-forge
ipython                   8.24.0             pyh707e725_0    conda-forge
ipython_genutils          0.2.0              pyhd8ed1ab_1    conda-forge
ipywidgets                8.1.2              pyhd8ed1ab_1    conda-forge
isoduration               20.11.0            pyhd8ed1ab_0    conda-forge
jedi                      0.19.1             pyhd8ed1ab_0    conda-forge
jinja2                    3.1.4              pyhd8ed1ab_0    conda-forge
json5                     0.9.25             pyhd8ed1ab_0    conda-forge
jsonpointer               2.4             py312h81bd7bf_3    conda-forge
jsonschema                4.22.0             pyhd8ed1ab_0    conda-forge
jsonschema-specifications 2023.12.1          pyhd8ed1ab_0    conda-forge
jsonschema-with-format-nongpl 4.22.0             pyhd8ed1ab_0    conda-forge
jupyter-lsp               2.2.5              pyhd8ed1ab_0    conda-forge
jupyter_client            8.6.2              pyhd8ed1ab_0    conda-forge
jupyter_core              5.7.2           py312h81bd7bf_0    conda-forge
jupyter_events            0.10.0             pyhd8ed1ab_0    conda-forge
jupyter_server            2.14.0             pyhd8ed1ab_0    conda-forge
jupyter_server_terminals  0.5.3              pyhd8ed1ab_0    conda-forge
jupyterlab                4.2.1              pyhd8ed1ab_0    conda-forge
jupyterlab_pygments       0.3.0              pyhd8ed1ab_1    conda-forge
jupyterlab_server         2.27.2             pyhd8ed1ab_0    conda-forge
jupyterlab_widgets        3.0.10             pyhd8ed1ab_0    conda-forge
kiwisolver                1.4.5           py312h389731b_1    conda-forge
krb5                      1.21.2               h92f50d5_0    conda-forge
lcms2                     2.16                 ha0e7c42_0    conda-forge
lerc                      4.0.0                h9a09cb3_0    conda-forge
libblas                   3.9.0           22_osxarm64_openblas    conda-forge
libbrotlicommon           1.1.0                hb547adb_1    conda-forge
libbrotlidec              1.1.0                hb547adb_1    conda-forge
libbrotlienc              1.1.0                hb547adb_1    conda-forge
libcblas                  3.9.0           22_osxarm64_openblas    conda-forge
libcxx                    17.0.6               h5f092b4_0    conda-forge
libdeflate                1.20                 h93a5062_0    conda-forge
libedit                   3.1.20191231         hc8eb9b7_2    conda-forge
libexpat                  2.6.2                hebf3989_0    conda-forge
libffi                    3.4.2                h3422bc3_5    conda-forge
libgfortran               5.0.0           13_2_0_hd922786_3    conda-forge
libgfortran5              13.2.0               hf226fd6_3    conda-forge
libjpeg-turbo             3.0.0                hb547adb_1    conda-forge
liblapack                 3.9.0           22_osxarm64_openblas    conda-forge
libopenblas               0.3.27          openmp_h6c19121_0    conda-forge
libpng                    1.6.43               h091b4b1_0    conda-forge
libsodium                 1.0.18               h27ca646_1    conda-forge
libsqlite                 3.45.3               h091b4b1_0    conda-forge
libtiff                   4.6.0                h07db509_3    conda-forge
libwebp-base              1.4.0                h93a5062_0    conda-forge
libxcb                    1.15                 hf346824_0    conda-forge
libzlib                   1.2.13               h53f4e23_5    conda-forge
llvm-openmp               18.1.5               hde57baf_0    conda-forge
markupsafe                2.1.5           py312he37b823_0    conda-forge
matplotlib                3.8.4           py312h1f38498_2    conda-forge
matplotlib-base           3.8.4           py312h4479663_2    conda-forge
matplotlib-inline         0.1.7              pyhd8ed1ab_0    conda-forge
mistune                   3.0.2              pyhd8ed1ab_0    conda-forge
munkres                   1.1.4              pyh9f0ad1d_0    conda-forge
nbclient                  0.10.0             pyhd8ed1ab_0    conda-forge
nbconvert                 7.16.4               hd8ed1ab_0    conda-forge
nbconvert-core            7.16.4             pyhd8ed1ab_0    conda-forge
nbconvert-pandoc          7.16.4               hd8ed1ab_0    conda-forge
nbformat                  5.10.4             pyhd8ed1ab_0    conda-forge
nbsphinx                  0.9.4              pyhd8ed1ab_0    conda-forge
ncurses                   6.5                  hb89a1cb_0    conda-forge
nest-asyncio              1.6.0              pyhd8ed1ab_0    conda-forge
notebook-shim             0.2.4              pyhd8ed1ab_0    conda-forge
numpy                     1.26.4          py312h8442bc7_0    conda-forge
openjpeg                  2.5.2                h9f1df11_0    conda-forge
openssl                   3.3.0                hfb2fe0b_3    conda-forge
overrides                 7.7.0              pyhd8ed1ab_0    conda-forge
packaging                 24.0               pyhd8ed1ab_0    conda-forge
pandoc                    3.2                  hce30654_0    conda-forge
pandocfilters             1.5.0              pyhd8ed1ab_0    conda-forge
parso                     0.8.4              pyhd8ed1ab_0    conda-forge
pexpect                   4.9.0              pyhd8ed1ab_0    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    10.3.0          py312h8a801b1_0    conda-forge
pip                       24.0               pyhd8ed1ab_0    conda-forge
pkgutil-resolve-name      1.3.10             pyhd8ed1ab_1    conda-forge
platformdirs              4.2.2              pyhd8ed1ab_0    conda-forge
prometheus_client         0.20.0             pyhd8ed1ab_0    conda-forge
prompt-toolkit            3.0.42             pyha770c72_0    conda-forge
psutil                    5.9.8           py312he37b823_0    conda-forge
pthread-stubs             0.4               h27ca646_1001    conda-forge
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pure_eval                 0.2.2              pyhd8ed1ab_0    conda-forge
pycparser                 2.22               pyhd8ed1ab_0    conda-forge
pygments                  2.18.0             pyhd8ed1ab_0    conda-forge
pyobjc-core               10.2            py312h9d22092_0    conda-forge
pyobjc-framework-cocoa    10.2            py312h9d22092_0    conda-forge
pyparsing                 3.1.2              pyhd8ed1ab_0    conda-forge
pysocks                   1.7.1              pyha2e5f31_6    conda-forge
python                    3.12.3          h4a7b5fc_0_cpython    conda-forge
python-dateutil           2.9.0              pyhd8ed1ab_0    conda-forge
python-fastjsonschema     2.19.1             pyhd8ed1ab_0    conda-forge
python-json-logger        2.0.7              pyhd8ed1ab_0    conda-forge
python_abi                3.12                    4_cp312    conda-forge
pytz                      2024.1             pyhd8ed1ab_0    conda-forge
pyyaml                    6.0.1           py312h02f2b3b_1    conda-forge
pyzmq                     26.0.3          py312hfa13136_0    conda-forge
readline                  8.2                  h92ec313_1    conda-forge
referencing               0.35.1             pyhd8ed1ab_0    conda-forge
requests                  2.32.2             pyhd8ed1ab_0    conda-forge
rfc3339-validator         0.1.4              pyhd8ed1ab_0    conda-forge
rfc3986-validator         0.1.1              pyh9f0ad1d_0    conda-forge
rpds-py                   0.18.1          py312h552d48e_0    conda-forge
send2trash                1.8.3              pyh31c8845_0    conda-forge
setuptools                70.0.0             pyhd8ed1ab_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
sniffio                   1.3.1              pyhd8ed1ab_0    conda-forge
snowballstemmer           2.2.0              pyhd8ed1ab_0    conda-forge
soupsieve                 2.5                pyhd8ed1ab_1    conda-forge
sphinx                    7.3.7              pyhd8ed1ab_0    conda-forge
sphinxcontrib-applehelp   1.0.8              pyhd8ed1ab_0    conda-forge
sphinxcontrib-devhelp     1.0.6              pyhd8ed1ab_0    conda-forge
sphinxcontrib-htmlhelp    2.0.5              pyhd8ed1ab_0    conda-forge
sphinxcontrib-jsmath      1.0.1              pyhd8ed1ab_0    conda-forge
sphinxcontrib-qthelp      1.0.7              pyhd8ed1ab_0    conda-forge
sphinxcontrib-serializinghtml 1.1.10             pyhd8ed1ab_0    conda-forge
stack_data                0.6.2              pyhd8ed1ab_0    conda-forge
terminado                 0.18.1             pyh31c8845_0    conda-forge
tinycss2                  1.3.0              pyhd8ed1ab_0    conda-forge
tk                        8.6.13               h5083fa2_1    conda-forge
tomli                     2.0.1              pyhd8ed1ab_0    conda-forge
tornado                   6.4             py312he37b823_0    conda-forge
traitlets                 5.14.3             pyhd8ed1ab_0    conda-forge
types-python-dateutil     2.9.0.20240316     pyhd8ed1ab_0    conda-forge
typing-extensions         4.11.0               hd8ed1ab_0    conda-forge
typing_extensions         4.11.0             pyha770c72_0    conda-forge
typing_utils              0.1.0              pyhd8ed1ab_0    conda-forge
tzdata                    2024a                h0c530f3_0    conda-forge
uri-template              1.3.0              pyhd8ed1ab_0    conda-forge
urllib3                   2.2.1              pyhd8ed1ab_0    conda-forge
wcwidth                   0.2.13             pyhd8ed1ab_0    conda-forge
webcolors                 1.13               pyhd8ed1ab_0    conda-forge
webencodings              0.5.1              pyhd8ed1ab_2    conda-forge
websocket-client          1.8.0              pyhd8ed1ab_0    conda-forge
wheel                     0.43.0             pyhd8ed1ab_1    conda-forge
widgetsnbextension        4.0.10             pyhd8ed1ab_0    conda-forge
xorg-libxau               1.0.11               hb547adb_0    conda-forge
xorg-libxdmcp             1.1.3                h27ca646_0    conda-forge
xz                        5.2.6                h57fd34a_0    conda-forge
yaml                      0.2.5                h3422bc3_2    conda-forge
zeromq                    4.3.5                hcc0f68c_4    conda-forge
zipp                      3.17.0             pyhd8ed1ab_0    conda-forge
zstd                      1.5.6                hb46c0d2_0    conda-forge

@vincefn
Copy link

vincefn commented May 25, 2024

A followup: after a few more tests, there is an option "save widget state automatically" in jupyter-lab:

image

Once this is activated this seems to change something in the saved processed notebook, and the rendering is then correct using both nbsphinx and nbconvert, and also when re-opening the notebook in jupyter-lab.

Maybe @Vital-Fernandez can confirm if this also solves the issue for him.

@mgeier
Copy link
Member

mgeier commented May 26, 2024

Yes, the widget state needs to be available for the widget to be shown in the HTML page.

Do you want to add this to the documentation, maybe at https://nbsphinx.readthedocs.io/en/0.9.4/code-cells.html#Troubleshooting?

nbconvert correctly renders the page with the figure (using jupyter nbconvert --to html ipympl-lab-test.ipynb)

I'm still wondering why nbconvert shows the plot even without widget state.

@vincefn
Copy link

vincefn commented May 27, 2024

I think it would be useful to document this - it took me a little while to find this 'widget state' option. Not sure when this was introduced.

I could not see the difference between the notebook saved with and without. As to how nbconvert manages to render -no idea, I guess there should be a way to catch an error and switch to the png automatically, but that's beyond my area of expertise.

@mgeier
Copy link
Member

mgeier commented Aug 13, 2024

@vincefn

I think it would be useful to document this

Can you please make a PR?

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