Jupyter Notebook Bases

A Notebook Document

A Notebook is a sequence of cells. There are three types of cells:

  • Code cells: The Input and Output of live code that is run in the kernel
  • Markdown cells: Text formated as a Markdown.
  • Raw cells: Unformatted text

// This cell itself is an example of Markdown

Markdown cell

Markdown cells are great way to provide documentation to the computations you're presenting.

You can read more about the markdown syntax: https://www.markdownguide.org/basic-syntax/

You can add HTML elements to any Markdown file. For example, next code:

<span style="color:">some styled text</span>

results in: some styled text

Cell Magics:

Reference: https://ipython.readthedocs.io/en/stable/interactive/magics.html

For instance, with %%HTML cell magic, you can insert a YouTube video in your notebook as an iframe in separate cell:

In [4]:
%%HTML
<iframe height="280" src="https://www.youtube.com/embed/HW29067qVWk" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Or execute bash commands, with %%bash magic:

In [24]:
%%bash

printf "The date is: `date` \n\n"

printf "And your fortune is:\n `fortune`"
The date is: Sat Oct 26 01:17:21 EEST 2019 

And your fortune is:
 Time to be aggressive.  Go after a tattooed Virgo.

Visualisations

In [40]:
import numpy as np
import matplotlib.pyplot as plt
In [41]:
X = np.arange(1,10)
Y = X**2
plt.scatter(X, Y)
Out[41]:
<matplotlib.collections.PathCollection at 0x7f844c0c7550>
In [9]:
import seaborn as sns
sns.set()

tips = sns.load_dataset("tips")

sns.relplot(x="total_bill", y="tip", col="time",
            hue="sex", style="smoker", size="size",
            data=tips);

Useful keyboard shortcuts

You can see all keyboard shortcuts by:

  • On Jupyter Notebook:
    Menu -> Help -> Keyboard Shortcuts

  • On Jupyter Lab:
    Menu->Settings->Advanced Settings Editor->Keyboard Shortcuts

But here are some of the most often used:

in both cell modes:

  • Ctrl + S - save and checkpoint
  • Shift + Enter - run the current cell, select below

in command mode:

  • Enter - take you into edit mode

  • Y - change the cell type to Code

  • M - change the cell type to Markdown

  • A - insert cell above

  • B - insert cell below

  • X - cut selected cells

  • C - copy selected cells
  • V - paste cells below
  • Shift + V - paste cells above

  • D+D - delete selected cells

  • Z - undo cell deletion

In cell edit mode:

  • Ctrl + Z - undo

  • Esc - take you into command mode

  • Tab - code completion or indent
  • Shift + Tab - tooltip and documentation
  • Ctrl + ] - indent
  • Ctrl + [ - dedent

  • Ctrl + A - select all

  • Ctrl + Z - undo

the Command Palette

One of the greatest features of Jupyter is that it supports the Command Palette which gives you fast access to all commands by fuzzy search.

Open it in Jupyter Notebook: CTRL+SHIFT+P or CTRL+SHIFT+F

In [ ]: