Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot_helper.py outdated for JupyterLab #11

Open
labarba opened this issue Aug 27, 2024 · 9 comments
Open

plot_helper.py outdated for JupyterLab #11

labarba opened this issue Aug 27, 2024 · 9 comments

Comments

@labarba
Copy link
Member

labarba commented Aug 27, 2024

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
Copy link
Member Author

labarba commented Aug 27, 2024

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
Copy link
Member Author

labarba commented Aug 27, 2024

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

@tingyu66
Copy link
Member

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

@labarba
Copy link
Member Author

labarba commented Aug 31, 2024

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
Copy link
Member Author

labarba commented Sep 1, 2024

Some testing and digging:

  • commenting the block of code implemented in modify plot_helper.py to work in JupyterLab #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
Copy link
Member Author

labarba commented Sep 1, 2024

  • 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
Copy link
Member Author

labarba commented Sep 1, 2024

@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
Copy link
Member Author

labarba commented Sep 3, 2024

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

@tingyu66
Copy link
Member

tingyu66 commented Sep 6, 2024

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

Yes, adding ipyml and jupyterlab should be sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants