Matplotlib pane throwing AttributeError when interactive=True

I have a panel app utilizing Matplotlib panes.
It works fine in one environment but not in another.
It throws an AttributeError as get_ipython() returns None instead of an IPython instance - as if the IPython is not launched correctly.

Here is a minimum example:

import panel as pn
from matplotlib import pyplot as plt

fig, ax = plt.subplots()
ax.plot([0,1,2], [2,2,4])
pn.pane.plot.Matplotlib(fig, interactive=True).show()

I have created the conda environment via requirements:

matplotlib
holoviz
ipympl
ipywidgets-bokeh

Any suggestions?

My conda list:

# Name                    Version                   Build  Channel
argon2-cffi               20.1.0           py38h294d835_2    conda-forge
async_generator           1.10                       py_0    conda-forge
attrs                     21.2.0             pyhd8ed1ab_0    conda-forge
backcall                  0.2.0              pyh9f0ad1d_0    conda-forge
backports                 1.0                        py_2    conda-forge
backports.functools_lru_cache 1.6.4              pyhd8ed1ab_0    conda-forge
bleach                    4.1.0              pyhd8ed1ab_0    conda-forge
bokeh                     2.4.0            py38haa244fe_0    conda-forge
brotlipy                  0.7.0           py38h294d835_1001    conda-forge
bzip2                     1.0.8                h8ffe710_4    conda-forge
ca-certificates           2021.5.30            h5b45459_0    conda-forge
certifi                   2021.5.30        py38haa244fe_0    conda-forge
cffi                      1.14.6           py38hd8c33c5_1    conda-forge
cftime                    1.5.1            py38h6f4d8f0_0    conda-forge
chardet                   4.0.0            py38haa244fe_1    conda-forge
charset-normalizer        2.0.0              pyhd8ed1ab_0    conda-forge
click                     8.0.1            py38haa244fe_0    conda-forge
cloudpickle               2.0.0              pyhd8ed1ab_0    conda-forge
colorama                  0.4.4              pyh9f0ad1d_0    conda-forge
colorcet                  2.0.6                      py_0    pyviz
cryptography              3.4.7            py38hd7da0ea_0    conda-forge
curl                      7.79.1               h789b8ee_1    conda-forge
cycler                    0.10.0                     py_2    conda-forge
cytoolz                   0.11.0           py38h294d835_3    conda-forge
dask                      2021.9.1           pyhd8ed1ab_0    conda-forge
dask-core                 2021.9.1           pyhd8ed1ab_0    conda-forge
datashader                0.13.0                     py_0    pyviz
datashape                 0.5.4                      py_1    conda-forge
debugpy                   1.4.1            py38h885f38d_0    conda-forge
decorator                 5.1.0              pyhd8ed1ab_0    conda-forge
defusedxml                0.7.1              pyhd8ed1ab_0    conda-forge
distributed               2021.9.1         py38haa244fe_0    conda-forge
entrypoints               0.3             pyhd8ed1ab_1003    conda-forge
fastparquet               0.4.0            py38h40bdaab_0    pyviz
freetype                  2.10.4               h546665d_1    conda-forge
fsspec                    2021.10.0          pyhd8ed1ab_0    conda-forge
hdf4                      4.2.15               h0e5069d_3    conda-forge
hdf5                      1.12.1          nompi_h2a0e4a3_101    conda-forge
heapdict                  1.0.1                      py_0    conda-forge
holoviews                 1.14.6                     py_0    pyviz
holoviz                   0.13.0                     py_0    pyviz
hvplot                    0.7.3                      py_0    pyviz
icu                       68.1                 h0e60522_0    conda-forge
idna                      3.1                pyhd3deb0d_0    conda-forge
importlib-metadata        4.8.1            py38haa244fe_0    conda-forge
intel-openmp              2021.3.0          h57928b3_3372    conda-forge
ipykernel                 6.4.1            py38h595d716_0    conda-forge
ipympl                    0.8.0              pyhd8ed1ab_0    conda-forge
ipython                   7.28.0           py38h595d716_0    conda-forge
ipython_genutils          0.2.0                      py_1    conda-forge
ipywidgets                7.6.5              pyhd8ed1ab_0    conda-forge
ipywidgets-bokeh          1.2.1                    pypi_0    pypi
jbig                      2.1               h8d14728_2003    conda-forge
jedi                      0.18.0           py38haa244fe_2    conda-forge
jinja2                    3.0.1              pyhd8ed1ab_0    conda-forge
jpeg                      9d                   h8ffe710_0    conda-forge
jsonschema                4.0.1              pyhd8ed1ab_0    conda-forge
jupyter_client            7.0.6              pyhd8ed1ab_0    conda-forge
jupyter_core              4.8.1            py38haa244fe_0    conda-forge
jupyterlab_pygments       0.1.2              pyh9f0ad1d_0    conda-forge
jupyterlab_widgets        1.0.2              pyhd8ed1ab_0    conda-forge
kiwisolver                1.3.2            py38hbd9d945_0    conda-forge
krb5                      1.19.2               hbae68bd_2    conda-forge
lcms2                     2.12                 h2a16943_0    conda-forge
lerc                      2.2.1                h0e60522_0    conda-forge
libblas                   3.9.0              11_win64_mkl    conda-forge
libcblas                  3.9.0              11_win64_mkl    conda-forge
libclang                  11.1.0          default_h5c34c98_1    conda-forge
libcurl                   7.79.1               h789b8ee_1    conda-forge
libdeflate                1.7                  h8ffe710_5    conda-forge
liblapack                 3.9.0              11_win64_mkl    conda-forge
libnetcdf                 4.8.1           nompi_h1cc8e9d_101    conda-forge
libpng                    1.6.37               h1d00b33_2    conda-forge
libsodium                 1.0.18               h8d14728_1    conda-forge
libssh2                   1.10.0               h680486a_2    conda-forge
libtiff                   4.3.0                h0c97f57_1    conda-forge
libzip                    1.8.0                hfed4ece_1    conda-forge
libzlib                   1.2.11            h8ffe710_1013    conda-forge
llvmlite                  0.37.0           py38h57a6900_0    conda-forge
locket                    0.2.0                      py_2    conda-forge
lz4-c                     1.9.3                h8ffe710_1    conda-forge
m2w64-gcc-libgfortran     5.3.0                         6    conda-forge
m2w64-gcc-libs            5.3.0                         7    conda-forge
m2w64-gcc-libs-core       5.3.0                         7    conda-forge
m2w64-gmp                 6.1.0                         2    conda-forge
m2w64-libwinpthread-git   5.0.0.4634.697f757               2    conda-forge
markdown                  3.3.4              pyhd8ed1ab_0    conda-forge
markupsafe                2.0.1            py38h294d835_0    conda-forge
matplotlib                3.4.3            py38haa244fe_1    conda-forge
matplotlib-base           3.4.3            py38h1f000d6_1    conda-forge
matplotlib-inline         0.1.3              pyhd8ed1ab_0    conda-forge
mistune                   0.8.4           py38h294d835_1004    conda-forge
mkl                       2021.3.0           hb70f87d_564    conda-forge
msgpack-python            1.0.2            py38hbd9d945_1    conda-forge
msys2-conda-epoch         20160418                      1    conda-forge
multipledispatch          0.6.0                      py_0    conda-forge
nbclient                  0.5.4              pyhd8ed1ab_0    conda-forge
nbconvert                 6.2.0            py38haa244fe_0    conda-forge
nbformat                  5.1.3              pyhd8ed1ab_0    conda-forge
nest-asyncio              1.5.1              pyhd8ed1ab_0    conda-forge
netcdf4                   1.5.7           nompi_py38h0500770_103    conda-forge
notebook                  6.4.4              pyha770c72_0    conda-forge
numba                     0.54.0           py38h5858985_0    conda-forge
numpy                     1.20.3           py38h09042cb_0    conda-forge
olefile                   0.46               pyh9f0ad1d_1    conda-forge
openjpeg                  2.4.0                hb211442_1    conda-forge
openssl                   1.1.1l               h8ffe710_0    conda-forge
packaging                 21.0               pyhd8ed1ab_0    conda-forge
pandas                    1.3.3            py38h5d928e2_0    conda-forge
pandoc                    2.14.2               h8ffe710_0    conda-forge
pandocfilters             1.5.0              pyhd8ed1ab_0    conda-forge
panel                     0.12.4                     py_0    pyviz
param                     1.11.1                     py_0    pyviz
parso                     0.8.2              pyhd8ed1ab_0    conda-forge
partd                     1.2.0              pyhd8ed1ab_0    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    8.3.2            py38h794f750_0    conda-forge
pip                       21.2.4             pyhd8ed1ab_0    conda-forge
prometheus_client         0.11.0             pyhd8ed1ab_0    conda-forge
prompt-toolkit            3.0.20             pyha770c72_0    conda-forge
psutil                    5.8.0            py38h294d835_1    conda-forge
pycparser                 2.20               pyh9f0ad1d_2    conda-forge
pyct                      0.4.8                      py_0    pyviz
pyct-core                 0.4.8                      py_0    pyviz
pygments                  2.10.0             pyhd8ed1ab_0    conda-forge
pyopenssl                 21.0.0             pyhd8ed1ab_0    conda-forge
pyparsing                 2.4.7              pyh9f0ad1d_0    conda-forge
pyqt                      5.12.3           py38haa244fe_7    conda-forge
pyqt-impl                 5.12.3           py38h885f38d_7    conda-forge
pyqt5-sip                 4.19.18          py38h885f38d_7    conda-forge
pyqtchart                 5.12             py38h885f38d_7    conda-forge
pyqtwebengine             5.12.1           py38h885f38d_7    conda-forge
pyrsistent                0.17.3           py38h294d835_2    conda-forge
pysocks                   1.7.1            py38haa244fe_3    conda-forge
python                    3.8.12          h7840368_1_cpython    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python-snappy             0.6.0            py38h8566a25_0    conda-forge
python_abi                3.8                      2_cp38    conda-forge
pytz                      2021.3             pyhd8ed1ab_0    conda-forge
pyviz_comms               2.1.0                      py_0    pyviz
pywin32                   301              py38h294d835_0    conda-forge
pywinpty                  1.1.4            py38hd3f51b4_0    conda-forge
pyyaml                    5.4.1            py38h294d835_1    conda-forge
pyzmq                     22.3.0           py38h09162b1_0    conda-forge
qt                        5.12.9               h5909a2a_4    conda-forge
requests                  2.26.0             pyhd8ed1ab_0    conda-forge
rise                      5.7.1            py38haa244fe_0    conda-forge
scipy                     1.7.1            py38ha1292f7_0    conda-forge
send2trash                1.8.0              pyhd8ed1ab_0    conda-forge
setuptools                58.2.0           py38haa244fe_0    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
snappy                    1.1.8                ha925a31_3    conda-forge
sortedcontainers          2.4.0              pyhd8ed1ab_0    conda-forge
sqlite                    3.36.0               h8ffe710_2    conda-forge
tbb                       2021.3.0             h2d74725_0    conda-forge
tblib                     1.7.0              pyhd8ed1ab_0    conda-forge
terminado                 0.12.1           py38haa244fe_0    conda-forge
testpath                  0.5.0              pyhd8ed1ab_0    conda-forge
thrift                    0.11.0          py38h6538335_1001    conda-forge
tk                        8.6.11               h8ffe710_1    conda-forge
toolz                     0.11.1                     py_0    conda-forge
tornado                   6.1              py38h294d835_1    conda-forge
tqdm                      4.62.3             pyhd8ed1ab_0    conda-forge
traitlets                 5.1.0              pyhd8ed1ab_0    conda-forge
typing_extensions         3.10.0.2           pyha770c72_0    conda-forge
ucrt                      10.0.20348.0         h57928b3_0    conda-forge
urllib3                   1.26.7             pyhd8ed1ab_0    conda-forge
vc                        14.2                 hb210afc_5    conda-forge
vs2015_runtime            14.29.30037          h902a5da_5    conda-forge
wcwidth                   0.2.5              pyh9f0ad1d_2    conda-forge
webencodings              0.5.1                      py_1    conda-forge
wheel                     0.37.0             pyhd8ed1ab_1    conda-forge
widgetsnbextension        3.5.1            py38haa244fe_4    conda-forge
win_inet_pton             1.1.0            py38haa244fe_2    conda-forge
winpty                    0.4.3                         4    conda-forge
xarray                    0.19.0             pyhd8ed1ab_1    conda-forge
xz                        5.2.5                h62dcd97_1    conda-forge
yaml                      0.2.5                he774522_0    conda-forge
zeromq                    4.3.4                h0e60522_1    conda-forge
zict                      2.0.0                      py_0    conda-forge
zipp                      3.6.0              pyhd8ed1ab_0    conda-forge
zlib                      1.2.11            h8ffe710_1013    conda-forge
zstd                      1.5.0                h6255e5f_0    conda-forge

This is the error I get

Traceback (most recent call last):
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/tornado/web.py", line 1704, in _execute
    result = await result
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/panel/io/server.py", line 214, in get
    session = await self.get_session()
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/bokeh/server/views/session_handler.py", line 144, in get_session
    session = await self.application_context.create_session_if_needed(session_id, self.request, token)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/bokeh/server/contexts.py", line 243, in create_session_if_needed
    self._application.initialize_document(doc)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/panel/io/server.py", line 173, in initialize_document
    super().initialize_document(doc)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/bokeh/application/application.py", line 195, in initialize_document
    h.modify_document(doc)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/bokeh/application/handlers/function.py", line 143, in modify_document
    self._func(doc)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/panel/io/server.py", line 82, in _eval_panel
    doc = as_panel(panel)._modify_doc(server_id, title, doc, location)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/panel/viewable.py", line 244, in _modify_doc
    return self.server_doc(doc, title, location)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/panel/viewable.py", line 794, in server_doc
    model = self.get_root(doc)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/panel/pane/base.py", line 267, in get_root
    root = self._get_model(doc, comm=comm)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/panel/pane/plot.py", line 192, in _get_model
    manager = self._get_widget(self.object)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/panel/pane/plot.py", line 170, in _get_widget
    from ipympl.backend_nbagg import FigureManager, Canvas, is_interactive
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/ipympl/__init__.py", line 27, in <module>
    matplotlib.use('module://ipympl.backend_nbagg')
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/matplotlib/__init__.py", line 1080, in use
    plt.switch_backend(name)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/matplotlib/pyplot.py", line 277, in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/matplotlib/pyplot.py", line 278, in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/jovyan/conda-envs/pmat/lib/python3.9/site-packages/ipympl/backend_nbagg.py", line 431, in <module>
    ip.events.register('post_execute', flush_figures)
AttributeError: 'NoneType' object has no attribute 'events'

When running python script.py in a new environment created using

conda create --name=pmat -c conda-forge -c bokeh -c pyviz matplotlib holoviz ipympl ipywidgets_bokeh

I’ve tried to downgrade to Ipykernel<6.0 (6.0 is known to not work with ipywidgets_bokeh yet. There should be a bug fix in the master branch already) and panel 0.12.1 and bokeh 2.3.3.

I would recommend reporting it as a bug.

Thanks @Marc, Would that be a bug in ipywidgets-bokeh or ipykernel?

I dont think it’s ipykernel. But it could also be Panel.

Where should this be reported @philippjfr ?

Honestly dislike the fact that you register events on the global get_ipython() object a lot. That said it looks like this has already been addressed in ipympl here: https://github.com/matplotlib/ipympl/pull/364

But do file an issue in ipywidgets_bokeh to see if we can somehow provide a shim for the get_ipython global and register events like this.

Thanks Phillip. I agree that the registering the global get_ipython() is not optimal. If there are more MatplotlibPanes in a Panel app, events generated in one of them will be broadcast to all of them, so the user needs to implement a custom filter to only process the events that are relevant for a given MatplotlibPane.

I tried applying the few lines of code changed in the pull request you mentioned. It does indeed fix the first attribute error, but then the next exception arises:

ERROR:fake:Invalid Message
Traceback (most recent call last):
  File "C:\Users\nchvg\Miniconda3\envs\optisoil-engineering-tools\lib\site-packages\ipykernel\kernelbase.py", line 312, in dispatch_shell
    msg = self.session.deserialize(msg, content=True, copy=False)
  File "C:\Users\nchvg\Miniconda3\envs\optisoil-engineering-tools\lib\site-packages\jupyter_client\session.py", line 1048, in deserialize
    raise ValueError("Duplicate Signature: %r" % signature)
ValueError: Duplicate Signature: b'9b25b08aacbdc8c77ef9f91368742482787dd5afe83a7a6872bb7f3e1366b335'

In addition the ipywidgets_bokeh source doesn’t mention get_ipython() in the source code, so I wonder if it is indeed the right place to file the issue? (If it is, I will happily do it)

Thanks, I’ve seen those duplicate signature errors before as will. I think they occur if the same message is processed multiple times. Also, yes, it is the right place. ipywidgets_bokeh tries to emulate a Jupyter kernel in a Bokeh server context and I would consider get_ipython to be part of that.

I have narrowed down the problematic versions, ie this is the highest combination of requirements where neither the Attribute error or duplicate signature are raised:

  - pip:
    - ipykernel<6
    - ipympl==0.7.0
    - panel<=0.12.1

If any of these three packages are upgraded, the minimal example fails…

2 Likes