Jupyter notebooks are an interactive medium for doing data analysis. It is very popular amongst data science community. For a beginner it may be very simple and easy way to start but it has a lot more to offer. In this blog, we’ll discuss a very powerful interactive computing environment Jupyter Notebook that helps us to create documents with live code, interactive plots, markdown text, images, etc. Project Jupyter is a non-profit, open-source software. It came out of the IPython project in 2014, as it evolved to support interactive data science and scientific computing in all programming languages. Jupyter is developed on Github in the open, unanimously from the Jupyter community.

Jupyter Notebook is quite popular amongst the data science community. You can directly run it from the browser, edit it, and export it in different formats such as Python script, HTML page, pdf document, etc. It is a nice way to present any data science project. In this article, we’ll see how we can install it in our local machine and start using it for data science projects.

Let’s start by installing it in our machine. There are two ways to get started with Jupyter notebooks. One way is by installing Anaconda and another one by directly installing the package using pip. In order to install it using Anaconda:

  1. We need to download the latest version of Anaconda for Python 3.
  2. Now install it in your local machine by following the instructions on the download page.

Anaconda comes with many pre-installed libraries and tools like pandas, numpy, matplotlib, etc. It helps us to avoid installing different packages separately.

The next option is to install using pip command. In your local machine, Python has to be installed. If not, you can download it from there website. Then open a terminal and run below command:

pip install jupyter

And that’s it. You have successfully installed Jupyter notebook in your local machine. Let’s see how to create our first notebook and save it. In order to open Jupyter, go to the terminal and run jupyter notebook command. It’ll open a window like this in your browser.

alt text

This is the Jupyter dashboard where you can find the jupyter notebooks of the current working directory. If you want to launch it from another folder, just change directory in the terminal and run jupyter notebook command from there. This dashboard shows http://localhost:8888/tree as the URL, which means the content is being served from the local machine. You can make yourself familiar with the interface. It’s quite simple and self-explanatory. On the right side there is New button. By clicking on it you’ll see few options like Python 3, Text file, terminal, folder etc.

alt text

Click on Python 3 to create a new Jupyter notebook. This new Jupyter Notebook will open in a new tab named as Untitled.ipynb Every .ipynb file is a text file that describes the contents of our jupyter notebook in a JSON format. Inside the notebook, we’ll create cells to add code into it. A cell contains the code to be executed by the notebook’s kernel. We can execute a cell by Ctrl + Enter command or by clicking on the >| Run icon on top of the menu bar. As soon as we run it, we can see the output below the cell.

In the menubar, there is a drop-down, which has Code in it as default. If we click on it, we see other options like Markdown, Heading etc. On selecting Markdown, our cell becomes a Markdown cell where we can use Markdown syntax also. On top of the notebook, it has its name. By default, it takes Untitled, which can be changed by clicking on it and renaming it. In the second toolbar, there is Save icon. By clicking on it, we can save our work. By default, it saves the checkpoint after every 120 seconds.

In the next section, we’ll discuss how we can utilize its tricks for enhancing our experience. Following are the tips and trick we can use while using Jupyter notebooks:

  1. Convert Jupyter notebook into other formats using nbconvert:

nbconvert gives us the ability to convert the jupyter notebook into other formats. For example we can export it into a python script using following command:

jupyter nbconvert --to script NOTEBOOK_NAME.ipynb

Similarly we can export it as a html page also.

jupyter nbconvert --to html NOTEBOOK_NAME.ipynb

  1. Important Shortcuts

    • Shift + Enter for running the current cell
    • Ctrl + S save the notebook
    • A for inserting cell above the current cell
    • B for inserting cell below the current cell
    • D + D(D twice) for deleting the current cell
    • Z for undo cell deletion
  2. Install packages

In order to install python packages we can use the following syntax:

!pip install <package_name>

  1. Sharing the code

We can share the code directly from Jupyter notebook:

%pastebin commands creates a URL which we can share with others.

  1. Plot charts and graphs inside the notebook

%matplotlib inline gives us the plots inside the jupyter notebook only.

  1. Multicursor like sublime

Multicursor can be enabled by draging your mouse while holding down Alt.

  1. nbextensions

There are many extensions available for jupyter notebook which enhance its experience. Let’s see how we can utilize nbextensions. First we need to install it.

# Install nbextensions
pip install jupyter_contrib_nbextensions
jupyter contrib nbextensions install 

After installing, we need to restart the Jupyter notebook, and we can see a new tab Nbextensions added to the menu bar. Let’s see few extensions which are very useful.

a. Code Prettify - It is used to format and indent the code. Shortcut key is `ctrl + L`.

b. Hinterland - It is used to autocomplete the code as we type without using tab everytime. It saves the time a lot.
  1. Documentaion

We can see the docstring in jupyter notebook using Shift + Tab.

  1. High-resolution plots

We have all tried to plot the charts in Jupyter notebook and face the problem of blurry images. We can solve it by setting the config in Jupyter notebook like this:

%config InlineBackend.figure_format = 'retina'

  1. Time taken by a task

We all have tried to print time taken by a task by declaring start time before the code and subtract the current time to get the run time. We can make it even easier by using the following command:

%time function_name()


%timeit function_name()

I hope you guys liked this article. These tricks can surely improve your productivity. Thanks for reading and we’ll meet in our next blog :)