VSCode Version

You are encouraged to use Jupyter with VSCode instead of its original interface.

The advantage of the VSCode version is that you can use the rich plugins available in VSCode, such as variable explorer and data viewer, code splitting (which are drawbacks of the web version), auto-completion, quick navigation, code correction, displaying function parameter documentation, custom themes, and interactive Git version control (e.g., PyLance, GitHub Copilot, GitHub Theme).

1) Install VSCode locally and install the RemoteSSH plugin. The specific process can be found in the official tutorial.

2) After setting up SSH passwordless login, add the following content to the local SSH configuration file.

The location of the local SSH configuration file is usually (and can be opened and edited using VSCode or Notepad) (C:\Users\[local_username]\.ssh\config) and can be opened and edited using VSCode or Notepad.

"C:\Users\[local_username]\.ssh\config"

This allows SSH to use the local private key for login, eliminating the need to enter the password manually.

Host [host]
  HostName [host]
  User [username]
  IdentityFile "C:\Users\[local_username]\.ssh\id_rsa"

3) After logging into the server in VSCode, add the project folder in the VSCode file explorer and click on the ipynb file to open Jupyter Lab.

4) Set Jupyter Lab to use a fixed token for login.

Start Jupyter Lab and copy the URI containing the token http://localhost:22222/lab?token=foobar (note that it should be displayed as http://localhost:22222/lab?token=... in the SSH terminal, and you need to fill in the token you set in the URI).

Enter this URI in the "Jupyter Server" in the bottom right corner of VSCode to avoid entering the Jupyter Lab password every time.

Why does it show missing components even though the Conda environment on the server is configured?

Reason

It may be because you haven't selected the kernel on the server side, so VSCode defaults to using the Jupyter Kernel on your local machine.

Solution

In the kernel settings in the top right corner of VSCode, select the IPython kernel on the server side. You can determine if it is a server-side kernel by the path of the kernel, the word "Remote," or the Conda environment name.

5) You can refer to the official tutorial for how to use Jupyter Lab in VSCode.

How to resume the kernel and continue working after the SSH connection is interrupted?

First, make sure you have run Jupyter Lab using this method.

In the kernel selection in the top right corner, select the previous kernel of this notebook.

Kernel management in VSCode version

Currently, the VSCode version does not support closing the kernel like the web version. Therefore, it is recommended to regularly clean up the kernels consciously, otherwise there may be too many kernels running in the background.

The easiest way is to restart Jupyter Lab regularly. Another method is to open the web version, clean up the kernels there, and then return to the VSCode version to work.

P.S.: If I am responsible for maintaining those servers, there will be scripts to automatically detect and prompt you to close processes that consume too much memory or have high cache usage.

How to find and replace within a cell in the VSCode version?

In the VSCode version of Jupyter, you cannot use the web version's Ctrl+Shift+R to find and replace within a cell. Instead, you can use the F3 key.

If you need to find and replace throughout the entire notebook, you can use Ctrl+F and Ctrl+H as usual.


Last update: September 16, 2023