Basic editing in Databricks notebooks

A Databricks notebook is a web-based code editor that allows you to write code and view results for interactive data analysis. This page covers the basics of using notebooks in Databricks, including how to navigate the toolbar and perform various cell actions.

Create a notebook

To create a new notebook, click the New button in the workspace sidebar or Create a notebook in any folder.

Open a notebook

To open a notebook, use the workspace Search function or use the workspace browser to navigate to the notebook and click on the notebook’s name or icon.

Notebook cells

Notebooks contain a collection of two types of cells: code cells and Markdown cells. Code cells contain runnable code. Markdown cells contain Markdown code that renders as text and graphics. Use Markdown to document or illustrate your code. You can add or remove cells of either type to your notebook to structure your work.

You can run a single cell, a group of cells, or the whole notebook. A notebook cell can contain at most 10MB, and its output is limited to 20 MB.

Notebook toolbar icons and buttons

The toolbar includes menus and icons that you can use to manage and edit the notebook.

Notebook toolbar

Next to the notebook name are buttons to change the default language of the notebook and, if the notebook is included in a Databricks Git folder, open the Git dialog.

To view previous versions of the notebook, click the “Last edit…” message to the right of the menus.

The icons and buttons at the right of the toolbar are described in the following table:

Icon

Description

Run all button Interrupt execution button

Run all cells or stop execution. The name of this button changes depending on the state of the notebook.

Notebook header compute selector

Open compute selector. When the notebook is connected to a cluster or SQL warehouse, this button shows the name of the compute resource.

Notebook header job scheduler

Open job scheduler.

Notebook header DLT selector

Open Delta Live Tables. This button appears only if the notebook is part of a Delta Live Tables pipeline.

Notebook header share button

Open permissions dialog.

Create cells

Notebooks have two types of cells: code and Markdown. The contents of Markdown cells are rendered into HTML. For example, this snippet contains markup for a level-one heading:

%md ### Libraries
Import the necessary libraries.

Renders as shown:

rendered Markdown example

To create a new cell, hover over a cell at the top or bottom. Click Code or Text to create a code or Markdown cell, respectively.

buttons to create a new cell

Cell actions

The cell actions menu lets you run, cut, and copy cells, move cells around in the notebook, and hide code or results.

Run a cell

To run code cells, click the down arrow at the upper-left of the code cell.

Cell run menu

After a cell has been run, a notice appears to the right of the cell run menu, showing the last time the cell was run and its duration. Hover your cursor over the notice for more details.

To show detailed run information by default, click your profile image at the top right. Then click Settings > Developer > Show detailed command run information.

last run image

If you have a tabular result output, you can also access this information by hovering over the “Last refreshed” section of the UI.

Run selected cells

You can run a single cell or a collection of cells. To select a single cell, click anywhere inside the cell. To select multiple cells, hold down the Command key on MacOS or the Ctrl key on Windows, and click on the cell outside of the text area as shown in the screenshot.

how to select multiple cells

To run the selected cells, click Run > Run and debug > Run selected cell(s) or use the keyboard shortcut Command + Enter on MacOS or Crtl + Enter on Windows. The behavior of this command depends on the compute resource that the notebook is attached to.

  • On a compute resource running Databricks Runtime 13.3 LTS or below, selected cells are executed individually. If an error occurs in a cell, the execution continues with subsequent cells.

  • On a compute resource running Databricks Runtime 14.0 or above, or on a SQL warehouse, selected cells are executed as a batch. Any error halts execution, and you cannot cancel the execution of individual cells. You can use the Interrupt button to stop execution of all cells.

Run selected text

You can highlight code or SQL statements in a notebook cell and run only the highlighted code. This is useful when you want to quickly iterate on code and queries.

  1. Highlight the lines you want to run.

  2. Select Run > Run selected text or use the keyboard shortcut Ctrl+Shift+Enter. If no text is highlighted, Run Selected Text executes the current line.

    run selected lines

If you are using mixed languages in a cell, you must include the %<language> line in the selection.

Run selected text also executes collapsed code, if there is any in the highlighted selection.

Special cell commands such as %run, %pip, and %sh are supported.

You cannot use Run selected text on cells that have multiple output tabs (that is, cells where you have defined a data profile or visualization).

Cell icons

The following screenshot describes the icons that appear at the upper-right of a notebook cell:

upper-right cell icons

Language selector: Select the language for the cell.

Databricks Assistant: Enable or disable Databricks Assistant for code suggestions in the cell.

Cell focus: Enlarge the cell to make editing easier.

Display cell actions menu: Open the cell actions menu. The options in this menu are slightly different for code and Markdown cells.

Cell actions menu

Delete a cell

To delete a cell, click the trash icon to the right of the cell. This icon only appears when you hover your cursor over the cell.

cell trash icon

Move a cell

To move a cell up or down, click and hold the drag handle icon move cell icon to the left of the cell.

drag cell up or down

You can also select Move up or Move down from the cell actions menu.

Cut, copy, and paste cells

There are several options for cutting and copying cells. However, if you are using the Safari browser, only the keyboard shortcuts are available.

  • From the cell actions menu select Cut cell or Copy cell.

  • Use keyboard shortcuts: Command-X or Ctrl-X to cut and Command-C or Ctrl-C to copy.

  • Use the Edit menu at the top of the notebook. Select Cut or Copy.

After you cut or copy cells, you can paste those cells elsewhere in the notebook, into a different notebook, or a notebook in a different browser tab or window. To paste cells, use the keyboard shortcut Command-V or Ctrl-V. The cells are pasted below the current cell.

To undo cut or paste actions, you can use the keyboard shortcut Command-Z or Ctrl-Z or the menu options Edit > Undo cut cells or Edit > Undo paste cells.

To select adjacent cells, click in a Markdown cell and then use Shift + Up or Down to select the cells above or below it. Use the edit menu to copy, cut, paste, or delete the selected cells as a group. To select all cells, select Edit > Select all cells or use the command mode shortcut Cmd+A.

Add code comments

To add a comment to code in a cell, highlight the code. To the right of the cell, a comment icon appears. Click the icon to open the comment box.

commment icon

Add a cell to a dashboard

To add a Markdown cell or a cell that has tabular results to a dashboard, select Add to dashboard from the cell actions menu. For more information, see Dashboards in notebooks.

Open Databricks Assistant

To open a Databricks Assistant text box in a cell, click the Databricks Assistant icon Databricks Assistant icon in the upper-right corner of the cell.

You can use it in a code cell to get help or suggestions for your code. Databricks Assistant must be enabled in your workspace.