How To Use Jupyter Notebook Tutorial

Jupyter notebook is a web application program. It can be used to easily create and share program documents. It supports real-time code, mathematical equations, visualization and markdown. It’s purpose includes data cleaning and conversion, numerical simulation, statistical modeling, machine learning, etc. This article will tell you how to install and run it.

1. Jupyter Notebook Operating Mechanism.

Jupyter notebook is essentially a web server based browser-server program, which is developed with tornado framework. Below is it’s working sequence diagram.

jupyter notebook working sequence diagram

Below is Jupyter notebook working process and some conclusion.

  1. User use web browser to interact with Jupyter notebook.
  2. Web browser interacts with the backend notebook web server over http and websocket protocol.
  3. Web server computes and communicates with the programming language kernel through ZMQ.
  4. Web server reads and writes the notebook file on hard disk.
  5. The web server maintains a readable and writable browser based editing environment and storage service. The kernel is only used to execute specific code and return results.
  6. The kernel here is not limited to Python, it can be more than 40 languages such as R language. The coding language in which the notebook file you are currently editing is linked to the interpreter kernel for that coding language.
  7. You can list currently used kernels with command jupyter kernelspec list in terminal like below.
    $ jupyter kernelspec list
    Available kernels:
      python3    /Users/songzhao/opt/anaconda3/share/jupyter/kernels/python3
  8. As a complete record of a session, notebook files interweave executable code, rich explanatory text, mathematical formulas, and result objects together.
  9. The notebook file is actually a JSON file, but is saved with the .ipynb extension.
  10. Because JSON is in plain text format, it can be versioned and shared with colleagues.

2. Install Jupyter Notebook.

Although Jupyter notebook supports multiple programming languages to run code, but it requires python (Python 3.3 or later, or Python 2.7) to install it. There are two methods to install it.

2.1. Install Jupyter Use PIP.

2.1.1 Install Jupyter In Python3.
# First upgrade pip version.
python3 -m pip install --upgrade pip

# Install jupyter.
python3 -m pip install jupyter
2.1.2 Install Jupyter In Python2.
python -m pip install --upgrade pip
python -m pip install jupyter

2.2 Installing Jupyter Notebook Through Anaconda.

  1. Download the latest version of Anaconda.
  2. Install Anaconda according to the installation wizard.
  3. If everything is correct, open the terminal in Mac / Linux, or open the command line interface in Windows, and run command jupyter notebook.
  4. Because of the problem of system path, sometimes you will be prompted that it cannot find the executable command. Please add the path of the jupyter script or executable to your system path variable to fix this issue.

3. Start Jupyter Notebook.

After install, you can run command jupyter notebook to start it, you can see output like below.

$ jupyter notebook
[I 13:32:51.030 NotebookApp] JupyterLab extension loaded from /Users/songzhao/opt/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 13:32:51.030 NotebookApp] JupyterLab application directory is /Users/songzhao/opt/anaconda3/share/jupyter/lab
[I 13:32:51.033 NotebookApp] Serving notebooks from local directory: /Users/songzhao
[I 13:32:51.033 NotebookApp] The Jupyter Notebook is running at:
[I 13:32:51.033 NotebookApp] http://localhost:8888/?token=baff2b5d3a5a55298320732d5974a1582c60c3d33e92750d
[I 13:32:51.033 NotebookApp]  or http://127.0.0.1:8888/?token=baff2b5d3a5a55298320732d5974a1582c60c3d33e92750d
[I 13:32:51.033 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

It will also open a default web browser and display the jupyter home page ( http://localhost:8888/tree ). The home page is like below picture. The currently displayed directory is the working directory of your notebook server, which can be configured.

jupyter notebook home page

Below are some useful command tips when start jupyter.

  1. Open the specified notebook file.
    jupyter notebook notebook.ipynb
  2. Specify the jupyter notebook web server port number.
    jupyter notebook --port 9999
  3. Start jupyter notebook web server without opening the browser.
    jupyter notebook --no-browser
  4. View jupyter notebook server help.
    jupyter notebook --help