Jupyter Notebook Tips
Magics
Manage environment variables
%env NUM_THREADS=4
1Get details
%timeit? list?
1
2Measure execution time
# Measure entire block %%time # Measure a single line %timeit func()
1
2
3
4Execute other python/notebook
# Excute code in demo.ipynb (can be .py) %run ./demo.ipynb # Load content in demo.py to the cell %load ./demo.py
1
2
3
4Store data between notebooks
data = 'this is the string I want to pass to different notebook' %store data del data # Read `data` in another notebook %store -r data # variable name must be the same
1
2
3
4
5
6Write cell code to python file
%%writefile pythoncode.py import numpy def append_if_not_exists(arr, x): if x not in arr: arr.append(x) def some_useless_slow_function(): arr = list() for i in range(10000): x = numpy.random.randint(0, 10000) append_if_not_exists(arr, x)
1
2
3
4
5
6
7
8
9
10
11Display file content
%pycat pythoncode.py
1Profiling
# Profile run time %prun func() # Profile run time by line # !pip install line_profiler %load_ext line_profiler %lprun func() # Profile memory # !pip install memory_profiler %load_ext memory_profiler %mprun func() # Measure the memory use of a single statement %memit func()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Debugging
%debug # launch ipdb debugger %pdb # set breakpoint (== `import ipdb; ipdb.set_trace()`)
1
2Use other kernels for the cell
%%python2 %%python3 %%ruby %%perl %%bash %%R
1
2
3
4
5
6
Tools
Write fast code using Cython
!pip install cython %load_ext Cython %%cython def myltiply_by_2(float x): return 2.0 * x myltiply_by_2(23.) # 46.0
1
2
3
4
5
6
7
8Install Jupyter Contrib Extensions
pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master pip install jupyter_nbextensions_configurator jupyter contrib nbextension install --user jupyter nbextensions_configurator enable --user
1
2
3
4RISE: jupyter notebook presentation
conda install -c damianavila82 rise # recommended pip install RISE # less recommended
1
2Display media
from IPython.display import display, Image display(Image('demo.jpg'))
1
2Connect Github to Binder to display notebooks to others