Jupyter Notebooks in VS Code with Interactive Matplotlib Figures and Persistent Sessions

TL;DR: I run a Jupyter Notebook server in a tmux session and configure VS Code to connect to that. I use ipympl to display interactive Matplotlib figures in VS Code.

Persistent Sessions

Generally, VS Code shuts down a Jupyter Kernel when is closed. This might be disadvantageous if it contains costly computations (like training of machine learning models). Then, it should continue to run in the background, even if the user is not connected.

To run a Jupyter notebook server continuously, I use tmux.

VS Code allows to specify a remote (or local) Jupyter Server to connect to when viewing notebooks: Press Ctlr+Shift+P to access the Command Palette and search for “Jupyter: Specify server for connections”:

Start the notebook server and copy its URL into the configuration field

$ jupyter notebook --no-browser
[I 22:07:43.456 NotebookApp] Jupyter Notebook 6.4.0 is running at:
[I 22:07:43.457 NotebookApp] http://localhost:8891/?token=deadbeef
[I 22:07:43.457 NotebookApp]  or
[I 22:07:43.457 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 22:07:43.483 NotebookApp] 
    To access the notebook, open this file in a browser:
    Or copy and paste one of these URLs:

Then, new notebooks will be opened using the configured notebook server.

Interactive Matplotlib Figures

By default, Matplotlib figures are static in VS Code. This can be changed by using the ipympl Matplotlib backend. It can be installed via Conda (conda install -c conda-forge ipympl) and has to be activated using the following cell magic:

%matplotlib widget