Jupyter notebooks tips and tricks
Tags: jupyter, python, productivity, tips
Published: 2019-11-19 01:26:29

## Shortcuts
- <kbd>Shift</kbd> + <kbd>Enter</kbd> to run a cell (code or markdown).
- <kbd>A</kbd> to insert a new cell above the current cell.
- <kbd>B</kbd> to insert a new cell below the current cell.
- <kbd>M</kbd> to change the current cell to Markdown
- <kbd>Y</kbd> to change the current cell to code.
- <kbd>D</kbd> + <kbd>D</kbd> (twice) to delete the selected cells.

---

## Magic commands
- Time the execution of a single line of code or the whole cell
```shell
# 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
```

- 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
```shell
%%bash

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.
```shell
%load_ext autoreload
%autoreload 2
```
- Embedd tensorboard in jupyter notebook
```shell
%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.

---

## Other
- Sometimes you will have this memory hungry variable, you can reclaim memory by setting it to `NONE` and then forcing gc to run 
```python
some_var = None
gc.collect()
```
- 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`](https://tqdm.github.io/) 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)`
```python
from tqdm import tqdm

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

## Classic notebooks extensions
There are many great extensions in [jupyter_contrib_nbextensions](https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions.html). You should be using **Jupyter lab** though instead.

First you need to instead `jupyter_contrib_nbextensions` and then you can install various useful extensions.
```shell
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](https://www.python.org/dev/peps/pep-0008/)
```shell
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.
```shell
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
```shell
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
```shell
jupyter nbextension enable varInspector/main
```

### Themes
- *[dunovank/jupyter-themes](https://github.com/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.
```shell
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
- *[krassowski/jupyterlab-go-to-definition](https://github.com/krassowski/jupyterlab-go-to-definition)* which allow me to use <kbd>Alt</kbd> + <kbd>click</kbd> to jump to a definition using your mouse, or <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>B</kbd> keyboard-only alternative.
```shell
jupyter labextension install @krassowski/jupyterlab_go_to_definition
```
- *[krassowski/jupyterlab-lsp](https://github.com/krassowski/jupyterlab-lsp)* adds support for code navigation + hover suggestions + linters + autocompletion. Check out their [full list of supported language servers](https://github.com/krassowski/jupyterlab-lsp/blob/master/LANGUAGESERVERS.md)
```shell
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
```shell
jupyter lab build
```

### 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
```shell
jupyter labextension install jupyterlab-topbar-extension jupyterlab-theme-toggle
```

Here is a list a few themes I used recently
```shell
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](https://twitter.com/mgazar_) are open!