Notebook snippets, tips and tricks

TODO:

Extension wishlist and todo:

Inspiration:

Import directives


In [ ]:
%matplotlib notebook

# As an alternative, one may use: %pylab notebook

# For old Matplotlib and Ipython versions, use the non-interactive version:
# %matplotlib inline or %pylab inline

# To ignore warnings (http://stackoverflow.com/questions/9031783/hide-all-warnings-in-ipython)
import warnings
warnings.filterwarnings('ignore')

import math
import numpy as np
import matplotlib.pyplot as plt

import ipywidgets
from ipywidgets import interact

Useful keyboard shortcuts

  • Enter edit mode: Enter
  • Enter command mode: Escape

In command mode:

  • Show keyboard shortcuts: h
  • Find and replace: f
  • Insert a cell above the selection: a
  • Insert a cell below the selection: b
  • Switch to Markdown: m
  • Delete the selected cells: dd (type twice 'd' quickly)
  • Undo cell deletion: z
  • Execute the selected cell: Ctrl + Enter
  • Execute the selected cell and select the next cell: Shift + Enter
  • Execute the selected cell and insert below: Alt + Enter
  • Toggle output: o
  • Toggle line number: l
  • Copy selected cells: c
  • Paste copied cells below: v
  • Select the previous cell: k
  • Select the next cell: j

  • Merge selected cells, or current cell with cell below if only one cell selected: Shift + m

In edit mode:

  • Code completion or indent: Tab
  • Tooltip: Shift + Tab
    • Type "Shift + Tab" twice to see the online documentation of the selected element
    • Type "Shift + Tab" 4 times to the online documentation in a dedicated frame
  • Indent: ⌘] (on MacOS)
  • Dedent: ⌘[ (on MacOS)
  • Execute the selected cell: Ctrl + Enter
  • Execute the selected cell and select the next cell: Shift + Enter
  • Execute the selected cell and insert below: Alt + Enter
  • Cut a cell at the current cursor position: Ctrl + Shift + -

Matplotlib

To plot a figure within a notebook, insert the %matplotlib notebook (or %pylab notebook) directive at the begining of the document.

As an alternative, one may use %matplotlib inline (or %pylab inline) for non-interactive plots on old Matplotlib/Ipython versions.

2D plots


In [ ]:
x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)

3D plots


In [ ]:
from mpl_toolkits.mplot3d import axes3d

# Build datas ###############

x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)

xx,yy = np.meshgrid(x, y)
z = np.sin(np.sqrt(xx**2 + yy**2))

# Plot data #################

fig = plt.figure()
ax = axes3d.Axes3D(fig)
ax.plot_wireframe(xx, yy, z)

plt.show()

Animations


In [ ]:
from matplotlib.animation import FuncAnimation

# Plots
fig, ax = plt.subplots()

def update(frame):
    x = np.arange(frame/10., frame/10. + 2. * math.pi, 0.1)
    ax.clear()
    ax.plot(x, np.cos(x))

    # Optional: save plots
    filename = "img_{:03}.png".format(frame)
    plt.savefig(filename)

# Note: "interval" is in ms
anim = FuncAnimation(fig, update, interval=100)

plt.show()

Interactive plots with Plotly

Interactive plots with Bokeh

Embedded HTML and Javascript


In [ ]:
%%html
<div id="toc"></div>

In [ ]:
%%javascript
var toc = document.getElementById("toc");
toc.innerHTML = "<b>Table of contents:</b>";
toc.innerHTML += "<ol>"

var h_list = $("h2, h3");    //$("h2");    // document.getElementsByTagName("h2");
for(var i = 0 ; i < h_list.length ; i++) {
    var h = h_list[i];
    var h_str = h.textContent.slice(0, -1);  // "slice(0, -1)" remove the last character
    if(h_str.length > 0) {
        if(h.tagName == "H2") {  // https://stackoverflow.com/questions/10539419/javascript-get-elements-tag
            toc.innerHTML += "<li><a href=\"#" + h_str.replace(/\s+/g, '-') + "\">" + h_str + "</a></li>";
        } else if(h.tagName == "H3") {  // https://stackoverflow.com/questions/10539419/javascript-get-elements-tag
            toc.innerHTML += "<li> &nbsp;&nbsp;&nbsp; <a href=\"#" + h_str.replace(/\s+/g, '-') + "\">" + h_str + "</a></li>";
        }
    }
}

toc.innerHTML += "</ol>"

IPython built-in magic commands

Execute an external python script


In [ ]:
%run ./notebook_snippets_run_test.py

In [ ]:
%run ./notebook_snippets_run_mpl_test.py

Load an external python script

Load the full script


In [ ]:
# %load ./notebook_snippets_run_mpl_test.py
#!/usr/bin/env python3

# Copyright (c) 2012 Jérémie DECOCK (http://www.jdhp.org)

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""
This module has been written to illustrate the ``%run`` magic command in
``notebook_snippets.ipynb``.
"""

import numpy as np
import matplotlib.pyplot as plt

def main():
    x = np.arange(-10, 10, 0.1)
    y = np.cos(x)

    plt.plot(x, y)
    plt.grid(True)

    plt.show()

if __name__ == '__main__':
    main()

Load a specific symbol (funtion, class, ...)


In [ ]:
# %load -s main ./notebook_snippets_run_mpl_test.py
def main():
    x = np.arange(-10, 10, 0.1)
    y = np.cos(x)

    plt.plot(x, y)
    plt.grid(True)

    plt.show()

Load specific lines


In [ ]:
# %load -r 22-41 ./notebook_snippets_run_mpl_test.py

"""
This module has been written to illustrate the ``%run`` magic command in
``notebook_snippets.ipynb``.
"""

import numpy as np
import matplotlib.pyplot as plt

def main():
    x = np.arange(-10, 10, 0.1)
    y = np.cos(x)

    plt.plot(x, y)
    plt.grid(True)

    plt.show()

if __name__ == '__main__':
    main()

Time measurement

%time


In [ ]:
%%time
plt.hist(np.random.normal(loc=0.0, scale=1.0, size=100000), bins=50)

%timeit


In [ ]:
%%timeit
plt.hist(np.random.normal(loc=0.0, scale=1.0, size=100000), bins=50)

ipywidget

On jupyter lab, you should install widgets extension first (see https://ipywidgets.readthedocs.io/en/latest/user_install.html#installing-the-jupyterlab-extension):

jupyter labextension install @jupyter-widgets/jupyterlab-manager


In [ ]:
#help(ipywidgets)
#dir(ipywidgets)

In [ ]:
from ipywidgets import IntSlider
from IPython.display import display

slider = IntSlider(min=1, max=10)
display(slider)

ipywidgets.interact

Documentation


In [ ]:
#help(ipywidgets.interact)

Using interact as a decorator with named parameters

To me, this is the best option for single usage functions...

Text


In [ ]:
@interact(text="IPython Widgets")
def greeting(text):
    print("Hello {}".format(text))

Integer (IntSlider)


In [ ]:
@interact(num=5)
def square(num):
    print("{} squared is {}".format(num, num*num))

In [ ]:
@interact(num=(0, 100))
def square(num):
    print("{} squared is {}".format(num, num*num))

In [ ]:
@interact(num=(0, 100, 10))
def square(num):
    print("{} squared is {}".format(num, num*num))

Float (FloatSlider)


In [ ]:
@interact(num=5.)
def square(num):
    print("{} squared is {}".format(num, num*num))

In [ ]:
@interact(num=(0., 10.))
def square(num):
    print("{} squared is {}".format(num, num*num))

In [ ]:
@interact(num=(0., 10., 0.5))
def square(num):
    print("{} squared is {}".format(num, num*num))

Boolean (Checkbox)


In [ ]:
@interact(upper=False)
def greeting(upper):
    text = "hello"
    if upper:
        print(text.upper())
    else:
        print(text.lower())

List (Dropdown)


In [ ]:
@interact(name=["John", "Bob", "Alice"])
def greeting(name):
    print("Hello {}".format(name))

Dictionnary (Dropdown)


In [ ]:
@interact(word={"One": "Un", "Two": "Deux", "Three": "Trois"})
def translate(word):
    print(word)

In [ ]:
x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)

@interact(function={"Sin": np.sin, "Cos": np.cos})
def plot(function):
    y = function(x)
    plt.plot(x, y)

Using interact as a decorator

Text


In [ ]:
@interact
def greeting(text="World"):
    print("Hello {}".format(text))

Integer (IntSlider)


In [ ]:
@interact
def square(num=2):
    print("{} squared is {}".format(num, num*num))

In [ ]:
@interact
def square(num=(0, 100)):
    print("{} squared is {}".format(num, num*num))

In [ ]:
@interact
def square(num=(0, 100, 10)):
    print("{} squared is {}".format(num, num*num))

Float (FloatSlider)


In [ ]:
@interact
def square(num=5.):
    print("{} squared is {}".format(num, num*num))

In [ ]:
@interact
def square(num=(0., 10.)):
    print("{} squared is {}".format(num, num*num))

In [ ]:
@interact
def square(num=(0., 10., 0.5)):
    print("{} squared is {}".format(num, num*num))

Boolean (Checkbox)


In [ ]:
@interact
def greeting(upper=False):
    text = "hello"
    if upper:
        print(text.upper())
    else:
        print(text.lower())

List (Dropdown)


In [ ]:
@interact
def greeting(name=["John", "Bob", "Alice"]):
    print("Hello {}".format(name))

Dictionnary (Dropdown)


In [ ]:
@interact
def translate(word={"One": "Un", "Two": "Deux", "Three": "Trois"}):
    print(word)

In [ ]:
x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)

@interact
def plot(function={"Sin": np.sin, "Cos": np.cos}):
    y = function(x)
    plt.plot(x, y)

Using interact as a function

To me, this is the best option for multiple usage functions...

Text


In [ ]:
def greeting(text):
    print("Hello {}".format(text))
    
interact(greeting, text="IPython Widgets")

Integer (IntSlider)


In [ ]:
def square(num):
    print("{} squared is {}".format(num, num*num))

interact(square, num=5)

In [ ]:
def square(num):
    print("{} squared is {}".format(num, num*num))

interact(square, num=(0, 100))

In [ ]:
def square(num):
    print("{} squared is {}".format(num, num*num))

interact(square, num=(0, 100, 10))

Float (FloatSlider)


In [ ]:
def square(num):
    print("{} squared is {}".format(num, num*num))

interact(square, num=5.)

In [ ]:
def square(num):
    print("{} squared is {}".format(num, num*num))

interact(square, num=(0., 10.))

In [ ]:
def square(num):
    print("{} squared is {}".format(num, num*num))

interact(square, num=(0., 10., 0.5))

Boolean (Checkbox)


In [ ]:
def greeting(upper):
    text = "hello"
    if upper:
        print(text.upper())
    else:
        print(text.lower())

interact(greeting, upper=False)

List (Dropdown)


In [ ]:
def greeting(name):
    print("Hello {}".format(name))

interact(greeting, name=["John", "Bob", "Alice"])

Dictionnary (Dropdown)


In [ ]:
def translate(word):
    print(word)

interact(translate, word={"One": "Un", "Two": "Deux", "Three": "Trois"})

In [ ]:
x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)

def plot(function):
    y = function(x)
    plt.plot(x, y)

interact(plot, function={"Sin": np.sin, "Cos": np.cos})

Example of using multiple widgets on one function


In [ ]:
@interact(upper=False, name=["john", "bob", "alice"])
def greeting(upper, name):
    text = "hello {}".format(name)
    if upper:
        print(text.upper())
    else:
        print(text.lower())

Display images (PNG, JPEG, GIF, ...)

Within a code cell (using IPython.display)


In [ ]:
from IPython.display import Image
Image("fourier.gif")

Within a Markdown cell

Use the following Markdown syntax: ![title](imga.gif)

Sound player widget


In [ ]:
from IPython.display import Audio

Generate a sound


In [ ]:
framerate = 44100
t = np.linspace(0, 5, framerate*5)
data = np.sin(2*np.pi*220*t) + np.sin(2*np.pi*224*t)

Audio(data, rate=framerate)

Generate a multi-channel (stereo or more) sound


In [ ]:
data_left = np.sin(2 * np.pi * 220 * t)
data_right = np.sin(2 * np.pi * 224 * t)

Audio([data_left, data_right], rate=framerate)

From URL


In [ ]:
Audio("http://www.nch.com.au/acm/8k16bitpcm.wav")

In [ ]:
Audio(url="http://www.w3schools.com/html/horse.ogg")

From file


In [ ]:
#Audio('/path/to/sound.wav')

In [ ]:
#Audio(filename='/path/to/sound.ogg')

From bytes


In [ ]:
#Audio(b'RAW_WAV_DATA..)

In [ ]:
#Audio(data=b'RAW_WAV_DATA..)

Youtube widget

Class for embedding a YouTube Video in an IPython session, based on its video id. e.g. to embed the video from https://www.youtube.com/watch?v=0HlRtU8clt4 , you would do:

See https://ipython.org/ipython-doc/dev/api/generated/IPython.display.html#IPython.display.YouTubeVideo


In [ ]:
from IPython.display import YouTubeVideo

In [ ]:
vid = YouTubeVideo("0HlRtU8clt4")
display(vid)

Convert a Reveal.js presentation written with Markdown to a Jupyter notebook

This is a quick and dirty hack to have one cell per slide in the notebook; it assumes the string "---" is used to separate slides within the markdown file.

  1. copy the markdown document within the Jupyter notebook (in a Markdown cell), save it and close it;
  2. to split this cell at each "---", open the ipynb notebook with vim and enter the following command and save the file:
:%s/,\n    "---\\n",/\r   ]\r  },\r  {\r   "cell_type": "markdown",\r   "metadata": {},\r   "source": [/gc