Everyone loves a good Jupyter Notebook. Jupyter Notebooks are an insanely convenient environment to rapidly prototype Python scripts and delve into Data Science. They speed up the time from writing code to actually executing it and you can visually see the output for each section you write.
I make heavy use Jupyter Notebooks in my day-job, not just for data analysis, but also for testing logic and quickly mocking up functions. Using them extensively everyday and having the personality that I have, has allowed me to optimise my workflow and learn the maybe lesser-known functions of Jupyter Notebooks. You too can quickly learn these easy-to-use tips, especially with the help of the FREE PDF cheatsheet available to download at the bottom of this article. Also, at the end of each section there is a bullet pointed summary for your convenience. Productivity boosts guaranteed!
Gaining Power Over the Cells
Probably the biggest thing holding most people back when using a computer in general is the mouse. Unless you have the ridiculous mouse speed and accuracy of a StartCraft player, then constantly using the mouse is probably slowing you down quite a bit. Luckily for mere mortals like you and I Jupyter Notebooks have us covered.
The Two Modes
If you have ever used Vim, then you will understand the concept of have multiple modes of operation, one for writing text and one for using a vast array of commands (and more, but this isn’t about Vim). Jupyter Notebooks also have multiple modes; ‘Command Mode‘ and ‘Edit Mode‘.
To enter Edit Mode simply hit ENTER while the cell you want to edit is selected. To Exit Edit Mode into Command mode, hit ESC.
Moving Up and Down and Selecting Cells
To navigate up and down we need to be in Command Mode, so first hit ESC to make sure you are not in Edit Mode. Then navigating up and down is as simple as pressing ↑ and ↓ on the arrow keys, or for the more Vim (and efficiency) inclined, you can also use J and K.
You can select cells in almost exactly the same way, except with the addition of shift. To select cells below you can use SHIFT-J or SHIFT-↓ and to select cells above you can use SHIFT-K or SHIFT-↑.
Executing Cells
One of the great things about Jupyter Notebooks is that they allow you to write lines or blocks of code and then execute them immediately.
To execute a cell and move onto the next, again make sure you are in Command Mode and then press SHIFT-ENTER. This will run all of the code in the currently selected cell and then move the selection to the next.
However there are times for example, when you are debugging code or testing logic and you don’t want to move onto the next cell. To do this press CTRL-ENTER. This will execute all of the code in the currently selected cell, and stay selected on that cell.
Adding New Cells
There are (lots) of times when you need to quickly add a cell somewhere in a notebook above or below an existing cell. This again is simple; select the cell you want to add another above or below in Command Mode, and press A for Above or B for Bellow.
Deleting, Cutting, Copying and Pasting Cells
Sometimes you will make temporary cells to try something out or have the need to move cells to a different location. This can easily be done in Jupyter Notebooks.
To delete a cell, simply DOUBLE-TAP D in Command Mode. This will delete the currently selected cell which can be undone by pressing Z. If you want to move a cell to a new location however, you can press X to cut a cell, or C to copy a cell and then V to paste it below the currently selected cell.
Merging Cells
One thing I often finding myself doing, is developing code in lots of separate cells and testing the different components separately. This is great until you have the finished code and are just left with tonnes of superfluous cells. When this happens I like to merge my cells together.
To merge cells together press SHIFT-M in Command Mode. If you have only one cell selected, it will merge with the cell below. If you have multiple cells selected it will merge those cells together.
Changing Cell Types
As you may or may not know, Jupyter Notebooks have different types of cells; Code, Markdown and Raw. I find that using the menu bar to switch really breaks my flow. Luckily there are keyboard shortcuts for these too.
To switch between these types, select a cell in Command Mode and hit Y for Code, M for Markdown and R for Raw.
Summary
- ESC – Enter Command Mode / Exit Edit Mode
- ENTER – Enter Edit Mode / Exit Command Mode
- ↓/U – Move a cell down
- ↑/K – Move a cell up
- SHIFT-↓/SHIFT-U – Select a cell down
- SHIFT-↑/SHIFT-K – Select a cell up
- SHIFT-ENTER – Execute cell and move to net
- CTRL-ENTER – Execute cell and remain in cell
- A – Create new cell above
- B – Create new cell below
- DD – Delete selected cell
- Z – Undo deletion
- X – Cut selected cell
- C – Copy selected cell
- V – Paste cut/copied cell
- SHIFT-M – Merge cells
- Y – Convert cell to a Code cell
- M – Convert cell to a Markdown cell
- R – Convert cell to a Raw cell
Unlocking Your Notebook’s Magic Powers
Jupyter Notebooks have some powerful, convenient little numbers called magic commands built into them. These commands allow you to quickly and easily access useful tools and functions to make your notebook life easier.
Squashing Bugs
In order to help squash those pesky bugs that pop up in your code, you can access the Python debugger within a Jupyter Notebook using the %debug magic command.
When you run a cell and an error pops up, go to the next cell below the erroneous one, type in %debug and execute it. This will run the debugger in the output area below the cell. Type q at any time to quit it.
Timing Your Code
There are two magic functions that you can use to time your code; %time and %timeit.
%time followed by the function or code you want to time will run said function or code and tell you how long it took to execute. %timeit followed by the function or code that you want to time however, will run the code many times and tell you the most accurate run time.
Running Files
You can run files from within a Jupyter Notebook using the magic command %run. This is useful as using this, you can run other Python scripts.
Accessing the Shell
In Jupyter Notebooks, the shell can be accessed and commands can be executed by typing ! before the command you want to execute, for example !pwd to print the current working directory.
Summary
There are many more magic commands available in Jupyter Notebooks and many useful ways you can customise the ones previously mentioned too. For more information on magic commands, go here.
- %debug – Run the Python debugger from within a notebook
- %time – Time code
- %timeit – Run code many times, timing it and getting the most accurate time
- %run – Run external scripts from within a notebook
- ! – Execute bash commands from within a notebook
Addition:
As Ryan pointed out in the comments, there is a distinction between % and %% when using magic commands.
A small distinction to add would be the that ‘%’ prefix to magics indicates a magic in line mode and a ‘%%’ prefix indicates a magic in cell mode. I.e., you can use ‘%%timeit’ to get the time execution of an entire cell.
Final Golden Nuggets
Editing the Notebooks HTML
You can change how your notebook looks by directly editing the HTML that produces it. For example in the code below, we make it so the cells within the notebook take up the full width of your browser window. The HTML can of course be changed to whatever you like.
from IPython.core.display import display, HTML display(HTML("<style>.container { width:100% !important; }</style>"))
Accessing Django Apps
Django apps can be accessed from within Jupyter Notebooks via the code below. This is especially useful and you can make use of Django’s QuerySets and access your apps Database.
import sys import os os.chdir('/var/hats/releases/current/hats') sys.path.append('/var/hats/releases/current/hats/hats') import django django.setup()
Summary
# Edit the Notebook's HTML from IPython.core.display import display, HTML display(HTML("<style>.container { width:100% !important; }</style>"))
# Accessing on Django apps import sys import os os.chdir('/path/to/django/app') sys.path.append('/path/to/django/app') import django django.setup()
The FREE PDF
As promised here’s your free Jupyter notebook cheatsheet in PDF format. Enjoy and by all means share it around, tweet it, email it, redistribute it and do what you like with it. Sharing is caring!
‘Ello, I’m Jamal – a Tokyo-based, indie-hacking, FinTech software developer with a dependence on data.
I write Shakespeare-grade code, nowadays mostly in Python and JavaScript and productivity runs in my veins.
I’m friendly, so feel free to say hello!