Code in JupyterLab#

This guide dives into the core functionalities of JupyterLab, focusing on running code cells, creating, deleting, and moving cells, using auto-close brackets, and managing kernel restarts.

With practical tips, shortcuts, and instructions, you will learn how to work more efficiently within JupyterLab, making the most of its features for your coding and data analysis tasks.


Running code cells#

JupyterLab allows you to run individual code cells in your notebook. Here are the different methods to run a code cell:

Mouse click: Click on the run icon in the toolbar to run the currently selected cell.

Keyboard shortcuts:

  • Shift + Enter: Run the current cell and move to the next cell. If the current cell is the last one, a new cell will be automatically created below.

  • Cmd + Enter (macOS) / Ctrl + Enter (Windows/Linux): Run the current cell without moving to the next cell.

  • Option + Enter (macOS) / Alt + Enter (Windows/Linux): Run the current cell, insert a new cell below, and move to the new cell, regardless of whether the current cell is the last one in the notebook.

Command mode vs. edit mode#

JupyterLab has two modes: command mode and edit mode. The mode you are in affects which actions you can perform with keyboard shortcuts.

  • Edit mode: This is where you can type code or text directly into a cell. You will know you are in edit mode if you see a blinking cursor in the cell. To enter edit mode, press Enter while a cell is selected.

  • Command mode: This is where you can perform notebook-level actions such as creating, deleting, or moving cells. You will know you are in command mode if the entire cell is highlighted in blue, and there is no blinking cursor. To enter command mode, press Esc while in a cell.

Note: The following shortcuts for creating, deleting, and moving cells require you to be in command mode.

Creating and deleting cells#

You can easily create or delete cells in your notebook. Here is how:

Creating cells:

  • Mouse click:

    • Add cell below: Click on the plus icon in the toolbar to create a new cell below the currently selected cell.

    • Add cell above: In the code cell itself, click on the icon with a plus sign above a square to add a new cell above.

    • Add cell below: In the code cell itself, click on the icon with a plus sign below a square to add a new cell below.

  • Keyboard shortcuts:

    • A: Press A to create a new cell above the current cell.

    • B: Press B to create a new cell below the current cell.

Deleting cells:

  • Mouse click: Select the cell you want to delete, then click the scissors icon in the toolbar.

  • Keyboard shortcut:

    • D D: Press D D (press D twice in quick succession) to delete the currently selected cell.

Moving cells#

You can rearrange the order of cells in your JupyterLab notebook by moving them up or down. This is useful for organizing your code, especially when you need to reorder steps in your workflow.

  • Mouse click:

    • Move cell up: In the code cell itself, click on the upward arrow icon to move the selected cell up.

    • Move cell down: In the code cell itself, click on the downward arrow icon to move the selected cell down.

  • Keyboard shortcuts:

    • Ctrl + Shift + Up Arrow: Move the current cell up.

    • Ctrl + Shift + Down Arrow: Move the current cell down.

Note: These keyboard shortcuts for moving cells require you to be in command mode.

Auto-close brackets#

JupyterLab can automatically close brackets for you, making it easier to write code efficiently. When you type an opening bracket (e.g., (, {, [), JupyterLab will automatically insert the corresponding closing bracket. This feature works for:

  • Parentheses: ()

  • Square brackets: []

  • Curly braces: {}

  • Quotes: "", ''

Additionally, if you mark a section of text and then type an opening bracket, JupyterLab will automatically enclose the entire selected text within the brackets. This can be particularly useful for quickly wrapping code or text in parentheses, quotes, or other brackets.

To use this feature, you need to ensure that auto-close brackets are activated. By default, this feature is not enabled. To activate it, follow these steps:

  1. Go to the Settings menu in JupyterLab.

  2. Ensure that the Auto Close Brackets option is turned on.

If you prefer to disable this feature, you can uncheck the option in the same menu.

Managing kernels#

Kernels are the computational engines that execute the code contained in your JupyterLab notebooks. You may need to restart the kernel in various situations, such as when you want to clear the memory or rerun all cells from a clean state.

Restart kernel options#

  • Restart kernel: This option restarts the kernel without running any cells.

    • Mouse click: Click the restart icon in the toolbar and select Restart Kernel.

    • Menu: Go to the Kernel menu and select Restart Kernel.

  • Restart kernel and run all cells: This option restarts the kernel and then runs all cells in the notebook from top to bottom.

    • Mouse click: Click the restart icon in the toolbar and select Restart Kernel and Run All Cells.

    • Menu: Go to the Kernel menu and select Restart Kernel and Run All Cells.

  • Restart kernel and clear all outputs: This option restarts the kernel and clears all the outputs in the notebook without running any cells.

    • Menu: Go to the Kernel menu and select Restart Kernel and Clear All Outputs.

Keyboard shortcut for restarting kernel#

  • 0 0 (zero zero): Pressing 0 0 in quick succession restarts the kernel. This shortcut is particularly useful when you need to quickly restart the kernel without using the mouse.