Data / Machine Learning

Jupyter Notebooks: The Web-Based Dev Tool You’ve Been Seeking

26 Feb 2021 8:55am, by

Jupyter notebooks are all the rage right now, and with good reason. This web-based interactive computing environment makes it easy for software engineers to create and share documents containing live code, equations, visualizations, and even narrative text. You can customize your notebooks with widgets, plots, images, video, and more.

The open source Project Jupyter supports over 40 languages (such as Python, R, Julia, Matlab, Octave, Scheme, Processing, and Scala). You can share your notebooks over email or via Dropbox, and GitHub. And with interactive output and Big Data integration, you can’t afford to overlook this powerful, user-friendly tool.

I want to walk you through the process of installing Jupyter and launching your first notebook. I’ll be demonstrating on Linux Mint, but you can get it installed on any device that supports conda, pip, pipenv, or Docker. When complete, you’ll have a fully-functioning Jupyter Notebook, ready for you to start your first project.

Installing the Dependencies

There are a number of software packages that must be installed, before Jupyter Notebook can be deployed.

The first dependency to be installed is Python. Again, we’re using Linux Mint; if you opt for a different Linux desktop release, you might have to change the installation commands (such as swapping out apt-get for dnf).

To install Python on Linux Mint, log in, open a terminal window and issue the command:

sudo apt-get install python3 -y

You might find that Python3 is already installed. Either way, to verify the installation, issue the command:

python3 --version

You should see the exact version of Python installed on your machine.

Next we need to install a piece of software that will allow us to create Python virtual environments. The command to install this is:

sudo apt-get install python3-venv -y

Since we’ll be making use of the Python package installer, pip, it must be installed with the command:

sudo apt-get install python3-pip -y

Finally, we need to install Voila, which makes it possible to view Jupyter Notebooks from within a web browser. First, create a new directory with the command:

mkdir ~/voila

We’ll now change into that new directory:

cd ~/voila

We can now use the Python venv command to create virtual environment, like so:

python3 -m venv venv

The above command will create a new directory, named venv, which includes a number of files and subdirectories. From within that directory we can load the predefined variables for voila with the command:

source venv/bin/activate

Finally, we can install Voila, using pip:

pip install voila

Install Jupyter

With all of the dependencies out of the way, we can now install Jupyter. We’ll add a couple of libraries to make it a bit more useful. You can always go back and install more Python libraries as needed. Issue the command:

pip install jupyter numpy matplotlib

That’s it. You’re finished with the installation. Now it’s time to deploy your first Jupyter Notebook.

Deploy a Notebook

Unfortunately, Jupyter Notebooks must be deployed from the command line…every time. So there isn’t a handy desktop shortcut to click. This is because Notebooks must be launched from within the virtual environment. So within the ~/voila directory, issue the command:

jupyter notebook

Shortly after you run the command, your default web browser will open, with an instance of Jupyter Notebook ready for work (Figure 1).

Figure 1: Our first Jupyter Notebook is ready to go.

From the Notebook main page, click New to reveal a drop-down (Figure 2).

Figure 2: The New file drop-down, where you can select from the available types.

Select Python3 and then, in the resulting window (Figure 3), click Untitled to name your Notebook.

Figure 3: The new file window, where you can start creating your Python project.

Kernel and Cells

There are two terms you need to understand, in order to successfully work with Jupyter Notebooks: Kernel and cells.

Within the realm of Jupyter, a kernel is a computational engine that executes the code contained within a Jupyter Notebook document. A cell is what forms the body of a notebook. It is within cells that you can write (or paste) your content. There are two types of cells: Code cell and Markdown cell. As you probably can guess, a code cell contains code and a Markdown cell contains text formatted using Markdown.

Hello, World!

Let’s create a new code cell, using the tried and true “Hello, World!” Python code. In the main window, where you see an empty cell, type:

print(‘Hello New Stack’)

You’ve created your first Code cell (Figure 4).

Figure 4: Our Hello World Python program is ready to run.

If you click the Run button, the kernel will execute the code and print the results under the cell (Figure 5).

Figure 5: Well, hello, New Stack!

After the code is run, a new cell will be created below the first, so you’re ready to continue building.

Working with Arrays

Let’s look at a different type of example. Remember, during the installation, we included numpy? Numpy is a Python library that adds support for large, multidimensional arrays. Let’s make use of that library.

First we’ll import the library by typing the following in a new cell:

Click Run to execute the code. We can now reference numpy as np and use the variable square in any other cell we create.

Next, we’ll create a small Python program that will calculate the square of a variable. Type the following code into the new cell:

Click Run and you’ll see the output of your new program (Figure 6).

Figure 6: 6 squared most certainly is 36.

If you select cell 3 and click the Run button, the program will run again, giving you different results. The cell will then be labeled 4 (Figure 7), as it’s the fourth time you’ve clicked the Run button (Jupyter keeps track of this for you).

Figure 7: Our new results and a new cell number.

Shut Down Your Notebook

If you close out the browser containing the Jupyter Notebook, you’ll see that the command is still running. In order to end the command, hit the [Ctrl]+ keyboard combination. You will then be asked if you want to shut down the notebook server. Type y and hit enter so the server will shut down.

The nice thing about Jupyter Notebook is that it automatically saves your projects. Deploy the notebook again (with the command jupyter notebook). When the browser window opens, you’ll see your named notebook (followed by the .ipymb extension). Click that name and you’ll find your work was saved and you’re ready to go at it again.

Congratulations, you’ve installed Jupyter Notebook and created a new project. To find out more about using this powerful tool, check out the official Jupyter Notebook documentation.

The New Stack is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Docker.

A newsletter digest of the week’s most important stories & analyses.