Jupyter Notebook – Introduction

Jupyter Notebook is a web-based application to execute code, document, display visualizations, etc all inside of a single notebook. You can create and share notebooks containing text, live code, visualizations, etc. It is this versatility that has made Jupyter Notebooks one of the most popular data science tools. Not only are these notebooks preferred by data scientists but they’re also quite popular among researchers across the globe.

In this introduction, we’ll give a complete overview of Jupyter Notebooks to get you up and running with this wonderful tool.

  • Jupyter Notebook Installation
  • Jupyter Notebook Basics
    1. Starting the Jupyter Notebook server
    2. The Notebook Interface
      • Naming
      • Menu Bar
      • Tool Bar
    3. Cells
      • Types of Cells
      • Running Cells
    4. Exporting Notebooks
  • Additional Resources

To install Jupyter Notebook, we recommend you install Anaconda, which not only installs python and Jupyter Notebook for you, it also installs other important tools like Spyder, RStudio, etc as well. The installation process is relatively simple.

  • It’s recommended that you download the latest Python 3 version of the installer. Navigate to Anaconda’s Individual Edition’s page and download the Anaconda installer depending on your system requirements.
Anaconda Individual Edition installer options
  • Install anaconda from the downloaded installer. During the installation process, provide your preference on how you’d like the anaconda to be installed. For more details, you can follow this guide or use the default configurations if you’re not sure.
  • On Successful installation, you’d be able to open up the Anaconda Navigator. Using the Navigator you can open a bunch of programs including Jupyter Notebook.

A Jupyter notebook, simply, is a series of cells. Inside each of these cells, you can either execute code or show some text. And, based on the input in a cell, we get an appropriate output on execution.

The Jupyter Notebooks server is the backend that powers the notebooks running on the browser. You can use the following methods to start the Jupyter Notebook server:

Method-1: From Anaconda Navigator
Open up the Anaconda Navigator. Based on your application’s version, you’d see a window like this:

Anaconda Navigator screenshot

From the navigator, in the Jupyter Notebook section, click Launch.

Method-2: From Anaconda Prompt
Open up the Anaconda Prompt, inside the prompt type jupyter notebook and hit enter.

Either of the methods will start the notebook server. Choose whichever method you’re comfortable with. This will open up the Jupyter Notebook web application in your browser and you’d see a file-explorer view of location you’ve opened the notebook from.

Jupyter Notebook home screen showing the files.
Jupyter Notebook Home

Now that we’ve started the notebook server it’s time to create a new notebook. To do so, follow these steps:

  1. Navigate to folder where you’d like to save the notebook.
  2. Click New -> Python 3.

As a result, you’d see a new notebook open up in another tab.

New Jupyter Notebook
New Jupyter Notebook

There are four significant elements of the Jupyter Notebook interface: the notebook name, the menu bar, the toolbar, and the code cells.

Jupyter Notebook Interface (Marked)
Interface | Source: Jupyter Notebook Docs

You can see the name of your notebook next to the Jupyter logo. To change it, simply click on the name. This opens up a prompt asking you for a new name. It’s a good practice to give clean and logical names to your notebooks to avoid any confusion later on.

Rename notebook prompt in Jupyter Notebook
Rename Notebook Prompt

The menu bar has different menus that help us interact with the notebook. The menu bar functions similarly to the menu bar in general applications you may have used in the past. We won’t be covering each and every part of it in detail as it’s beyond the scope of this introduction. But, some of the important functionality of the menu bar includes:

Save and Checkpoint: Under the file menu, you have the option to save and create a checkpoint in your notebook. It’s one of the jupyter notebook best practices to periodically checkpoint your notebook so that you revert back to them if required.

Kernel: The kernel menu helps to manage to kernel powering your notebook. You can interrupt, reconnect, restart, clear the output, or even shut the kernel down altogether using it.

One of the criticisms of Jupyter Notebook is that if the cells are not executed in sequential order, it becomes quite difficult to track them. Thus, it becomes necessary to restart the kernel and run the cells from top to bottom. As one Twitter user put it, “Restart and run all or it didn’t happen.”

The toolbar has some quick access buttons for manipulating the cells. Whether it’s adding/removing a cell, shifting it, copying it, running it, or entirely changing the type of cell itself, all can be done from the toolbar.

Functionally, a Jupyter Notebook has two components. Users input programming code or text in rectangular cells on a front-end web page. The browser then passes that code to a back-end ‘kernel’, which runs the code and returns the results.

A cell is a multiline text input field. The execution behavior of a cell depends upon the type of the cell. There are three types of cells: code cells, markdown cells, and raw cells.

Code cells: A code cell allows you to write and execute code. When you run a code cell, the code inside it is sent to the kernel associated with your notebook. The results from the kernel are displayed as output immediately below your cell.

Example of print("Hello World") executed in a code cell

Markdown cells: Markdown cells allow you to display text. The specialty of these cells is that they allow you to markup your text with the Markdown language. Thus, instead of having just plain text, we can format our text which rich features like headings, lists, bold, italics, etc. For more on markdown check out this guide.

Raw cells: Raw cells allow you to write output directly. These cells are not evaluated by the notebook. Instead, they’re used when you convert your notebook to another format such as HTML or Latex. For more on raw cells, refer to the documentation.

Each cell starts off as a code cell but its type can be changed using a drop-down on the toolbar.

Toggle Cell type from the toolbar

For most purposes, you’d be dealing with Code and Markdown cells only.

To run a cell, you can use either of the following methods:

  • Shift+Enter, this is the short-cut to run a cell.
  • Click the “Run” button on the toolbar.
  • From the menu bar, go to Cell->Run Cells

Based on the type and contents of the cell, a corresponding output would be displayed below the cell.

Jupyter Notebooks by default are saved as .ipynb files. You can share these files with your colleagues who can open, execute, modify, re-execute it running it on the Jupyter Notebook server.

At times, you may also want to share your notebooks with non-technical people. Particularly, for sharing some results. For this, you can save your Jupyter notebook as an HTML file or one of the formats (shown in the image below) which can be opened without the Jupyter Notebook server.

Options to download your Jupyter notebook
Download Notebook

This completes our tutorial on Introduction to Jupyter Notebook. With the topics covered in this tutorial, we hope that you received a good introduction to this tool which is basically a Swiss Army Knife in Data Scientist’s toolkit.

A highly recommended next step would be to try things out. Create your own notebooks, play around with what you can do, and maybe come up with some really cool notebooks and share your learnings along the way.

If you’d like to have a deeper dive into all the features and capability of Jupyter Notebooks, do check out the official documentation.

Also, if you’re interested in learning in data science and want to learn python, check out our series Python for Data Science