EngComp4_landlinear icon indicating copy to clipboard operation
EngComp4_landlinear copied to clipboard

plot_helper.py outdated for JupyterLab

Open labarba opened this issue 1 year ago • 9 comments

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!

labarba avatar Aug 27 '24 22:08 labarba

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.

labarba avatar Aug 27 '24 23:08 labarba

I still have %matplotlib notebook in the first cell, but the figures are not interactive. At least there's no error.

labarba avatar Aug 27 '24 23:08 labarba

@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.

tingyu66 avatar Aug 31 '24 17:08 tingyu66

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!

labarba avatar Aug 31 '24 20:08 labarba

Some testing and digging:

  • commenting the block of code implemented in #12 and using %matplotlib widget works after I installed ipympl
  • 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.py is checking if _backend in _int_backends: to shrink figsize and fontsize in interactive plots
  • this if is no longer true if the backend is the longer string shown above, instead of 'nbAgg'

labarba avatar Sep 01 '24 16:09 labarba

  • removing%matplotlib widget, the backend is 'module://matplotlib_inline.backend_inline'
  • can execute all the plots until the section on 3D matrices without %matplotlib widget and add this just before the first 3d plot

labarba avatar Sep 01 '24 17:09 labarba

@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.

labarba avatar Sep 01 '24 22:09 labarba

Should we edit the requirements.txt and environment.yml files?

labarba avatar Sep 03 '24 19:09 labarba

Should we edit the requirements.txt and environment.yml files?

Yes, adding ipyml and jupyterlab should be sufficient.

tingyu66 avatar Sep 06 '24 00:09 tingyu66