Table of Contents
Introduction
When working with an application within the browser, there might be a need for some JavaScript environment configurations. Applications such as JupyterLab are one of such applications that need JavaScript support, especially when running interactive code or plots.
In situations where the right environment is not set up in JupyterLab leading to errors. One such error is the Javascript Error: IPython is not defined
error message.
In this article, we will discuss how to deal with the JavaScript Error message involving IPython.
Use matplotlib inline to solve JavaScript Error in IPython
In a situation where you are running a plot (using matplotlib
) in a JupyterLab environment and encounter a Javascript Error: IPython is not defined
error message, you can configure your matplotlib
to use the inline
magic command which allows you to draw only static plots within your JupyterLab.
To make use of the inline
magic command, you can add the statement below before your plot
code.
%matplotlib inline
However, this means that you won’t be able to have an interactive plot within your JupyterLab. If you need an interactive
plot, the ipympl
package is helpful.
Use ipympl to solve JavaScript Error in IPython
Unlike the inline
magic command, you can make use of a matplotlib package named ipympl which provides Jupyter integration and allows you to work with interactive plots within your matplotlib.
To have that, you need to install ipympl
using either pip
or conda
depending on whether you are using a native python environment or an anaconda environment respectively.
To use pip
, you can use the command below
pip install ipympl
However, if you use conda
, you can use the command below
conda install -c conda-forge ipympl
Also, if you are using JupyterLab 2, you might need to follow the command below
conda install -c conda-forge nodejs jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-matplotlib
After installing the ipympl
package, you can restart your JupyterLab and make use of the widget
magic command to allow for interactive matplotlib within your code.
%matplotlib widget
Summary
To run interactive plots using matplotlib, you will need an extension - ipympl
- to provide for such which relies on NodeJS
to work, or you can render such a plot as a static plot using the inline
magic command.
References
GitHub - matplotlib/ipympl: Matplotlib Jupyter Integration
https://matplotlib.org/ipympl/