Turbocharge Jupyter Lab using Language Servers

With the Language Server Protocol's (LSP) development, it's now possible to turbocharge your Jupyter Lab installation! I'm going to show you how to make this work.

Why install Jupyter LSP?

Installing the Jupyter LSP brings a world of superpowers to your Jupyter experience! If you are a human programmer, you will undoubtedly make mistakes in programming, such as forgetting to assign a value to a variable or forgetting a function's signature. According to the repository README, you'll get superpowers such as:

  1. Hover tooltips to remind yourself of what a variable is
  2. Code style diagnostics (squiggly yellow lines!)
  3. Ability to jump to the definition of a variable/function
  4. Automatic completion and hinting
  5. Function signature suggestions
  6. Kernel-free suggestions (for those times your kernel is busy)
  7. Rename functions and variables throughout the workspace

If you've used the Python extension and Jupyter notebook interface inside VSCode, these should feel familiar to you!

Prerequisites

To make this work, you will need jupyterlab>=3.0.

Installation

Installation starts by installing the jupyterlab-lsp package. It's available on both PyPI and conda. You can follow the official installation instructions online, though I am also summarizing the instructions below.

tl;dr

The tl;dr version is as follows. Ensure you have the following packages in your environment.yml:

name: <your_env_name>
channels:
- conda-forge
dependencies:
- python
- jupyterlab>=3.0
- jupyter
- ipykernel
- jupyter-lsp
- jupyterlab-lsp
- python-language-server
- jedi=0.17.2 # pinned because one of the language server packages needs it precisely pinned here
# other packages that you need go below!

Then, update your environment and run Jupyter Lab.

conda activate <your_env_name>
conda env update -f environment.yml
jupyter lab

Finally, configure the Code Completion section in JupyterLab's advanced settings. The most pertinent key-value pairs to configure are:

{
    "continuousHinting": true,
    "kernelCompletionsFirst": true,
}

Install the LSP packages

We first need to ensure that you have the language server protocol packages installed for Jupyter and JupyterLab. Ensure that you install them in the appropriate environment.

conda activate <your_environment>
conda install -c conda-forge jupyter-lsp jupyterlab-lsp

Install a particular language server

We now need a particular language's language server. The available language servers are listed in the docs. If you're a conda user, you can install all of them from conda-forge:

conda install -c conda-forge python-language-server

Now, the Jupyter LSP package will communicate with the Python language server when Jupyter or Jupyter Lab are running!

Configure code completion

Of the many things that the language server protocol provides, code completion is probably the handiest of them all to configure. I've listed the section to configure above in the tl;dr section, so I'll ask you to refer back to it above in the spirit of Don't Repeat Yourself.

References

Choose and customize your development environment

At the end of the day, we choose a development environment that we are most comfortable with. The interface with our colleagues is at the level of what we share, so this should not be the highest of your concerns. Nonetheless, let me showcase where some tools can be used. Above all, avoid religious wars about text editors. Be productive, stay productive.