plot_helper.py outdated for JupyterLab
Trying to run a plot command from plot_helper.py in a recent installation (JupyterLab) gives the error:
Javascript Error: Can't find variable: IPython
This typically occurs when trying to use an IPython-specific feature or functionality in a JupyterLab environment where IPython is not directly accessible.
In JupyterLab, the underlying architecture and the way it interacts with JavaScript have changed compared to the classic Jupyter Notebook interface. IPython, which provides the interactive computing environment in Jupyter Notebook, is not directly available in JupyterLab in the same way.
TO DO: plot_helper.py needs to be updated to be compatible with JupyterLab's architecture.
@tingyu66 — If you have some hints on where updates are needed in the script, let me know!
I used Copilot Workspace, and got the following suggested code, to insert after line 13:
# Check for JupyterLab environment
try:
from IPython import get_ipython
if 'ipykernel' in str(get_ipython()):
import os
if 'JPY_PARENT_PID' in os.environ:
_is_jupyterlab = True
else:
_is_jupyterlab = False
else:
_is_jupyterlab = False
except:
_is_jupyterlab = False
# Set appropriate backend for JupyterLab
if _is_jupyterlab:
import matplotlib
matplotlib.use('agg')
I tried this locally, and the error is gone, but now I'm not seeing the figures inline after executing a plot command.
I still have %matplotlib notebook in the first cell, but the figures are not interactive. At least there's no error.
@labarba After some quick online research, I feel like we should switch from using %matplotlib notebook to %matplotlib widget. The latter requires ipympl package and is based on ipywidgets, which works both with jupyter lab and notebook.
I tested locally with this updated conda env file on a linux machine:
- pandas>=0.20.3
- sympy
- jupyter
+ - jupyterlab>=4.0,<5
+ - ipympl>=0.9.3
- imageio
- ipywidgets
, and called %matplotlib widget before importing any libraries. The interactive plots work fine in both lab and notebook environments even before this fix #12.
I also saw the reference to using %matplotlib widget but when I tried it locally it didn't work. Running conda list shows me that I have ipywidgets but not ipympl. It's already present on the JupyterHub image, however. Thank you for the digging!
Some testing and digging:
- commenting the block of code implemented in #12 and using
%matplotlib widgetworks after I installedipympl - but the figure size may need to be adjusted in
plot_helper.py - running
get_backend()in a new code cell shows me that it's'module://ipympl.backend_nbagg' - a command in
plot_helper.pyis checkingif _backend in _int_backends:to shrink figsize and fontsize in interactive plots - this
ifis no longer true if the backend is the longer string shown above, instead of'nbAgg'
- removing
%matplotlib widget, the backend is'module://matplotlib_inline.backend_inline' - can execute all the plots until the section on 3D matrices without
%matplotlib widgetand add this just before the first 3d plot
@tingyu66 – I deleted the code added in PR12 to check for JupyterLab and added a line to the if block for when the backend is 'module://ipympl.backend_nbagg', but I don't think it is working as expected – see line comment.
Should we edit the requirements.txt and environment.yml files?
Should we edit the
requirements.txtandenvironment.ymlfiles?
Yes, adding ipyml and jupyterlab should be sufficient.