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).
From the Notebook main page, click New to reveal a drop-down (Figure 2).
Select Python3 and then, in the resulting window (Figure 3), click Untitled to name your Notebook.
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).
If you click the Run button, the kernel will execute the code and print the results under the cell (Figure 5).
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:
1 2 3 | import numpy as np def square(x): return x * x |
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:
1 2 3 | x = np.random.randint(1, 10) y = square(x) print(‘%d squared is %d’ % (x, y)) |
Click Run and you’ll see the output of your new program (Figure 6).
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).
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.
InApps is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Docker.