Jupyter Notebook Tutorial

With my school start delayed, I found myself with nothing to do at home. I decided to explore MOOCs, and while taking notes, I realized that OneNote couldn't effectively document code – it lacked features like code formatting, indentation, and line numbering. That's when I thought about Jupyter Notebook, a program that had been sitting unused on my computer. I decided to document the installation and usage process.

This post was translated from my Chinese blog post with the aid of ChatGpt.

Installation

Before installing Jupyter Notebook, you need to have Python or Anaconda installed. You can install it using either conda (for Anaconda) or pip.

The Jupyter official recommendation is to install it using conda (Anaconda). Anaconda typically includes Jupyter Notebook by default during installation. However, if it’s not installed, you can use the following command:

1
conda install jupyter notebook

If you prefer not to use Anaconda, you can install Jupyter Notebook using pip with the following command:

1
pip install jupyter notebook

Relationship Between Anaconda and Python

Let me explain the relationship between Anaconda and Python briefly. Python is a programming language, as you may know. Anaconda, on the other hand, is like a distribution of Python. It comes with many scientific computing libraries and the package manager conda. You can think of it as a comparison between Ubuntu (Python) and Linux (Anaconda).

After installing Anaconda, don’t forget to manually add the following system environment variables:

1
2
3
C:\ProgramData\Anaconda3\Scripts
C:\ProgramData\Anaconda3
C:\ProgramData\Anaconda3\Library\bin

Configuration

Installing Java Extension

I use the Java plugin called IJava, which supports features like code execution, auto-suggestions, error prompts, and more. Here’s how to install it:

  1. Download the compressed package from the release page.
  2. Extract the contents and open the extracted folder.
  3. Run the terminal as an administrator inside the extracted folder.
  4. Enter python install.py -h.
  5. Check if the installation was successful by entering jupyter kernelspec list. If successful, it should return java C:\ProgramData\jupyter\kernels\java.

Installing Nbextensions

This is a collection of extensions that provide various functionalities such as setting the auto-save interval, supporting more LaTeX elements, adjusting code font size, and more. Each feature in the extension comes with detailed explanations. I primarily use it to display a table of contents for Markdown. Here’s how to install it:

1
conda install -c conda-forge jupyter_contrib_nbextensions

After installation, it should appear in the Jupyter status bar. If it doesn’t, use this command:

1
jupyter contrib nbextension install --user --skip-running-check

Inside it, enable the Table of Contents(2) to activate the table of contents feature.

Installing nb_conda

nb_conda is mainly used for switching between multiple runtime environments. To install it, use the following command:

1
conda install nb_conda

Changing the Default Storage Location

The default storage location for Jupyter Notebook can get cluttered with many files. To change the notebook’s storage location to a specific folder, follow these steps:

  1. First, create the Jupyter configuration file at C:\Users\Sun\.jupyter\jupyter_notebook_config.py using this command:
    1
    jupyter notebook --generate-config
  2. Open C:\Users\Sun\.jupyter\jupyter_notebook_config.py, search for c.NotebookApp.notebook_dir, remove the # at the beginning of the line, and place the directory path of your chosen folder between the single quotes. After modification, it should look like this:
    1
    2
    ## The directory to use for notebooks and kernels.
    c.NotebookApp.notebook_dir = 'E:\Jupyter'
  3. Save the file and open Jupyter Notebook to check if the change was successful.

Usage

Starting

  • After installation, open the terminal and type jupyter notebook to launch it. The default address to access it in your web browser is http://localhost:8888.
  • You can specify a port number using jupyter notebook --port port_number.
  • To open a specific notebook, use jupyter notebook notebook_name.ipynb.
  • You can start the service without opening a browser using jupyter notebook --no-browser.

Shutting Down

There are two ways to shut down Jupyter Notebook:

  • In your web browser, click “Quit.”
  • In the terminal, use ctrl + c to stop the server.

Home Page Operations

Home Page Operations

Notebook Operations

Notebook Operations

Additionally, you can click on the notebook title (“Untitled”) to rename it.

Jupyter Notebook in Markdown mode supports internal links. Here’s an example:

1
2
3
[Example](#link)
Text in between
<a id=link>Link Text</a>

You can omit the “Link Text” if you don’t want a visible description for the link.

Commands

In the Python kernel, you can use Shell commands by prefixing them with %. For example:

1
2
3
4
5
%pwd # Get the absolute path of the folder
%ls # List files in the current folder
%run file_path # Run a local file
%load file_path # Load a local file
%load URL # Load source code from a specific website, e.g., https://www.baidu.com/

Keyboard Shortcuts

Jupyter Notebook has two different keyboard input modes: Edit Mode and Command Mode.

Edit Mode

Edit Mode

Command Mode

Command Mode

Custom Shortcuts

Jupyter Notebook has limited default keyboard shortcuts, but it provides space for custom shortcuts. You can set them in Help - Edit Keyboard Shortcuts. In the add shortcut section, enter the desired key combination and click the + sign on the right. Please note:

  • You need to input complete letters for Ctrl, Shift, Alt, etc., without abbreviations.
  • Use hyphens (-) to separate keys.
  • After configuring shortcuts, remember to click “OK” in the lower right corner.

I’ve set up two custom shortcuts here:

  • “clear cell output” to clear the output of a cell, with the shortcut Alt-Shift-D.
  • “clear all cells output” to clear the output of all cells in the current notebook, with the shortcut Alt-Ctrl-D.

References:
https://zhuanlan.zhihu.com/p/33105153
https://www.zhihu.com/question/380990243
https://blog.csdn.net/qq_34992030/article/details/89175633