4. JupyterLab for Interactive Computing#
4.1. Introduction#
JupyterLab is an open-source, web-based integrated development environment (IDE) for notebooks, code, and data. It has a flexible and powerful interface for interactive computing, data analysis, and scientific computing. JupyterLab is a part of the Jupyter Project, which aims to provide a platform-agnostic, interactive computing experience. JupyterLab builds upon the functionality of traditional Jupyter notebooks by offering a more feature-rich environment for data scientists, researchers, and students.
4.2. Installing JupyterLab#
To Install JupyterLab, follow the steps below:
Using pip
$ pip install jupyterlab
Using Conda
$ conda install jupyterlab
Note: You need to install JupyterLab in each environment that you need to use it. In most scientific projects, JupyterLab is always included in the environment.yml
. At the end of this chapter you will learn how to install one instance of JupyterLab and use different conda environments from that instance. Just make sure you use the latest version to take advantage of all the new features.
4.3. Launching JupyterLab#
You can launch JupyterLab using the following command:
$ jupyter lab
This will open a new tab in your web browser, displaying the JupyterLab interface.
Let’s look into what’s happening when you run JupyterLab. Behind the scene, JupyterLab runs a Jupyter server that is hosted on your computer and accessible through port 8888
(default) of localhost. After you run jupyter lab
, several lines will be printed in your terminal which indicates the path to the Jupyter server on your machine, and the URL to access it. The URL has a format like this:
http://127.0.0.1:8888/lab?token=TOKEN
127.0.0.1
is the IP of your local host, 8888
is the port number, and TOKEN is a randomly generated string that is used to authenticate access to the notebook server. If JupyterLab doesn’t automatically open in your browser, you can copy the link from terminal and paste it in your browser. We will use this later on when we deploy JupyterLab inside a Docker container.
4.4. JupyterLab Interface Overview#
Fig. 4.1 shows the latest JupyterLab interface at the time of this writing (Sep 2023). The interface has multiple sections:
Launcher
Launcher is a tab that contains shortcuts to launch a notebook, terminal, markdown file, python file, etc. You can access launcher under File or by clicking the large blue button on the top left of the screen.
File Browser
On the left-hand side of the interface, you’ll find the file browser, which allows you to navigate your file system and create, open, and manage notebooks and other files. The files and directories that you can access here are the ones that are located in the directory you launched JupyterLab from.
Tabs and Workspaces
JupyterLab supports multiple tabs, enabling you to work on multiple notebooks or files simultaneously. You can also organize your workspaces by creating customized layouts to suit your workflow.
Kernel
The kernel is responsible for executing code within a notebook. You can choose different kernels for different programming languages (e.g., Python, R, Julia) depending on your analysis needs. You can see all running kernels on the left had side by clicking on the kernels icon.
The JupyterLab documentation has detailed tutorials for The JupyterLab Interface, Managing Kernels and Terminals, Working with Terminals, Notebooks, Text Editor, and Working with Files.
4.5. Accessing Conda Environments from JupyterLab#
When you run the jupyter lab
command in your terminal, JupyterLab server will launch from the conda environment that was active in your terminal. For example, if you are in the base
environment, the JupyterLab instance will be launched in base
and all notebook will use the base environment by default.
In order to use other conda environments in your JupyterLab instance, you have two choices:
Launch JupyterLab in the target environment that you are interested in. In this case, you will only be able to access your target environment from your JupyterLab
If you prefer to be able to change your environment inside the JupyterLab instance, you need to do two things:
Decide which environment you want to use to launch JupyterLab from (this can be your
base
environment), and installnb_conda_kernels
in that environment as following:
$ conda install nb_conda_kernels
Make sure to install
ipykernel
in any other environment that you would like to be able to access inside JupyterLab
$ conda install ipykernel
4.6. JupyterLab Shortcuts#
There are several very useful and practical shortcuts for JupyterLab that improves your experience of working with it. You can see all of the default shortcuts after launching your JupyterLab byt navigating to Settings>Advanced Setting Editor>Keyboard Shortcuts.
We will introduce some of the useful ones in the class.