TODO:
Extension wishlist and todo:
Inspiration:
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
In command mode:
Select the next cell: j
Merge selected cells, or current cell with cell below if only one cell selected: Shift + m
In edit mode:
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.
In [ ]:
x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)
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()
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()
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> <a href=\"#" + h_str.replace(/\s+/g, '-') + "\">" + h_str + "</a></li>";
}
}
}
toc.innerHTML += "</ol>"
In [ ]:
%run ./notebook_snippets_run_test.py
In [ ]:
%run ./notebook_snippets_run_mpl_test.py
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()
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()
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()
In [ ]:
%%time
plt.hist(np.random.normal(loc=0.0, scale=1.0, size=100000), bins=50)
In [ ]:
%%timeit
plt.hist(np.random.normal(loc=0.0, scale=1.0, size=100000), bins=50)
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)
In [ ]:
#help(ipywidgets.interact)
To me, this is the best option for single usage functions...
In [ ]:
@interact(text="IPython Widgets")
def greeting(text):
print("Hello {}".format(text))
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))
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))
In [ ]:
@interact(upper=False)
def greeting(upper):
text = "hello"
if upper:
print(text.upper())
else:
print(text.lower())
In [ ]:
@interact(name=["John", "Bob", "Alice"])
def greeting(name):
print("Hello {}".format(name))
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)
In [ ]:
@interact
def greeting(text="World"):
print("Hello {}".format(text))
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))
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))
In [ ]:
@interact
def greeting(upper=False):
text = "hello"
if upper:
print(text.upper())
else:
print(text.lower())
In [ ]:
@interact
def greeting(name=["John", "Bob", "Alice"]):
print("Hello {}".format(name))
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)
To me, this is the best option for multiple usage functions...
In [ ]:
def greeting(text):
print("Hello {}".format(text))
interact(greeting, text="IPython Widgets")
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))
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))
In [ ]:
def greeting(upper):
text = "hello"
if upper:
print(text.upper())
else:
print(text.lower())
interact(greeting, upper=False)
In [ ]:
def greeting(name):
print("Hello {}".format(name))
interact(greeting, name=["John", "Bob", "Alice"])
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})
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())
In [ ]:
from IPython.display import Image
Image("fourier.gif")
In [ ]:
from IPython.display import Audio
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)
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)
In [ ]:
Audio("http://www.nch.com.au/acm/8k16bitpcm.wav")
In [ ]:
Audio(url="http://www.w3schools.com/html/horse.ogg")
In [ ]:
#Audio('/path/to/sound.wav')
In [ ]:
#Audio(filename='/path/to/sound.ogg')
In [ ]:
#Audio(b'RAW_WAV_DATA..)
In [ ]:
#Audio(data=b'RAW_WAV_DATA..)
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)
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.
:%s/,\n "---\\n",/\r ]\r },\r {\r "cell_type": "markdown",\r "metadata": {},\r "source": [/gc