Jupyter Notebook

Author: Olatomiwa Bifarin

Treat (read) notebook as a draft

1 | What is Jupyter Notebook?

It is a web-based application in the form of a notebook that can be used for storing (and sharing) codes, notes, mathematical equations, and visualizations. It is one of the pivotal tools for doing data science. In this extremely short tutorial, I will go over the bare basics.

2 | Installing and Starting Jupyter

You can install Jupyter notebook via Anaconda. It is an extremely popular distribution of python. And by installing via Anaconda, you get Jupyter notebook and python with many scientific libraries installed on your system.

To start up the Jupyter Notebook server, click on the ananconda application icon, this opens up the anaconda navigator. Click on launch button of the jupyter notebook, and this opens up jupyter on your default browser with the following URL: http://localhost:8889/tree

The URL displays your local directories. Navigate through appropriate folders, and then click on 'New', then click on 'Python 3' to launch Jupyter notebook (JN).

An alternative way is to open JN via the terminal: create a subfolder on your computer. Then go to the sub-folder location via the terminal and run the command:

$ jupyter notebook

Remember this, do not close the server while you are running a notebook. It will mess things up.

3 | Creating Notebooks

Now if everything works well, you have your jupyter notebook open. Lets dive into it's anatomy, and I will love to start with cells. Actually, wait, let's give our notebook a name, a befitting one. If you look at the top of your jupyter notebook, next to the jupyter logo, you have 'Untitled' next to it, click on it. And rename as you wish.

Now let's go back to cells.

3.1 | Cells

Let see: a cell in jupyter notebook (for example, I am typing out these words in a cell) can be in at least 4 forms - 1) as a code, 2) markdown, 3) Raw NB CConvert, 4) Heading.

The default form is the code, and the applicable programming language here is the kernel you chose while creating the notebook.

To change the form of each cell, just toggle the white rectangle box, containing the keywords, at top panel of the notebook.

Each cell can be executed one at a time with shift+enter. Alternately, click on 'Cell' in the top panel menu and 'Run cells'. There are other options for running cells, pick anyone that suites you.

3.2 | Text

What I have been using is 'text in jupyter notebook', and so far it looks good. So.

For text in jupyter, 'Markdown' is appropriate.

Markdown can do you alot of good, here is a few:

  • Heading
  • Emphasis
  • Mathematical symbols
  • Line breaks
  • Colors
  • Indenting
  • Bullets
  • Numbered Lists
  • Graphics
  • Geometric shapes
  • Horizontal lines
  • Internal links
  • External links

For more details on the aforementioned, here is a really cool cheat sheet, click on it and thank me later :) : Mark Down Jupyter Notebook Cheat Sheet

3.3 | Code

Here are a few examples:

In [1]:
x = 4 +7
x
Out[1]:
11
In [2]:
x += 2
x
Out[2]:
13

Note that the output was captured below each cell.

This applies similarly for plot output too.

In [12]:
import seaborn as sns

#Load the example tips dataset
tips = sns.load_dataset("tips")
#plot
sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips);

3.4 | Menus and Toolbar

The top panel contains the menu. As of the time of writing, I can see the following:

  • File: allows you to manage your jupyter notebook, from creating new notebooks, to downloading current ones.
  • Edit: essentially helps to manipulate cells, cut, paste, merge etc.
  • View: for toggling header, line number, and toolbar; for viewing slideshows, and all sort.
  • Insert: Insert cell above or below.
  • Cell: change cell type, and run cell as you wish.
  • Kernel: Interrupt your Kernel, reconnect, restart - if you code enough, you will need to debug your notebook, then these becomes handy.
  • Help: access to all sort of references: user interface tour, Numpy, Scipy, pandas, keyboard shortcuts, etc.

The toolbar is just directly below the Menu, and the symbols are decriptive enough, I wouldn't spend time on this: a plus sign to open a new notebook, a diskette to save notebook, a scissor to cut cell, and so on.

4 | Exporting and Sharing Notebooks

You have just finished this great notebook, and then it hit you - how the hell can I share this with my boss who doesn't do Jupyter notebook or python?

Well, you could export these things. By using nbconvert tool which comes with jupyter notebook.

The nbconvert tool converts from (.ipynb) formats to other formats like HTML, latex, and PDF. However, to convert seamlessly, you might need to install Pandoc and Tex. Once all is set, you can easily go to 'Download as' under 'File', and pick the appropriate format.

5 | Resources and References

Presenting Notebooks

Your Jupyter notebook could effectively replace your power point if you know your way around. This could be done with RISE: Reveal.js - Jupyter/IPython Slideshow Extension.

Here are some resources that will help you get started.

Other Useful Resources

In [ ]: