Jupyter Notebook Enhancements, Tips And Tricks

I will start:

Go To The Current Running Cell Keyboard Shortcut

I often find myself scrolling through the running notebook, trying to find the currently running cell.

I wrote a keyboard shortcut that takes me there directly using Alt-I.

If you’d like to the same functionality add the following code to ~/.jupyter/custom/custom.js (you may need to create the folder and the file if you don’t have them already):

// Go to Running cell shortcut
Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Alt-I', {
    help : 'Go to Running cell',
    help_index : 'zz',
    handler : function (event) {
        setTimeout(function() {
            // Find running cell and click the first one
            if ($('.running').length > 0) {
                //alert("found running cell");
                $('.running')[0].scrollIntoView();
            }}, 250);
        return false;
    }
});

You can experiment with this code also by creating a code cell in your notebook and adding:

%%javascript

on the first line, and then pasting the above code on the following lines, and running the cell. it will affect only the current notebook.

I got the idea from https://stackoverflow.com/questions/44273643/how-can-i-jump-to-the-cell-currently-being-run-in-a-jupyter-notebook

wrt implementation I’m not sure if Alt-I is the best choice - suggestions are welcome.

Next, it’d be nice to have a special mode where the notebook automatically re-focuses the view on the currently running cell, so one could easily follow Run All Cells hands off. I don’t know yet enough about jupyter innards to know how to code that one, so suggestions are welcome.

Pretty Print All Cell’s Outputs (and not just the last output of the cell)

Normally only the last output in the cell gets pretty printed - the rest you have to manually add print() which is not very convenient. So here is how to change that:

At the top of the notebook add:

from IPython.core.interactiveshell import InteractiveShell

# pretty print all cell's output and not just the last one
InteractiveShell.ast_node_interactivity = "all"

Examples:

Normal behavior: only one output is printed:

In  [1]: 4+5
         5+6

Out [1]: 11

After the new setting is activated both outputs get printed:

In  [1]: 4+5
         5+6

Out [1]: 9
Out [1]: 11

To restore the original behavior add:

from IPython.core.interactiveshell import InteractiveShell

# pretty print only the last output of the cell
InteractiveShell.ast_node_interactivity = "last_expr"

note: you have to run the setting change in a separate cell for it to take effect for the next cell run.

To make this behavior consistent across all notebooks edit: ~/.ipython/profile_default/ipython_config.py:

c = get_config()

# Run all nodes interactively
c.InteractiveShell.ast_node_interactivity = "all"

The tip was found here: https://stackoverflow.com/a/36835741/9201239

Suppressing Output

The side-effect of this change is that some libraries will start spewing a lot of noise (e.g. matplotlib). To fix that add ; at the end of the lines whose output you don’t want to be displayed.

Example:

fig, axes = plt.subplots(2, 1, figsize=(20, 20))
axes[0].set_ylim(-5, 5);

Without using ; it outputs:

Out[53]: (-5, 5)

However the hack doesn’t seem to work when the code line that outputs something is part of some loop, in which case the workaround of assigning to _ does the trick:

fig, axes = plt.subplots(2, 1, figsize=(20, 20))
for i in [1,2]:
    _ = axes[i].set_ylim(-5, 5)
20 Likes