CV4GS - Jupyter Notebook tutorial
In this tutorial, we will cover:
Code cells
y
to turn cell into code cellMarkdown cells
m
to turn cell into markdown cellRaw cells
(rarely used)r
to turn cell into raw cellThe cell below is a Code cell
(default type) with Python code.
Press shift + return
to execute it. The result will get rendered beneath the cell.
x = 1
y = 2
print(x+y)
3
Variables are shared between cells. Try executing the cell below:
print(y + 2)
4
Figures will be plotted in the cell:
from matplotlib import pyplot as plt
x = [1,2,3,4,5]
y = [6,7,8,9,10]
plt.plot(x, y)
[<matplotlib.lines.Line2D at 0x7f5f50fc29a0>]
Markdown
is a lightweight markup language* for creating formatted text.
"A markup language is a system for annotating a document in a way that is syntactically distinguishable from the text, meaning when the document is processed for display, the markup language is not shown, and is only used to format the text." (wikipedia)
Examples: HTML, LaTeX, RTF, etc.
Key design goal of Markdown
language is readability, ommiting obvious tags and formatting instructions like those used by HTML, RTF, etc.
Esc
& Enter
: switch to command or edit modem
=> switch cell to markdown typey
=> switch cell to code type
Shift-Enter
: execute cell, jump to cell below (or create a new one if last cell is selected)
Ctrl-Enter
: execute cell, insert new cell below
b
=> add below the current one (Note you have to be in esc
mode.)
a
=> add above the current one (Note you have to be in esc
mode.)d+d
=> delete cell (Note you have to be in esc
mode.)o
=> hide/show cell outputctrl+shift+minus
: split cell
shift+tab
: open documentation of a function
f
: find text in cell/notebook (Note you have to be in esc
mode.)alt+c
: comment code (if 'comment-uncomment' jupyter extension is enabled)ctrl+\
: comment code (american keyboard layout)ctrl+}
: comment code (latin american keyboard layout)0+0
=> restart notebook kernel the current one (Note you have to be in esc
mode.)ctrl+s
=> save notebookMagic commands are special commands which add special functionalities to the jupyter notebook interface.
Below is a selection of some useful commands.
List currently available magic functions.
%lsmagic
Available line magics: %alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %conda %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics.
Execute a python script.
%run <my_script.py>
Insert code from an external file into cell.
%load <my_script.py>
Export the contents of a cell/Show the contents of an external script
%%writefile pythoncode.py
import numpy
a = 1
b = 2
def add(x,y)
return x+y
c = add(a, b)
Writing pythoncode.py
Time the execution of the code.
%time
import random
for i in range(0, 1000000):
random.random()
CPU times: user 0 ns, sys: 0 ns, total: 0 ns Wall time: 8.11 µs
Select how to display matlpotlib graphs.
List the available backends:
Note: not all will work, it depends on your installation
%matplotlib --list
Available matplotlib backends: ['tk', 'gtk', 'gtk3', 'wx', 'qt4', 'qt5', 'qt', 'osx', 'nbagg', 'notebook', 'agg', 'svg', 'pdf', 'ps', 'inline', 'ipympl', 'widget']
Plot static figure embedded in notebook (default setting):
%matplotlib inline
from matplotlib import pyplot as plt
x = [1,2,3,4,5]
y = [6,7,8,9,10]
plt.plot(x, y)
[<matplotlib.lines.Line2D at 0x7ff98343f2e0>]
Plot interactive figure:
%matplotlib notebook
from matplotlib import pyplot as plt
x = [1,2,3,4,5]
y = [6,7,8,9,10]
plt.plot(x, y)
Reload modules before executing user code.
%autoreload 0 => Disable automatic reloading.
%autoreload 1 => Reload all modules imported every time before executing the Python code typed.
%autoreload 2 => Reload all modules every time before executing the Python code typed
%aimport => List modules which are to be automatically imported or not to be imported.
%load_ext autoreload
%autoreload 2
from foo import some_function
# Run function
some_function()
# Open foo.py in an editor and change some_function to return 43
# Run function after edit => will reflect the changes and return 43
some_function()
Note: the following example requires to have the cython package installed
# --- Python implementation of primes():
def primes():
result = []
p = [0] * 1000
kmax = 1000
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
result.append(n)
n = n + 1
return result
%timeit primes()
58.4 ms ± 1.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# --- Cython implementation of primes():
%load_ext Cython
%%cython
def primes():
cdef int n, k, i #<--- defines integer variables
cdef int p[1000] #<--- defines integer array
result = []
cdef int kmax = 1000 #<---
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
result.append(n)
n = n + 1
return result
%timeit primes()
2.16 ms ± 3.85 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Example: list files in working directory
!ls -la
Example: test python version, and cython package version
!python --version
!conda list | grep cython
Example: install additional package
!conda install <package_name>
!pip install <package_name>
IPython can run various Kernel in a cell: bash
, HTML
, ruby
, perl
, python2
, python3
, etc.
Example: run bash code
%%bash
for i in {1..5}
do echo "i = $i"
done
i = 1 i = 2 i = 3 i = 4 i = 5
%%python2
for i in xrange(5):
print i
0 1 2 3 4
%%python3
for i in range(5):
print(i)
0 1 2 3 4
import ipywidgets as widgets
slider = widgets.FloatSlider(
value=7.5,
min=5.0,
max=10.0,
step=0.1,
description='Input:',
)
slider
FloatSlider(value=7.5, description='Input:', max=10.0, min=5.0)
print(slider.value)
7.5
EX: set slider, and display value in a text box.
# Create text box to hold slider value
text = widgets.FloatText(description='Value')
# Link slider value and text box value
widgets.link((slider, 'value'), (text, 'value'))
# Put them in a vertical box
widgets.VBox([slider, text])
VBox(children=(FloatSlider(value=8.3, description='Input:', max=10.0, min=5.0), FloatText(value=8.3, descripti…
def f(x):
print(x * x)
widgets.interact(f, x=(0, 100));
interactive(children=(IntSlider(value=50, description='x'), Output()), _dom_classes=('widget-interact',))
slider = widgets.FloatSlider(
value=7.5,
min=5.0,
max=10.0,
step=0.1,
description='Input:',
)
# Create non-editable text area to display square of value
square_display = widgets.HTML(description="Square: ", value='{}'.format(slider.value**2))
# Create function to update square_display's value when slider changes
def update_square_display(change):
square_display.value = '{}'.format(change.new**2)
slider.observe(update_square_display, names='value')
# Put them in a vertical box
widgets.VBox([slider, square_display])
VBox(children=(FloatSlider(value=7.5, description='Input:', max=10.0, min=5.0), HTML(value='56.25', descriptio…
Installation example (from terminal using Anaconda's package manager):
$ conda install -c conda-forge bqplot
Example: binary star simulation dashboard using ipywidgets
, pythreejs
, and bqplot
: https://github.com/JuanCab/AstroInteractives
Notebooks can be converted to a number of formats, using the command from terminal: jupter nbconvert --to <desired format> <notebook.ipynb>
$ jupyter nbconvert --to slides mynotebook.ipynb
=> install extension "Remote - SSH"
=> https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh
$ ssh user@ip
$ jupyter notebook --no-browser --port=8889
$ ssh -L localhost:8888:localhost:8889 user@ip
http://localhost:8888/tree/