Jupyter Notebook | An Ultimate Guide

Prajeet Singh Kalchuri
Analytics Vidhya
Published in
7 min readApr 24, 2020
Photo by Wes Hicks on Unsplash

­­­­­The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. It can be used for various applications like data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more. It is maintained by the people at Project Jupyter.

The name, Jupyter comes from the core programming languages that it supports which are: JULIA, PYTHON, and R.

Getting Started with Jupyter Notebook

It is not included in python so if you want to use it, you have to install Jupyter. Python Language has many distributions, but here we will just focus on two of them to install Jupyter notebook.

It is assumed that you are using Python 3.

Installation

The most popular distribution of python is ANACONDA. It has its installer tool called conda which can be used for installing a third-party package, and it comes with many preinstalled scientific libraries, and Jupyter Notebook is one of them, so you have to do nothing other than installing Anaconda itself.

You can also directly install Jupyter Notebook by using pip :

pip install jupyterlab

There are various other Ways also, I have listed the most convenient methods.

Starting the Jupyter Notebook

Now that you have installed Jupyter, let’s learn how to use it. First of all, you need to open up your terminal application and create a folder of your choice. Now select “new” and create a new “Python 3” notebook

You will be redirected to http://localhost:8888/notebooks/

Localhost is not a website, it indicates that the content is being served from your local machine i.e your own computer. Jupyter’s Notebooks and dashboard are web apps, and Jupyter starts up a local Python server to serve these apps to your web browser, which makes it essentially platform-independent and opening the door to easier sharing on the web.

Creating the notebook

You have to click the “New” button and it will open up a list of choices, and select Python 3 (or the version of your choice, I will recommend Python 3)

A new webpage like this will open up in a new tab :

At the topmost you will see a new file untitled.ipynb and below that you will see a lot of options like — File, Edit, View, Insert, Cell, Kernel, Widgets, Help.

What is a .ipynb File?

.ipynb is a text file that describes the contents of your notebook in a format called JSON. It contains all the content from the Jupyter Notebook web application session, which includes the inputs and outputs of computations, mathematics, images, and explanatory text.

What are Kernels?

Behind every notebook runs a kernel .when we run a code cell, that code is executed within the kernel and any output is returned to the cells to be displayed.

Options for kernels:

Jupyter Notebook provides various options for kernels. This can be useful if you want to reset things. The options are:

  • Restart: This will restart the kernels i.e. clearing all the variables that were defined, clearing the modules that were imported, etc.
  • Restart and Clear Output: This will do the same as above but will also clear all the output that was displayed below the cell.
  • Restart and Run All: This is also the same as above but will also run all the cells in the top-down order.
  • Interrupt: This option will interrupt the kernel execution. It can be useful in the case where the programs continue for execution or the kernel is stuck over some computation.

Let us import a python package and define a function :

import numpy as np 
def square(x):
return x*x

Okay now once we have executed the cell above, we can reference np and square in any other cell.

x = np.random.randint(1,10)
y = square(x)
print('%d squared is %d' %(x,y))

What are cells?

A Cell is a container for text to be displayed in a notebook or code to be executed by the notebook’s kernels.

There are mainly two types of cell that we will cover :

  • Code Cell — It contains the code to be executed in the kernel and displays its output below.
  • Markdown Cell — It contains text formatted using Markdown and displays its output in-place when it runs.

The First cell is always a code cell. So let us try the classic example and try to print ‘Hello World’. For running the cell you can click the ‘run button’ or use the keyboard shortcut (Ctrl + Enter).

Our result will be like this:

print('Hello World')

Naming the Notebook

Before starting any projects we have to name our notebook and give it a relevant and meaningful name. The naming of notebooks are very easy, as we can see that at the top of our notebook there is something written as untitled.ipynb, (see the image below)

Click Here !!

So we have to just click on the untitled.ipynb and a rename option will pop the screen like this :

But we have noted that we cannot rename a notebook while it is running, so for this, we have to shut it down (“File > Close and Halt”), and then rename it.

Saving your Notebook

By default, Jupyter will save autosave your notebook every 120 seconds to the checkpoint file without altering the primary notebook file. But, it is best practice to save regularly. Pressing (Ctrl +S) will save your notebook file.

What is this Checkpoint thing?

So, whenever we create a new notebook, a checkpoint file is created as well as your notebook file, it will be located in a hidden subdirectory of your save location called .ipynb_checkpoints and also a .ipynb file.

Exporting Your Notebooks

Jupyter has built-in support for exporting to HTML and PDF which can be found from the menu under “File > Download As.” Indeed, as many researchers in academic institutions are given some public or internal webspace.

You can also export a notebook to an HTML file, Jupyter Notebooks can be an especially convenient way for them to share their results with their peers. We can also turn our notebook into a slideshow or share it online with Github.

But if sharing exported files doesn’t cut it for you, there are also some immensely popular methods of sharing .ipynb files more directly on the web.

Keyboard Shortcuts :

To be fast and to adopt a speedy cell-based workflow, we can use the keyboard shortcuts. Which you can find in ‘help’ dropdown. It will open a list of shortcuts.

Some most useful shortcuts are listed below:

Additional Notes :

Notebook Extensions

Jupyter Notebook extensions are simple add-ons that extend the basic functionality of the notebook environment. Jupyter supports four types of extensions.

  • Kernel
  • IPyhton Kernel
  • Notebook
  • Notebook server

Installing Extension

Almost all the extensions can be installed using Python’s pip tool. If an extension can not be installed using pip, then install the extension using the below command.

jupyter nbextension install extension_name

The above only installs the extension but does not enables it. To enable it type the below command in the terminal.

jupyter nbextension enable extension_name

Conclusion

The Jupyter Notebook is quite useful not only for learning and teaching a programming language such as Python but also for sharing your data. The power of Jupyter notebook should also be evident, and we covered plenty of leads to get started exploring more advanced features in your project.

--

--