DEV Community

Cover image for Jupyter notebooks tips and tricks
Mostafa Gazar
Mostafa Gazar

Posted on • Updated on

Jupyter notebooks tips and tricks

Shortcuts

  • Shift + Enter to run a cell (code or markdown).
  • A to insert a new cell above the current cell.
  • B to insert a new cell below the current cell.
  • M to change the current cell to Markdown
  • Y to change the current cell to code.
  • D + D (twice) to delete the selected cells.

Magic commands

  • Time the execution of a single line of code or the whole cell
# Run the code multiple times and find mean runtime
%timeit CODE_LINE
%%timeit CODE_CELL

# Run once and report
%time CODE_LINE
%%time CODE_CELL
Enter fullscreen mode Exit fullscreen mode
  • Use ! prefix to run a single bash command line
  • %%bash to change the current code cell to run to bash mode, basically writing bash commands in there
%%bash

echo "hello from $BASH"
Enter fullscreen mode Exit fullscreen mode
  • %%js, %% html, %%latex, %%python2, %%python3, ... run and render code cells in that specified language or format.

  • autoreload IPython extension is exceptionally helpful when you do not want to worry about reloading modules before executing new code. In other words when you change something in a certain module the current notebook uses, changes will take place when you run new code cells without having to worry about anything.

%load_ext autoreload
%autoreload 2
Enter fullscreen mode Exit fullscreen mode
  • Embedd tensorboard in jupyter notebook
%load_ext tensorboard

%tensorboard --logdir logs/model_training_logs
Enter fullscreen mode Exit fullscreen mode
  • Finally you can list all available magics by running %lsmagic, this will show both line and cell magics currently defined.

Other

  • Sometimes you will have this memory hungry variable, you can reclaim memory by setting it to NONE and then forcing gc to run
some_var = None
gc.collect()
Enter fullscreen mode Exit fullscreen mode
  • Use sudo service jupyter restart to restart jupyter because every once in a while jupyter would throw a fit and restarting kernels will not be enough to get it back to a responsive state.
  • Add ? before almost any function, variable, ... and run the code cell to access its documentation.
  • tqdm which means "progress" in Arabic (taqadum, تقدّم) is not really related to jupyter notebooks but it can be used to show a smart progress meter. Just by wrapping any iterable with tqdm(iterable)
from tqdm import tqdm

for i in tqdm(range(10000)):
    pass
Enter fullscreen mode Exit fullscreen mode
  • When you want to count the number of files in a directory you can run the following command
!ls DIR_NAME | wc -l
Enter fullscreen mode Exit fullscreen mode

Classic notebooks extensions

There are many great extensions in jupyter_contrib_nbextensions. You should be using Jupyter lab though instead.

First you need to instead jupyter_contrib_nbextensions and then you can install various useful extensions.

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
Enter fullscreen mode Exit fullscreen mode

These are the ones I love:

  • code_prettify backed by autopep8 is great for reformatting code in notebook code cells base on PEP 8 style guide
pip install autopep8
jupyter nbextension enable code_prettify/autopep8
Enter fullscreen mode Exit fullscreen mode
  • spellchecker highlights incorrectly spelled words in Markdown cells which saved me from a few embarrassing typos.
jupyter nbextension enable spellchecker/main
Enter fullscreen mode Exit fullscreen mode
  • toggle_all_line_numbers as its names suggests it adds a toolbar button to toggle between showing line numbers or not
jupyter nbextension enable toggle_all_line_numbers/main
Enter fullscreen mode Exit fullscreen mode
  • varInspector is great for debugging python and R kernels. It displays the values of all defined variables in a floating window
jupyter nbextension enable varInspector/main
Enter fullscreen mode Exit fullscreen mode

Themes

  • dunovank/jupyter-themes has one of the best themes I came across. I tried it and then I stopped using it because I switch environments all the time so it was best for me to get used to the stock theme.
pip install jupyterthemes

# dark
jt -t onedork -fs 95 -altp -tfs 11 -nfs 115 -cellw 88% -T

# light
jt -t grade3 -fs 95 -altp -tfs 11 -nfs 115 -cellw 88% -T

# Restore default theme
jt -r
Enter fullscreen mode Exit fullscreen mode

Jupyter lab extensions

There are two extensions that I only use at the moment

jupyter labextension install @krassowski/jupyterlab_go_to_definition
Enter fullscreen mode Exit fullscreen mode
pip install --pre jupyter-lsp
jupyter labextension install @krassowski/jupyterlab-lsp

conda install -c conda-forge python-language-server
Enter fullscreen mode Exit fullscreen mode

Finally you need to rebuild the jupyter lab app

jupyter lab build
Enter fullscreen mode Exit fullscreen mode

Themes

There are many themes out there, the first customization plugin in my list is not a theme though. It is a topbar extension to quickly switch between light and dark themes

jupyter labextension install jupyterlab-topbar-extension jupyterlab-theme-toggle
Enter fullscreen mode Exit fullscreen mode

Here is a list a few themes I used recently

jupyter labextension install @telamonian/theme-darcula
jupyter labextension install @rahlir/theme-gruvbox
jupyter labextension install @kenshohara/theme-nord-extension
Enter fullscreen mode Exit fullscreen mode

Share it and get it in touch, my Twitter DMs are open!

Discussion (0)