matplotlib / ipympl

Matplotlib Jupyter Integration
https://matplotlib.org/ipympl/
BSD 3-Clause "New" or "Revised" License
1.59k stars 225 forks source link

ipympl support for classic Jupyter notebook (6.5.2-6.5.4) #525

Open pythonic2020 opened 1 year ago

pythonic2020 commented 1 year ago

While current ipympl (0.9.3) works perfectly in Jupyterlab, it no longer works properly in a classic Jupyter notebook 6.5.2 from conda-forge repo. Has classic notebook support for ipympl been abandoned?

ianhi commented 1 year ago

@pythonic2020 can you please post the output of the command in the bug report: python -c "import sys; print('\n',sys.version); import ipympl; print('ipympl version:', ipympl.__version__)" && jupyter --version && jupyter nbextension list && jupyter labextension list

When I make a new environment with mamba create -n test -c conda-forge notebook ipympl it works in notebook so I suspect something is wrong in your environment.

pythonic2020 commented 1 year ago

Note that I just updated my conda env to notebook 6.5.3 today.

3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:28:38) [MSC v.1929 64 bit (AMD64)] ipympl version: 0.9.3 Selected Jupyter core packages... IPython : 8.11.0 ipykernel : 6.21.3 ipywidgets : 8.0.4 jupyter_client : 8.0.3 jupyter_core : 5.2.0 jupyter_server : 2.4.0 jupyterlab : 3.6.1 nbclient : 0.7.2 nbconvert : 7.2.9 nbformat : 5.7.3 notebook : 6.5.3 qtconsole : not installed traitlets : 5.9.0 Known nbextensions: config dir: C:\Users\me\Miniconda3\envs\forge39\etc\jupyter\nbconfig notebook section jupyter-matplotlib/extension enabled jupyter_bokeh/extension enabled jupyterlab-plotly/extension enabled jupyter-js-widgets/extension enabled

  - Validating: ok
  - Validating: ok
  - Validating: ok
  - Validating: ok

JupyterLab v3.6.1 C:\Users\me\Miniconda3\envs\forge39\share\jupyter\labextensions jupyter-matplotlib v0.11.3 enabled ok jupyterlab-plotly v5.13.1 enabled ok jupyterlab_pygments v0.2.2 enabled ok (python, jupyterlab_pygments) @bokeh/jupyter_bokeh v3.0.5 enabled ok (python, jupyter_bokeh) @jupyter-widgets/jupyterlab-manager v5.0.5 enabled ok (python, jupyterlab_widgets)

There is no error, but the kernel becomes intermittent when I run ipympl-related 3d interactive plotting in classic notebook 6.5.3 (see example code below from ipympl docs). Rotating the plot with the mouse works for a while, then the kernel breaks, after which interactive functions will not work.

%matplotlib widget

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)

# Plot a basic wireframe.
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()
ianhi commented 1 year ago

then the kernel breaks, after which interactive functions will not work.

Are there any error messages printed to the terminal you started the notebook from? Also can you check if there are error messages in the browser console: https://balsamiq.com/support/faqs/browserconsole/

pythonic2020 commented 1 year ago
AssertionError
[I 04:11:31.982 NotebookApp] Starting buffering for 17a5fa29-bfdb-4a64-9b5c-03f2051ce9b8:f23f5821d4cd48d8a8ee97b5bed74ea2
Task exception was never retrieved
future: <Task finished name='Task-268' coro=<WebSocketProtocol13.write_message.<locals>.wrapper() done, defined at C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\websocket.py:1090> exception=WebSocketClosedError()>
Traceback (most recent call last):
  File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\websocket.py", line 1092, in wrapper
    await fut
tornado.iostream.StreamClosedError: Stream is closed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\me\Miniconda3\envs\forge39\lib\asyncio\tasks.py", line 256, in __step
    result = coro.send(None)
  File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\websocket.py", line 1094, in wrapper
    raise WebSocketClosedError()
tornado.websocket.WebSocketClosedError

Then the connection restarts:

[I 04:11:32.998 NotebookApp] Restoring connection for 17a5fa29-bfdb-4a64-9b5c-03f2051ce9b8:f23f5821d4cd48d8a8ee97b5bed74ea2

I have tornado 6.2 installed:

#
# Name                    Version                   Build  Channel
tornado                   6.2              py39ha55989b_1    conda-forge
pyzmq                     25.0.0           py39hea35a22_0    conda-forge

Second run:

[E 04:17:52.984 NotebookApp] Uncaught exception, closing connection.
    Traceback (most recent call last):
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 702, in _handle_events
        self._handle_write()
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 976, in _handle_write
        self._write_buffer.advance(num_bytes)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 182, in advance
        assert 0 < size <= self._size
    AssertionError
Exception in callback None()
handle: <Handle cancelled>
Traceback (most recent call last):
  File "C:\Users\me\Miniconda3\envs\forge39\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\platform\asyncio.py", line 206, in _handle_events
    handler_func(fileobj, events)
  File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 702, in _handle_events
    self._handle_write()
  File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 976, in _handle_write
    self._write_buffer.advance(num_bytes)
  File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 182, in advance
    assert 0 < size <= self._size
AssertionError
[W 04:17:52.988 NotebookApp] zmq message arrived on closed channel
[W 04:17:52.991 NotebookApp] zmq message arrived on closed channel
[I 04:17:52.991 NotebookApp] Starting buffering for 17a5fa29-bfdb-4a64-9b5c-03f2051ce9b8:f23f5821d4cd48d8a8ee97b5bed74ea2
[W 04:17:52.993 NotebookApp] zmq message arrived on closed channel

There are no pertinent messages in Firefox browser console.

Third run, kernel really quits this time, will not restart:

[E 04:24:37.686 NotebookApp] Uncaught exception in ZMQStream callback
    Traceback (most recent call last):
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\zmq\eventloop\zmqstream.py", line 584, in _run_callback
        f = callback(*args, **kwargs)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\zmq\eventloop\zmqstream.py", line 308, in stream_callback
        return callback(self, msg)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\notebook\services\kernels\handlers.py", line 572, in _on_zmq_reply
        super()._on_zmq_reply(stream, msg)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\notebook\base\zmqhandlers.py", line 256, in _on_zmq_reply
        self.write_message(msg, binary=isinstance(msg, bytes))
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\websocket.py", line 339, in write_message
        return self.ws_connection.write_message(message, binary=binary)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\websocket.py", line 1086, in write_message
        fut = self._write_frame(True, opcode, message, flags=flags)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\websocket.py", line 1061, in _write_frame
        return self.stream.write(frame)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 546, in write
        self._handle_write()
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 976, in _handle_write
        self._write_buffer.advance(num_bytes)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 182, in advance
        assert 0 < size <= self._size
    AssertionError
[E 04:24:37.688 NotebookApp] Uncaught exception in zmqstream callback
    Traceback (most recent call last):
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\zmq\eventloop\zmqstream.py", line 621, in _handle_events
        self._handle_recv()
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\zmq\eventloop\zmqstream.py", line 650, in _handle_recv
        self._run_callback(callback, msg)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\zmq\eventloop\zmqstream.py", line 584, in _run_callback
        f = callback(*args, **kwargs)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\zmq\eventloop\zmqstream.py", line 308, in stream_callback
        return callback(self, msg)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\notebook\services\kernels\handlers.py", line 572, in _on_zmq_reply
        super()._on_zmq_reply(stream, msg)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\notebook\base\zmqhandlers.py", line 256, in _on_zmq_reply
        self.write_message(msg, binary=isinstance(msg, bytes))
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\websocket.py", line 339, in write_message
        return self.ws_connection.write_message(message, binary=binary)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\websocket.py", line 1086, in write_message
        fut = self._write_frame(True, opcode, message, flags=flags)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\websocket.py", line 1061, in _write_frame
        return self.stream.write(frame)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 546, in write
        self._handle_write()
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 976, in _handle_write
        self._write_buffer.advance(num_bytes)
      File "C:\Users\me\Miniconda3\envs\forge39\lib\site-packages\tornado\iostream.py", line 182, in advance
        assert 0 < size <= self._size
pythonic2020 commented 1 year ago

If I create a new, fresh conda-forge based Python 3.9 env and install just matplotlib, notebook, and ipympl (and required dependencies), the issue still occurs.

ianhi commented 1 year ago

Those errors seem to be upstream of ipympl. Can you try this code:

from ipywidgets import IntSlider

slider = IntSlider()
display(slider)

If I create a new, fresh conda-forge based Python 3.9 env and install just matplotlib, notebook, and ipympl (and required dependencies), the issue still occurs.

Can you post the exact command you used to make the new env

pythonic2020 commented 1 year ago

Your IntSlider code above works perfectly in a classic notebook.

Env creation:

conda create -n ipympl39 -c conda-forge python=3.9
conda activate ipympl39 
conda config --env --set channel_priority flexible
conda config --env --add channels conda-forge
conda config --env --show channels
conda config --env --show channel_priority
conda update --all
conda install matplotlib notebook ipympl

After the above env creation and package installation, all packages are from conda-forge repo.

Output of the info command in the new env (jupyterlab not installed):

3.9.16 | packaged by conda-forge | (main, Feb  1 2023, 21:28:38) [MSC v.1929 64 bit (AMD64)]
ipympl version: 0.9.3
Selected Jupyter core packages...
IPython          : 8.11.0
ipykernel        : 6.21.3
ipywidgets       : 8.0.4
jupyter_client   : 8.0.3
jupyter_core     : 5.2.0
jupyter_server   : 2.4.0
jupyterlab       : not installed
nbclient         : 0.7.2
nbconvert        : 7.2.9
nbformat         : 5.7.3
notebook         : 6.5.3
qtconsole        : not installed
traitlets        : 5.9.0
Known nbextensions:
  config dir: C:\Users\me\Miniconda3\envs\ipympl39\etc\jupyter\nbconfig
    notebook section
      jupyter-matplotlib/extension enabled 
      jupyter-js-widgets/extension enabled 

      - Validating: ok
      - Validating: ok
usage: jupyter [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir]
               [--paths] [--json] [--debug]
               [subcommand]

Jupyter: Interactive Computing

positional arguments:
  subcommand     the subcommand to launch

optional arguments:
  -h, --help     show this help message and exit
  --version      show the versions of core jupyter packages and exit
  --config-dir   show Jupyter config dir
  --data-dir     show Jupyter data dir
  --runtime-dir  show Jupyter runtime dir
  --paths        show all Jupyter paths. Add --json for machine-readable
                 format.
  --json         output paths as machine-readable json
  --debug        output debug information about paths

Available subcommands: bundlerextension dejavu events execute kernel
kernelspec migrate nbclassic nbclassic.exe.c~ nbconvert nbextension notebook
run script server serverextension troubleshoot trust

Jupyter command `jupyter-labextension` not found.
pythonic2020 commented 1 year ago

I tried the 3d code in my new env in a classic notebook in MS Edge, and got these Jupyter terminal errors. Note "AttributeError: 'NoneType' object has no attribute 'advance'".

[E 09:37:21.655 NotebookApp] Exception in callback functools.partial(<function ZMQStream._update_handler.<locals>.<lambda> at 0x0000026BA5D67C10>)
    Traceback (most recent call last):
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\tornado\ioloop.py", line 740, in _run_callback
        ret = callback()
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\zmq\eventloop\zmqstream.py", line 705, in <lambda>
        self.io_loop.add_callback(lambda: self._handle_events(self.socket, 0))
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\zmq\eventloop\zmqstream.py", line 621, in _handle_events
        self._handle_recv()
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\zmq\eventloop\zmqstream.py", line 650, in _handle_recv
        self._run_callback(callback, msg)
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\zmq\eventloop\zmqstream.py", line 584, in _run_callback
        f = callback(*args, **kwargs)
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\zmq\eventloop\zmqstream.py", line 308, in stream_callback
        return callback(self, msg)
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\notebook\services\kernels\handlers.py", line 572, in _on_zmq_reply
        super()._on_zmq_reply(stream, msg)
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\notebook\base\zmqhandlers.py", line 256, in _on_zmq_reply
        self.write_message(msg, binary=isinstance(msg, bytes))
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\tornado\websocket.py", line 339, in write_message
        return self.ws_connection.write_message(message, binary=binary)
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\tornado\websocket.py", line 1086, in write_message
        fut = self._write_frame(True, opcode, message, flags=flags)
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\tornado\websocket.py", line 1061, in _write_frame
        return self.stream.write(frame)
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\tornado\iostream.py", line 546, in write
        self._handle_write()
      File "C:\Users\me\Miniconda3\envs\ipympl39\lib\site-packages\tornado\iostream.py", line 976, in _handle_write
        self._write_buffer.advance(num_bytes)
    AttributeError: 'NoneType' object has no attribute 'advance'
pythonic2020 commented 1 year ago

Using notebook 7.0.0, ipympl works fine!

pythonic2020 commented 1 year ago

ipympl 0.9.3 works fine in notebook 6.5.5 in a python 3.9.17 environment! "Case" closed. :-)