Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Converted from version '1.0'.

Introduction

Jupyter Notebooks allow data scientists to create and share their documents, from codes to full blown reports. They help data scientists streamline their work and enable more productivity and easy collaboration. Due to these and several other reasons you will see below, Jupyter Notebooks are one of the most popular tools among data scientists.

  • Data visualizations. Most people have their first exposure to Jupyter Notebook by way of a data visualization, a shared notebook that includes a rendering of some data set as a graphic. Jupyter Notebook lets you author visualizations, but also share them and allow interactive changes to the shared code and data set.
  • Code sharing. Cloud services like GitHub and Pastebin provide ways to share code, but they’re largely non-interactive. With a Jupyter Notebook, you can view code, execute it, and display the results directly in your web browser.
  • Live interactions with code. Jupyter Notebook code isn’t static; it can be edited and re-run incrementally in real time, with feedback provided directly in the browser. Notebooks can also embed user controls (e.g., sliders or text input fields) that can be used as input sources for code.
  • Documenting code samples. If you have a piece of code and you want to explain line-by-line how it works, with live feedback all along the way, you could embed it in a Jupyter Notebook. Best of all, the code will remain fully functional—you can add interactivity along with the explanation, showing and telling at the same time.


It was earlier known as IPython notebook.

The name Jupyter is an acronym which stands for the three languages it was designed for: JUlia , PYThon, and R.

Prerequisites

  • Python 3
  • pip
  • Python venv
  • zeromq



$ sudo apt-get install libzmq3-dev python3-venv python3-dev python3-pip -y


$ python3 -v

Python 3.6.7


Create a virtual environment for Jupyter Notebook


$ mkdir ~/jupyter/

$ python3 -m venv ~/jupyter/jupyter-env


Essentially, pyvenv sets up a new directory that contains a few items which we can view with the ls command:


ls my_env


Set up zeromq



# Download zeromq
# Ref http://zeromq.org/intro:get-the-software
$ wget https://github.com/zeromq/libzmq/releases/download/v4.2.2/zeromq-4.2.2.tar.gz


# Unpack tarball package
$ tar xvzf zeromq-4.2.2.tar.gz


# Install dependency
$ sudo apt-get update && \
$ sudo apt-get install -y libtool pkg-config build-essential autoconf automake uuid-dev


# Create make file
$ cd zeromq-4.2.2
$ ./configure


# Build and install(root permission only)
$ sudo make install


# Install zeromq driver on linux
$ sudo ldconfig


# Check installed
$ ldconfig -p | grep zmq


# Expected
############################################################
# libzmq.so.5 (libc6,x86-64) => /usr/local/lib/libzmq.so.5
# libzmq.so (libc6,x86-64) => /usr/local/lib/libzmq.so
############################################################



Installing Jupyter Notebook

Jupyter notebook can be installed in either of two ways:

  • Using Anaconda
  • Using pip


Here we will install using pip.


Upgrade pip and install jupyter

$ pip install --upgrade --force-reinstall --no-cache-dir jupyter


When the installation had completed, you will then be able to run Jupyter notebook with the following command:

Running Jupyter Notebook Server

When the installation had completed, you will then be able to run Jupyter notebook with the following command:

$ jupyter notebook

[I 08:58:24.417 NotebookApp] Serving notebooks from local directory: /Users/catherine
[I 08:58:24.417 NotebookApp] 0 active kernels
[I 08:58:24.417 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/
[I 08:58:24.417 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

Connecting to Jupyter Notebook using SSH Tunneling

After running this command, you will see output similar to the following:



Output


[I 19:46:22.031 NotebookApp] Writing notebook server cookie secret to /home/sammy/.local/share/jupyter/runtime/notebook_cookie_secret
[I 19:46:22.365 NotebookApp] Serving notebooks from local directory: /home/sammy/environments
[I 19:46:22.365 NotebookApp] 0 active kernels
[I 19:46:22.366 NotebookApp] The Jupyter Notebook is running at:
[I 19:46:22.366 NotebookApp] http://localhost:8888/?token=Example_Jupyter_Token_3cadb8b8b7005d9a46ca4d6675
[I 19:46:22.366 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 19:46:22.366 NotebookApp] No web browser found: could not locate runnable browser.
[C 19:46:22.367 NotebookApp]

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=Example_Jupyter_Token_3cadb8b8b7005d9a46ca4d6675&tokenExample_Jupyter_Token_3cadb8b8b7005d9a46ca4d6675




You might notice in the output that there is a No web browser found warning. This is to be expected, since the application is running on a server and you likely haven't installed a web browser onto it.


For now, exit the Jupyter Notebook by pressing CTRL+C followed by y, and then pressing ENTER to confirm:


Jupyter Notebook is now installed on the server. However, in order to access the application and start working with notebooks, you'll need to connect to the application using SSH tunneling and a web browser on your local computer.




Output

Shutdown this notebook server (y/[n])? y
[C 20:05:47.654 NotebookApp] Shutdown confirmed
[I 20:05:47.654 NotebookApp] Shutting down 0 kernels


Then log out of the server by using the exit command:


$ exit


SSH tunneling is a simple and fast way to connect to the Jupyter Notebook application running on the server.


SSH Tunneling using macOS or Linux

If your local computer is running Linux or macOS, it's possible to establish an SSH tunnel just by running a single command.

ssh is the standard command to open an SSH connection, but when used with the -L directive, you can specify that a given port on the local host (that is, your local machine) will be forwarded to a given host and port on the remote host (in this case, your server). This means that whatever is running on the specified port on the remote server (8888, Jupyter Notebook's default port) will appear on the specified port on your local computer (8000 in the example command).

To establish your own SSH tunnel, run the following command. Feel free to change port 8000 to one of your choosing if, for example, 8000 is in use by another process. It is recommended that you use a port greater than or equal to 8000, as those port numbers are unlikely to be used by another process. Be sure to include right server IP address and the user to connect to the server


ssh -L 8000:localhost:8888 linaro@10.101.101.1



If there are no errors from this command, it will log you into your remote server. From there, we need to activate the virtual environment we had setup earlier:


source ~/jupyter/jupyter-env/bin/activate



Now, run the Jupyter Notebook application:


(jupyter_env)$ jupyter notebook



To connect to Jupyter Notebook, use your favorite web browser to navigate to the local port on the local host: http://localhost:8000.


SSH Tunneling using Windows and PuTTY


PuTTY is an open-source SSH client for Windows which can be used to connect to linux server. After downloading and installing PuTTY on your Windows machine (as described in the prerequisite tutorial), open the program and enter your server URL or IP address, as shown here:

Image Added


Next, click + SSH at the bottom of the left pane, and then click Tunnels. In this window, enter the port that you want to use to access Jupyter on your local machine (8000 ). It is recommended to use a port greater or equal to 8000 as those port numbers are unlikely to be used by another process. If 8000 is used by another process, though, select a different, unused port number. Next, set the destination as localhost:8888, since port 8888 is the one that Jupyter Notebook is running on. Then click the Add button and the ports should appear in the Forwarded ports field:

Configure SSH tunnel in PuttyImage Added


Finally, click the Open button. This will both connect your machine to the server via SSH and tunnel the desired ports. If no errors show up, go ahead and activate your virtual environment:



source ~/jupyter/jupyter-env/bin/activate



Now, run the Jupyter Notebook application:


$ jupyter notebook


To connect to Jupyter Notebook, use your favorite web browser to navigate to the local port on the local host: http://localhost:8000.


Using Jupyter Notebook


After navigating to http://localhost:8000, you will be presented with a login page:

Jupyter Notebook login screenImage Added


In the Password or token field at the top, enter the token shown in the output after you ran jupyter notebook from your server:



Output

[I 20:35:17.004 NotebookApp] Writing notebook server cookie secret to /run/user/1000/jupyter/notebook_cookie_secret
[I 20:35:17.314 NotebookApp] Serving notebooks from local directory: /home/sammy
[I 20:35:17.314 NotebookApp] 0 active kernels
[I 20:35:17.315 NotebookApp] The Jupyter Notebook is running at:
[I 20:35:17.315 NotebookApp] http://localhost:8888/?token=Example_Jupyter_Token_3cadb8b8b7005d9a46ca4d6675
[I 20:35:17.315 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 20:35:17.315 NotebookApp] No web browser found: could not locate runnable browser.
[C 20:35:17.316 NotebookApp]
. . .





Alternatively, you can copy that URL from your terminal output and paste it into your browser's address bar.


Automatically, Jupyter notebook will show all of the files and folders stored in the directory from which it's run.


When accessed through a web browser, Jupyter Notebook provides a Notebook Dashboard which acts as a file browser and gives you an interface for creating, editing and exploring notebooks. Think of these notebooks as documents (saved with a .ipynb file extension) which you populate with any number of individual cells. Each cell holds an interactive text editor which can be used to run code or write rendered text.


Create a new notebook file by clicking New then Python 3 at the top-right of the Notebook Dashboard:


Create a new Python3 notebookImage Added

How do I open a specific Notebook?

The following code should open the given notebook in the currently running notebook server, starting one if necessary.


jupyter notebook notebook.ipynb


How do I start the Notebook using a custom IP or port?

By default, the notebook server starts on port 8888. If port 8888 is unavailable or in use, the notebook server searches the next available port. You may also specify a port manually. In this example, we set the server’s port to 9999:


jupyter notebook --port 9999