Cover image for Jupyter notebooks tips and tricks

Jupyter notebooks tips and tricks

mgazar_ profile image Mostafa Gazar Updated on ・4 min read


  • 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_CELL
  • 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

echo "hello from $BASH"
  • %%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
  • Embedd tensorboard in jupyter notebook
%load_ext tensorboard

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


  • 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
  • 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)):
  • When you want to count the number of files in a directory you can run the following command
!ls DIR_NAME | wc -l

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

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
  • spellchecker highlights incorrectly spelled words in Markdown cells which saved me from a few embarrassing typos.
jupyter nbextension enable spellchecker/main
  • 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
  • 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


  • 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

Jupyter lab extensions

There are two extensions that I only use at the moment

jupyter labextension install @krassowski/jupyterlab_go_to_definition
pip install --pre jupyter-lsp
jupyter labextension install @krassowski/jupyterlab-lsp

conda install -c conda-forge python-language-server

Finally you need to rebuild the jupyter lab app

jupyter lab build


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

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

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

Posted on by:

mgazar_ profile

Mostafa Gazar


Android Pro, built million-downloads app, Y-Combinator alumni. I write about Machine Learning and Mobile.


Editor guide