Jupyter Tricks

Here’s a list of my top-used Juypter tricks, and what they do.

UI

I find the UI to be intuitive, Help > User Interface Tour describes more. There are command (enter by pressing the escape button or clicking outside of a cell) and edit (enter by typing in a cell) modes. You can tell you’re in edit mode if the “pencil” corner indicator is present:

It’s also faster to use the commands as listed in Help > Keyboard Shortcuts; with those you can also remove the toolbar with View > Toggle Toolbar.

jupyter notebook existing-notebook.ipynb - auto-launch an existing notebook without the Jupyter menu.

Remote Serving

Run the kernel on a beefy server, view with a browser on your laptop. You can change the ports appropriately to something high and unused.

1. On laptop, initiate SSH with a tunnel ssh -L8888:localhost:12321 vlad@my-beefy-server.com
2. On server, launch tmux if you’d like to persist the Jupyter server (useful if you need to keep running stuff and reconnect notebook later).
3. On server, jupyter notebook --no-browser --port=12321
4. On laptop, navigate to localhost:8888 in-browser.

TeX

The way I use TeX in Jupyter notebook depends on the end goal of the notebook itself.

• Embedded (Math) TeX. Here, the end product is *.ipynb notebook itself, in which case the TeX is auxiliary to the code, placed only to elucidate the math involved.
• Jupyter-prepared Reports. Here, the end product is a prepared *.pdf report, in which case the code is auxiliary to the TeX, with Jupyter used to create the source code for a more formal report or document, one you would print out.

In both of the above use cases, one may find it useful to generate rendered TeX from code

Embedded TeX

Use MathJax in Markdown cells. In the first equation you can add convenience \newcommand items if you prefer (MathJax will evaluate cells top down).

Note: The magic %%latex works, but I don’t use it. It’s treated like a code cell, but we’re really only interested in the output in this setting. Finally, you can embed images right in the Markdown, too, with <img src="path/to/image.png">.

Jupyter-prepared Reports

In this setting, use raw input cells to create segments of LaTeX code. This won’t render within the notebook, but this is OK since we’re treating the notebook like source in this setting. To generate a report (with the inline evaluated code), I use nbconvert. For prepared scripts, check out my ipython-latex repo.

Logging

Bring logging to the cell output:

import logging
logging.getLogger(some_module.that.I.want.logged).setLevel(logging.INFO)


Matplotlib

%matplotlib inline
import matplotlib.pyplot as plt
plt.rc('font', family='serif', serif='Computer Modern Roman')
plt.rc('text', usetex=True)


This preamble will render generated Matplotlib objects in the Jupyter HTML. The font specified above is consistent with the LaTeX generated by ipython-latex in Jupyter-prepared reports. Notably, this is going to differ from the MathJax font that is used inside Markdown cells.

Magic

Docs accessible with %<magic>?.

• x = !! echo hi - run a bash command in a subshell, save stdout in returned string, split on newlines (! for no split)
• %%bash - run cell as bash
• %%timeit - time cell
• ? f - get docstring
• ?? f - get source
• %run nb.ipynb - line magic, runs notebook
• %pdb - run debbuger on cell evaluation
• %env ENV_VAR=3 - set enviornment variable in kernel
• Cell code profiling.

Extensions

These are the extensions I find useful: auto-format code, toggle font size, auto-comment regions, spell check, and control the cutoff at which output starts scrolling, respectively, below.

conda install -c conda-forge jupyter_contrib_nbextensions
pip install yapf # for code-prettification
for i in hide_input/main code_prettify/code_prettify code_font_size/code_font_size comment-uncomment/main spellchecker/main autoscroll/main; do jupyter nbextension enable \$i ; done