Jupyter Lab is a very useful tool to write Python code. I use it all the time when I'm starting a new project. It's also a good way to present something you're working on and add notes next to your code. This post will help you setup Jupyter Lab with various kernels and widgets, so that your productivity increases.
We are going to see how to make it from Jupyter Lab most basic interface to a more sophisticated one, with kernels, widgets and everything to make you love Jupyter Lab even more. You will start from the picture on the left, and hopefully end up with the picture on the right.
Install Jupyter Lab:
pip3 instal jupyterlab
Launch it with:
Note: I prefer to launch it in the background with tmux:
tmux new -d -s jupyter "jupyter-lab --no-browser"
It’s even cooler to use aliases, in your
.bashrc ), append:
alias jupyter-tmux='tmux new -d -s jupyter "jupyter-lab --no-browser"'; alias tas='tmux attach-session -t'
Then launch jupyter with:
Attach to the session with:
Jupyter comes with a default Python kernel,
the one installed on your computer. This is a good start but you might want
to use different python environments
(if you’re used to working with
virtualenv for instance) with different
python versions or librairies. You might also want to use Jupyter Lab with
other programming languages, like
Julia. If that sounds appealing to
you, look no further, we are going to install kernels !
The installed kernels will be located under
Python kernel from a virtual environment
Let’s first add Python kernels to Jupyter Lab. I am going to assume you are familiar with virtual environments, if not you should refer to this tutorial: Pipenv & Virtual Environments.
Make sure you have
virtualenv installed on your computer:
pip3 install virtualenv
Step 1: A Python 3 Kernel
We are going to create a first kernel based on Python 3, and we are going to name it “Python (data-science)”. It’s fairly easy:
- Create the virtual environment based on
virtualenv data-science -p python3
- Activate the environment
pip install ipykernel
- Register the environment as a kernel, give it a name and a display name
python -m ipykernel install --user --name data-science --display-name "Python (data-science)"
Step 2: A Python 2 Kernel
By following the same steps, here’s how to add a Python 2 kernel, for your legacy projects:
virtualenv python2 -p python2 source data-science/bin/activate pip install ipykernel python -m ipykernel install --user --name python2 --display-name "Python 2"
Now if you install libraries within one of these virtual environments, they will be available though the corresponding Jupyter Lab kernel. If you reload your Jupyter Lab interface, you should see something like this:
Here are some useful commands:
- List your kernels:
jupyter kernelspec list
- Uninstall a kernel:
jupyter kernelspec uninstall data-science
Kernels with other programming languages
Assuming you have R installed, run these lines in a terminal:
Rscript -e 'install.packages(c("repr", "IRdisplay", "IRkernel"), type = "source", repos="https://cran.rstudio.com");' Rscript -e 'IRkernel::installspec()'
Reload Jupyter Lab and you should see this:
Nice, R kernel just appeared. Here’s another trick, you can actually write
code within a
Python notebook. For this, you need to install the library
pip3 install rpy2
Then, from a Python notebook cell, load the
You can now write R code within that cell! For instance:
I have been recently introduced to the Julia programming language, and it seems very fun and powerful. Let’s create a Julia kernel, because why not ?
Assuming you have Julia installed, launch Julia from a terminal
Julia) and then type:
Reload Jupyter Lab as usual, and you should now see the new Julia kernel:
That’s pretty neat, we now have 5 kernels:
- Python 3: the default python on your computer
- Python (data-science): python 3 from a virtual environment
- Python 2: python 2 from a virtual environment
- R: to run R code
- Julia: to run Julia code
Let’s now tweak our beloved Jupyter Lab by adding some widgets. Namely:
- Table of contents: automatically generate a table of contents for your notebook, based on the markdown sections you wrote
- Git: If working within a git repository, directly add & commit from the Jupyter Lab interface
- Latex: Write and compile Latex files from Jupyter Lab
- Templates: Lets you start a notebook from a template
Note: you can either install extensions from the command line, or by using the “Extension Manager” tab within the Jupyter Lab user interface. I prefer the command line though, it’s easier to store my extensions and make a script to automatically install them.
Table of contents
As mentioned earlier, this widget generates a table of content from your notebook.
Install it with:
jupyter-labextension install @jupyterlab/toc
You’ll see a new tab appear, when you click on it, it’ll show the table of content. The sections are automatically created based on the markdown sections. Note that you can either enable automatic numbering or not. I did enable it on the following screenshot, and it looks like this:
If you’re tired of running back and forth between your terminal and Jupyter Lab to commit your code, consider this extension that brings a Git interface to Jupyter Lab. Install it with:
pip3 install jupyterlab-git jupyter-labextension install @jupyterlab/git
As usual, a new tab appears:
I mostly use overleaf for Latex, but if you’re not and if you wish to use Jupyter Lab for compiling Latex documents, this plugin will do the job. Install it with:
pip3 install jupyterlab_latex jupyter-labextension install @jupyterlab/latex
Now when you are editing a Latex file, a right click on the document will show an option that lets you render the file to pdf:
Sometimes you might feel like you always copy paste the same lines of code at the beginning of a new notebook. If you are, consider using notebook templates with this plugin. Install it with:
pip3 install jupyterlab_templates jupyter labextension install jupyterlab_templates jupyter serverextension enable --py jupyterlab_templates
Then you will see a new “Template” icon next to your kernels. If you click on it, you will be asked which template you want to use, and it will create a notebook based on this template. You can create your own templates by saving notebooks in the template directory. You also get to choose which directory the plugin will pick the templates from.
You know now how to setup Jupyter Lab and enhance it with kernels and widgets. There’s a lot more to it and I’d love to hear about what kernels and widgets you love. Feel free to reach out in the comments. I hope this tutorial was somehow helpful.