The world’s leading publication for data science, AI, and ML professionals.

5 Jupyter Notebook Tricks I Only Discovered 2 Years Into My Data Science Career

Custom keyboard shortcuts, highlighting text, and more

Despite their popularity amongst users of R, Python and Julia, Jupyter Notebooks are rarely used to their full potential.

Most users know the basic commands (execute code, comment, save, etc.), but few make use of Jupyter’s hidden tricks – even though these can save significant time and effort.

Since 2019 (when I started using Jupyter), I’ve discovered lots of time-saving tricks and tips that most beginners are unaware of. In this article, I’ll show you 5 of my favourites:

  1. Keyboard shortcuts – "off-the-shelf" commands such as inserting/deleting/moving cells and text
  2. Custom keyboard shortcuts – add in your own advanced commands like moving cells up/down or restarting the kernel and running up to the current cell, right from the keyboard
  3. Markdown formatting – create tables, format text and create checkboxes
  4. HTML formatting – highlight text & make your comments stand out
  5. "Enable scrolling for outputs" – suppress lengthy cell results (this is a goldmine when tuning hyperparameters)

Tip 1: Keyboard shortcuts

Keyboard shortcuts provide a nifty way to navigate around a Jupyter Notebook and execute commands. Here are the main ones I use:

Run cells

  • Shift + Enter – run the current cell AND select the cell below
  • Ctrl/Cmd + Enter – run the current cell
  • Option/Alt + Enter – run the current cell and insert another cell below

Save progress

  • Ctrl/Cmd + s— save the notebook

Insert/delete cells

First, click inside a cell, and then make sure you’re in command mode by pressing Esc. If you don’t press Esc, you’ll be in edit mode and only able to perform actions on contents of a cell (rather than the cells themselves). Once you’re in command mode, the cursor in the cell will stop blinking. Then, press one of the following:

  • a – insert a new cell above the current cell
  • dd (press d key twice) – delete the current cell
  • b – insert a new cell below the current cell

Change cell type

One of the joys of Jupyter Notebooks is that they allow you to place comments and code side-by-side. To set a cell’s type and determine whether it should treat text as "comment" or "code," start by entering command mode by selecting a cell and pressing Esc. Then, press one of the following:

  • m – markdown mode (for writing comments and headers)
  • y – code mode (for writing code)

Selecting multiple cells

Once again, make sure you’re in command mode, and then, while holding Shift, use the Up or Down arrow to expand the selection to as many cells as you like.

Once you’ve selected all the cells you want, you can delete or move them all at the same time, rather than having to do it individually.

Tip 2: Custom keyboard shortcuts

This was a game-changer for me.

In addition to the standard keyboard shortcuts which are pre-defined for you when you install Jupyter, you can create your own custom shortcuts for executing advanced commands.

For example, let’s say I want to define two new keyboard shortcuts: one for moving a cell up, and another for moving a cell down. By default, it’s possible to do this by "dragging-and-dropping" the cells using the mouse, but it’s also possible to create a custom keyboard shortcut that can do this for you at the touch of a button (or two).

First, click ‘Settings’ and then ‘Advanced Settings Editor’ in your JupyterLab window, and you’ll be able to see all the existing keyboard shortcuts.

To add custom keyboard shortcuts, you’ll need to click the ‘JSON Settings Editor’ in the top right of the Advanced Settings Editor window (shown in the image above).

Then, on the ‘User Preferences’ tab on the right, scroll down to the list of keyboard shortcuts and add in the desired shortcuts. I’ve added in these two, which were suggested by David on StackOverflow.

{
    "command": "notebook:move-cell-up",
    "keys": [
        "Ctrl Shift ArrowUp"
    ],
    "selector": ".jp-Notebook:focus"
},
{
    "command": "notebook:move-cell-down",
    "keys": [
        "Ctrl Shift ArrowDown"
    ],
    "selector": ".jp-Notebook:focus"
},

Once you’ve finished adding shortcuts, hit ‘Save’ (in the top right) and then you’re good to go!

Pretty nifty, eh?


If you’re liking this story, it would mean a lot to me if you click my ‘Follow’ button – only 1% of my readers do! Thanks for reading.


Tip 3: Markdown formatting

When you’re writing text in a so-called "comment" cell, you’re actually writing in markdown and so, surprise surprise, you can write using markdown formatting.

That might seem obvious or insignificant, but in my experience very few Jupyter users seem to make full use of the capabilities of markdown in their Notebooks.

Using markdown, you’re able to write text, headers, lists, tables, and even code snippets. It’s a great way to bring a bit more dynamism and structure to your Notebooks.

For example, here are some of the formats that I use most often. I particularly love the ‘table’ formatting (as it’s a great way to show previews of what some preprocessing code will achieve), and I recommend using this Markdown Tables Generator to quickly generate tables in the right format.

Tip 4: HTML formatting

This tip is kind of an extension to the one above, but it’s even less commonly used.

When you’re writing text in a markdown cell, you can use HTML tags to format the text and achieve really custom formatting like highlighting text and hiding text in toggles.

Personally, I find the highlighting function really helpful when I’m reviewing someone else’s notebook and want to make comments or highlight something to return to later. It’s a great way to emphasise the notes I’ve left without cluttering up the Notebook.

Tip 5: "Enable scrolling for outputs"

By default, the ‘output’ cells in Jupyter Notebooks will flex to the height of the output – i.e. when you run some code, Jupyter will print everything your code instructs it to print. And I mean everything.

Usually, that’s really useful, but on occasions when you’re running some code that prints a lot of outputs, your notebook cells can quickly grow to a ridiculous length, making it hard to navigate around the notebook.

For example, the image below shows a simple code snippet which will print every integer from 1 to 1001. As you can see, the output is loooooong.

To condense the outputs without losing the valuable printed information, right-click on the output cell and select ‘Enable Scrolling for Outputs.’ This retains the outputs in a scrollable output cell, but avoids cluttering up your notebook.

In real life, I find this very useful when I’m doing things like (a) printing out the names of all columns in a table, or (b) using a library like optuna to tune a model’s hyperparameters. In both of these scenarios, it’s valuable to see all the outputs (all the column names, or the results of every single round of training), and I rarely want to entirely suppress the outputs because they’re really useful for debugging. By selecting ‘Enable Scrolling for Outputs,’ I’m able to retain the outputs but minimise them so that they don’t dominate the entire notebook.

One more thing –

I’ve started a free newsletter called AI in Five where I share 5 bullet points each week on the latest AI news, coding tips and career stories for Data Scientists/Analysts. There’s no hype, no "data is the new oil" rubbish and no tweets from Elon – just practical tips and insights to help you develop in your career.

Subscribe here if that sounds up your street!

AI in Five | Matt Chapman | Substack


Related Articles