How to get started with Jupyter Lab, an all-in-one programming interface designed by and for scientists.
Researchers often juggle a suite of software applications. R studio for R, Spyder for Python, Overleaf for Latex, Atom for other languages, etc. JupyterLab can replace these applications with a single platform that will streamline your work and make it easier to reproduce. I highly recommend JupyterLab for anyone that uses Python and R regularly. If you only use R, I recommend sticking with R studio. Here's a quick look at some of the most significant advantages:
The only notable draw back is that the set up and learning curve is a bit more difficult than something like R studio. It doesn't take long to get comfortable with it though, and once you do you'll find it will improve your workflow from production to presentation. If that sounds attractive to you then this tutorial will show you how to install JupyterLab, set up your first virtual environment, and get Jupyter up and running.
Anaconda is not the only way to use Jupyter, but it has the massive advantage of easy to manage virtual environments. This goes a long way in encouraging best practices, and will save you massive headaches in the long run.
After you’ve installed Anaconda your computer may have opened Anaconda Navigator:
Close this out, we won’t be using it! Now let’s open up our terminal and confirm that everything was installed correctly. Type "conda info" in your terminal and hit enter. You should see something like this:
If your print out looks similar anaconda successfully installed. Keep that terminal open and we'll use it for the next step.
Virtual environments create contained computing environments that can run independently and won’t interfere with anything else on your computer. They also make your work easier to reproduce. They are not required to run Jupyter, but you’ll regret it down the road if you don’t use them.
I like to have a “sandbox” virtual environment for things like homework or other small projects that don't need to worry about reproducibility. It has most of what I need, I can spend no effort maintaining it, and if it ever breaks I can just delete it and make a new one. If working on a larger project I create a separate environment specific to that project. For now, let’s create a sandbox environment. In your terminal type:
You can replace “sandbox” with whatever you want to name the environment and you can change the python version to whatever version you want to work with. You can remove the python version entirely for a blank environment, but specifying one will automatically install some basic tools so it’s generally a good idea to do so. If you're more interested in R than python, create the environment with any version of python for now and I'll go over R in the next step. When you’re asked to proceed press “y” and enter.
Now anytime you want to use this environment type:
For a full list of commands to manage virtual environments see the anaconda documentation here: Conda Environment Docs
R and JupyterLab weren't included out of the box with your environment so let's install them now. In your terminal with your environment activated type:
To install Jupyter type:
Going forward ,“conda install packagename” should be your default method to install python packages. If conda doesn’t have a package you need, try adding the Conda Forge repository and installing. As a last resort, you can try using “pip install packagename.” This will try to pull the package from a different repository, but can occasionally cause some compatibility issues.
Jupyter can’t use R out of the box, so we need to enable it as a final step. In your terminal, start an R session by simply typing “R” and pressing enter. Now run the following two lines of code:
> install.packages('IRkernel')
> Irkernel::installspec()
You can exit the R session by hitting CTRL + Z.
We’re now ready to boot up JupyterLab. Before you do though, close the terminal and open a fresh one. This is the process you’re going to run every time you want to boot up JupyterLab so remember this!
It should look something like this:
That’s it! JupyterLab will now open in your default browser. Even though it’s opened in your browser, Jupyter is not connected to the internet so don’t worry if you’re working with sensitive data. You’ll notice the terminal you used to open Jupyter is still running in the background. Keep this open while Jupyter is running. Let’s take a quick tour.
First things first, I like to change the theme to dark. You can change this in the settings here:
The bar on the left is used to navigate JupyterLab:
Of particular importance is the folder at the top which you can use to navigate your local directories, and the puzzle piece at the bottom you can use to install plugins. Jupyter has all kinds of great plugins that can do things like add LaTeX support and render documents similar to overleaf, integrate with github, create a variable viewer, or provide spell checking. You’ll need to install nodejs via conda before extensions work. It’s a single line in your terminal and instructions can be found here: Extension instructions
On the right is the launcher where you open new tabs and create new files to program in. The primary interface you will use for programming is the Jupyter notebook on the top row, but you can also open a text file or console for a more light weight programming interface.
Notebooks use a system of executable cells that contain either blocks of codes or text. When you run a cell, the output is printed directly below it. Cells can switch between programming languages or markdown and output anything from numbers and tables to charts and multimedia.
To effectively use a notebook there are a handful of keyboard shortcuts you must be familiar with. It will only take a couple of programming sessions for these to become second nature:
Shortcut | Effect |
---|---|
CTRL+Enter | Run the selected cell |
Enter | Enter edit mode for the selected cell |
A | Create a cell above the current cell |
B | Create a cell below the current cell |
DD | Delete the selected cell |
M | Switch cell to markdown mode |
Y | Switch cell to code |
CTRL+S | Save notebook |
Shift+Tab | Open function documents in Python |
This is enough to get you started, but there’s a lot more you can do with Jupyter! Here’s a few suggested next steps: