Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live and interactive code, equations, visualizations and narrative text.

Example Notebook

It is one of the examples from official demo Notebook. As we can see in the image, it contains formatted text, beautiful equations, interactive sliders, mind blowing graphs. It also contains real python code running and giving us that ‘hello world’ output. Putting simply, it is a document that we can use to combine our analysis code with narrative texts, mathematical equations and visualization figures. And that is the reason behind its name — ‘Notebook’ (Jupyter Notebook).

Jupyter Notebook is a part of Project Jupyter and is successor to the earlier IPython Notebook. The loose acronym for Jupyter is Julia, Python, and R– Core supported languages in Jupyter Notebooks. But currently Jupyter Notebook supports more than 40 programming languages.

Going little deeper in the application, Jupyter Notebook is an application with server-client architecture that consists of mainly two components:

  • Notebook Document: Interactive document which we talked about earlier.
  • Notebook Kernel: Computational engine (Server) running in background responsible for code execution.

As I mentioned earlier, Jupyter Notebooks are available with over 100 kernels for different language. However, in this tutorial we are focusing on Python based Notebook with IPython kernel.

Installing Jupyter Notebook in Pythonic way: pip

You can simply install Jupyter Notebook by opening terminal/command and entering following command:

This installs the Jupyter Notebook and all its dependencies in Global Environment which might mess up with dependencies of other projects. For this reason, I strongly recommend installing Jupyter in virtual environment.

Creating Python virtual environment.

Starting Jupyter Notebook server

After finishing the installation, we can now start the Notebook server. It is fairly simple to start the server. Before starting the server, you might want to activate a virtual environment (if used) or create new folder somewhere to store future Notebooks, open command/terminal in that folder and enter following command.

And the output would be like:

Then you’ll see the application opening in the web browser on the following address: http://localhost:8888 .

This interface we are seeing in browser is Dashboard. You can see your files here in the folder where you started server, if any. It’s like file explorer for Notebooks.

Creating a new Notebook.

Click the “New” drop-down button in the top-right and select “Python 3” to create Python 3 notebook (Why Python3 ?).

And after clicking ‘Python3’: ka-boom new Notebook tab with new interface will open.

Empty Notebook Interface

This opens in a new tab so that you can run multiple Notebooks simultaneously. Here, looking back to empty directory, now we can find Untitled.ipynb file there.

This file actually saves all of our code, texts, output and other metadata required for Notebook to load next time in JSON format.

Familiarization with interface

All these buttons in toolbar and options like in Microsoft Office might overwhelm you at first glance. But don’t worry. There are only two concept unique to Jupyter notebook. And they are:

1. Cell

Cells are individual boxes in Notebook where we can modify our content (code/formatted text). In the screenshot of Notebook above, that box with the green outline is an empty cell. There are mainly two types of cell.

  • code cell contains code to be executed in the kernel and the output is displayed immediately below. By default, it is a code cell when you add new cell from ‘cell’ menu or by clicking on ‘+’ button. Each code cells have ‘In [] or In [<number>]‘ in left hand side.
  • Markdown cell contains text formatted using Markdown and displays its output in-place when it is run. Input in markdown cell are not sent to kernel for execution as it doesn’t contain executable content.

See, it’s that easy! You can run code inside cell either by clicking button on toolbar, or with Shift+Enter. Notice that when we run the code in cell, In [] changes to In [*] for a brief period of time and finally its In [1]. Here, this In stands for Input and number between brackets indicate when the code input is submitted to kernel for execution. And * in between appears when kernel is currently executing the code.

2. Kernel

Like I explained earlier, this is the main application server running in the background. It is responsible for evaluation and execution of the code from cell and sending back the output to the cell. We usually do not interact with kernel directly, but there are a few aspects of kernel that are worth knowing for debugging and testing.

1.Kernel pertains the state of cells and Notebook. Functions, objects, variables defined in one cell are visible in another cell as well. Let’s clear this out with simple example.

jupyter notebook cell test

Here, in this example, when we run cell 1, the variable test_var and function sum are created and saved in memory by kernel. In cell 2, we are printing variable from cell 1. And in cell 3, we are calling function defined in cell 1. In summary, we can tell that Notebook acts as a single python file with code from individual cells combined.

2. Exceptions and errors are in cell level and doesn’t affect other cells. Let’s assume we have a python script which does heavy computation and prints result.

When we execute this script it runs for 3 hour. But the program exits due to a simple syntax error– *#$(!#$!@#^&. In such a scenario, we have to re-run the whole program. However, this is not the case in Notebook. What we can do is run the long running task in one cell and do other work in another cell like this.

In the above example, when task completes, result is stored in variable. When error occurs in one cell, we can edit code and re-run that particular cell without affecting others. This is one of the main reason that Jupyter Notebook is widely used in the field of Data Science. This also boosts rapid development. Each cell in a Notebook is like a checkpoint due to which it is best suited for Iterative Development.

Restarting and Interrupting Kernel

When we are stuck in an infinite loop or a never ending code execution, we need to stop code from executing. In normal python code execution from command line, we stop execution by Keyboard interrupt or Ctrl+C or Ctrl+Z. But in Jupyter notebook, this is achieved by kernel interruption.

In the example above, the first cell is running infinitely. We can’t run codes in other block while kernel is busy executing code from other cell. In order to stop infinite loop we have to interrupt the kernel. Result of kernel interruption is shown below.

There might be some cases when we want to flush all the variables And fresh start the execution. In this case we need to restart the kernel.

Markdown cell in Jupyter Notebook

New cells in Jupyter notebook, by default, are code cells. To convert a code cell into a markdown cell, click on the ‘Markdown’ option under cell type under cell in toolbar.

Headings

We can add headings by starting a line with one (or multiple) # followed by a space. Greater the number of #, the smaller the heading. Notebook also gives us a preview while we are typing.

Heading in jupyter notebook

And when we run the cell we get the final result.

Text styling

You can make text italic or bold by surrounding a block of text with a single or double * or _ respectively.

Creating lists

We can create a list (bullet points) by using dashes, plus signs, or asterisks.

And output for above input is:

LaTeX equations

You can include mathematical expressions both inline and in new line by using $ or $$ before expression respectively.

Output:

More about markdown in Jupyter notebook could be found in this link.

Shortcuts in Jupyter Notebook

Jupyter notebook has a ton of shortcuts for doing almost anything within the notebook. Some shortcuts are for edit mode while some of them are for command mode. There are a few commands that work in both the modes. When you are in edit mode (default mode) you can see icon in upper right corner of notebook. Pressing Esc activates the command mode and that icon disappears. To find keyboard shortcuts click Help>Keyboard Shortcuts. When clicking new pop up window like this opens.

And you can edit shortcuts by clicking Help>Edit Keyboard Shortcuts.

Bonus Point

If you made through this post so far, here is a bonus point especially for you. You can run command in cmd/terminal from Notebook cell. It could be done by adding ‘!’ in-front of command in code cell and run. This becomes handy when Jupyter notebook is running on remote server. We will discuss about setting Jupyter notebook up in remote server in our future post.

Installing module directly from Notebook

So far we have talked about basic concept related to notebook. But it’s not the end. There are a lot of other cool things about Jupyter notebook. Some of them are magic commands and extensions for Notebook. Which we will talk about in our future posts.

If you have any questions or comments regarding the post (or something else), please feel free to reach out through the comments.