matplotlib / ipympl

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

Non responsive ipympl with ipywidgets after updating environment #469

Open orome opened 2 years ago

orome commented 2 years ago

Describe the issue

I have consistently used the following workflow for fully updating my Jupyter Lab working environments:

$ rmvirtualenv my_env
$ mkvirtualenv --python=`which python` my_env
[my_env] $ pip install -r requirements.txt
[my_env] $ jupyter lab build
[my_env] $ jupyter lab

Recently however, after these steps:

  1. My widgets all become non-functional. I can operate and interact with them, but the figures they control do not change at all.
  2. All of the widget controls move from being above the figure they control to to below it.

Projects in virtualenvs that have not been recently updated in this way continue to work fine, and updating them reliably makes them stop working.

This occurs consistently (I have now ruined half a dozen projects confirming the pattern), and even for notebooks hosted outside my local machine (such as this one hosted on Binder). Control widgets themselves seem to work fine though (e.g., in notebooks like this one) when ipympl is not involved. I have also confirmed that the observed behavior is independent of browser and local machine (at least macOS vs iOS).

Has something about Jupyter Lab, ipywidgets, or ipympl changed recently that might be causing this?


Typical post update configuration:

$ jupyter --version
Selected Jupyter core packages...
IPython          : 8.4.0
ipykernel        : 6.13.1
ipywidgets       : 7.7.0
jupyter_client   : 7.3.4
jupyter_core     : 4.10.0
jupyter_server   : 1.17.1
jupyterlab       : 3.4.3
nbclient         : 0.6.4
nbconvert        : 6.5.0
nbformat         : 5.4.0
notebook         : 6.4.12
qtconsole        : not installed
traitlets        : 5.2.2

$ jupyter labextension list 
JupyterLab v3.4.3
/Users/Rax/Documents/Projects/Coding/Python/venvs/picollisions/share/jupyter/labextensions
        jupyterlab_pygments v0.2.2 enabled OK (python, jupyterlab_pygments)
        nbdime-jupyterlab v2.1.1 enabled OK
        jupyter-matplotlib v0.11.1 enabled OK
        @jupyterlab/git v0.37.1 enabled OK (python, jupyterlab-git)
        @jupyter-widgets/jupyterlab-manager v3.1.0 enabled OK (python, jupyterlab_widgets)
        @kiteco/jupyterlab-kite v2.0.2 enabled OK (python, jupyterlab_kite)

Other labextensions (built into JupyterLab)
   app dir: /Users/Rax/Documents/Projects/Coding/Python/venvs/picollisions/share/jupyter/lab

Typical pre update configuration:

$ jupyter --version
jupyter core     : 4.7.1
jupyter-notebook : 6.4.3
qtconsole        : not installed
ipython          : 7.26.0
ipykernel        : 6.2.0
jupyter client   : 6.1.12
jupyter lab      : 3.1.10
nbconvert        : 6.1.0
ipywidgets       : 7.6.4
nbformat         : 5.1.3
traitlets        : 5.0.5

jupyter labextension list
JupyterLab v3.1.10
/Users/Rax/Documents/Projects/Coding/Python/venvs/picollisions/share/jupyter/labextensions
        nbdime-jupyterlab v2.1.0 enabled OK
        jupyter-matplotlib v0.9.0 enabled OK
        @jupyterlab/git v0.32.2 enabled OK (python, jupyterlab-git)
        @jupyter-widgets/jupyterlab-manager v3.0.0 enabled OK (python, jupyterlab_widgets)
        @kiteco/jupyterlab-kite v2.0.2 enabled OK (python, jupyterlab_kite)

/usr/local/share/jupyter/labextensions
        jupyterlab_pygments v0.2.2 enabled OK (python, jupyterlab_pygments)

Other labextensions (built into JupyterLab)
   app dir: /Users/Rax/Documents/Projects/Coding/Python/venvs/picollisions/share/jupyter/lab

Typical requirements:

#...
ipywidgets                 
ipympl

jupyterlab >=3.1                
jupyterlab-git                 
jupyterlab-kite >=2.0.2        

(I've tried omitting Kite to see if that was the culprit. In any case it is not present in the Binder versions.)

Versions

 3.9.13 (main, May 24 2022, 21:28:44) 
[Clang 13.0.0 (clang-1300.0.29.30)]
ipympl version: 0.9.1
Selected Jupyter core packages...
IPython          : 8.4.0
ipykernel        : 6.13.1
ipywidgets       : 7.7.0
jupyter_client   : 7.3.4
jupyter_core     : 4.10.0
jupyter_server   : 1.17.1
jupyterlab       : 3.4.3
nbclient         : 0.6.4
nbconvert        : 6.5.0
nbformat         : 5.4.0
notebook         : 6.4.12
qtconsole        : not installed
traitlets        : 5.2.2
Known nbextensions:
  config dir: /Users/Rax/.jupyter/nbconfig
    notebook section
      nbdime/index  enabled 
      - Validating: OK
  config dir: /Users/Rax/Documents/Projects/Coding/Python/venvs/picollisions/etc/jupyter/nbconfig
    notebook section
      jupyter-matplotlib/extension  enabled 
      - Validating: OK
      nbdime/index  enabled 
      - Validating: OK
      jupyter-js-widgets/extension  enabled 
      - Validating: OK
  config dir: /usr/local/etc/jupyter/nbconfig
    notebook section
      nbdime/index  enabled 
      - Validating: OK
JupyterLab v3.4.3
/Users/Rax/Documents/Projects/Coding/Python/venvs/picollisions/share/jupyter/labextensions
        jupyterlab_pygments v0.2.2 enabled OK (python, jupyterlab_pygments)
        nbdime-jupyterlab v2.1.1 enabled OK
        jupyter-matplotlib v0.11.1 enabled OK
        @jupyterlab/git v0.37.1 enabled OK (python, jupyterlab-git)
        @jupyter-widgets/jupyterlab-manager v3.1.0 enabled OK (python, jupyterlab_widgets)
        @kiteco/jupyterlab-kite v2.0.2 enabled OK (python, jupyterlab_kite)

Other labextensions (built into JupyterLab)
   app dir: /Users/Rax/Documents/Projects/Coding/Python/venvs/picollisions/share/jupyter/lab
ianhi commented 2 years ago

@orome I believe this is something to do with the changes about when draws happen - I think between 0.8 and 0.9. The new behavior has several advantages in terms of plot persistence, but may (i'm not totally sure) be causing your problem.

Happily I think the fix is very simple. In your binder link if I add fig.canvas.draw() as the last line of plot_logisitic then it seems to work as intended. This issue aside I'd generally recommend explicit draws like this as best practice, before you were relying on a draw being triggered by something else which doesn't come with any garuntees

fomightez commented 2 years ago

See additional troubleshooting attempts here. (Using older versions of the software that still are available in MyBInder launches from Gesis was a different tack.)

ianhi commented 2 years ago

Has something about Jupyter Lab, ipywidgets, or ipympl changed recently that might be causing this?

im pretty sure the specific difference between old and newer versions is this function: https://github.com/matplotlib/ipympl/pull/343/files#diff-4979a0b66cf007ec0278c825c5c82b71fdf56711069626bb0ffa5f3c4cc9412bR380

ianhi commented 2 years ago

interestingly calling plt.ion() also fixes things, even though plt.isinteractive() returns True. That smells a bit like a bug somewhere

ianhi commented 2 years ago

more specifically even though plt.isinteractive returns True the repl_display_hook doesn't seem to have been installed which may be due to the newer delayed backend selection in pyplot? So you can even just call plt.install_repl_displayhook() and get back the old behavior.

@tacaswell is it a bug that: plt.isinteractive() returns True even though the displayhook is not installed? That is there seems to be a partially completed interactive state at the start which you can then escape with plt.ion, but never return to.

ianhi commented 2 years ago

oh wow - I should've looked deeper. I think that the answer to my question is: yes it is a bug, but it was fixed 23 days ago: https://github.com/matplotlib/matplotlib/pull/23057

@orome I'll bet that if you go to an older mpl version the old way will work again

orome commented 2 years ago

@ianhi Just to be sure I've got it: there is indeed a bug, but adding an explicit fig.canvas.draw() will fix it and is something I should be doing anyway, right?

ianhi commented 2 years ago

@orome yup!

there is indeed a bug

I think the bug is in matplotlib 3.5.1 and 3.5.2 but will be fixed in 3.5.3+

but adding an explicit fig.canvas.draw() https://github.com/matplotlib/ipympl/issues/469#issuecomment-1151558213, right?

yes and yes