Some neat Jupyter tricks
This article assumes that you already know basic keyboard shortcuts for Jupyter notebooks. If you don’t, here’s a quick recap. Invest time in learning these and the ones that follow. Don’t use your mouse. It makes you slow.
0. Recap
Esc
: Toggle between command mode and edit modeA
: Insert cell aboveB
: Insert cell belowDD
: Delete cellsC
: Copy cellsX
: Cut cellsV
: Paste cellsShift + Enter / Return
Run cell
1. Restart notebook
If you want to restart your notebook just press ESC + 00
to do so.
2. Scroll your notebook
You can scroll your notebook using Space
to scroll down and Shift + Space
to scroll back up.
3. Undo cell deletion
If you delete a cell by mistake, you can undo it using ESC + Z
4. Seperate cells
A lot of times, we start by writing code in one cell, and then we want to split parts of it into multiple cells. To do so, simple place the cursor before the line you want to split, and press Control + Shift + -
5. Print Everything
When we print multiple things in Jupyter, it will only print the last thing.
To print everything, copy paste and run these lines on code in your Jupyter notebook.
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
The disadvantage of this would be that some libraries would spit out a lot of outputs. To suppress printing them just put a semi-colon at the end of the lines spitting a lot of output or just undo the above behavior by changing “all” to “last_expr”.
6. Skip a cell from running
There are multiple reasons why you might want to skip cells from running. Instead of commenting it out, deleting it or doing something else, just add a simple
%%script falseLines I don't want to run
at the top of the cell.
7. Jump to current running cell.
Copy paste these lines of code at the top of your notebook, then you can jump to the current running cell using Control + I
(You can change this)
%%javascript
// 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;
}
});
Also checkout:
- Profiling: https://jakevdp.github.io/PythonDataScienceHandbook/01.07-timing-and-profiling.html
- Diffying and merging Jupyter notebooks: https://github.com/jupyter/nbdime
- Markdown printing outputs: https://github.com/NirantK/best-of-jupyter#markdown-printing
- Searching code cells across multiple notebooks: https://github.com/NirantK/best-of-jupyter#search-magic
References:
https://forums.fast.ai/t/jupyter-notebook-enhancements-tips-and-tricks/17064/28