Sub topic 6
Reusing and chaining notebooks
This chapter assumes you can write basic Python or SQL, know how to attach a notebook to compute, and have used widgets to give a notebook parameters. It covers the two ways to call one Databricks notebook from another, and when to pick each.
Two ways to call another notebook
Databricks offers two mechanisms. They look similar but behave differently.
%runruns another notebook inline, in the same session as the caller.dbutils.notebook.runstarts the other notebook as a separate, ephemeral run.
Inline reuse with %run
The %run magic must be alone in its cell. It executes the target notebook as if its cells were pasted into the current one. Variables, functions, and imports defined there become available to the caller.
%run ./shared_setupThis suits shared helper functions, constants, and configuration. Paths can be relative (./shared_setup) or absolute workspace paths.
Chaining with dbutils.notebook.run
dbutils.notebook.run(path, timeout_seconds, arguments) runs the target as a separate run. It has its own session, so it shares no variables with the caller. Arguments arrive as widget values in the child. The call returns a string, which is whatever the child passes to dbutils.notebook.exit.
Caller:
result = dbutils.notebook.run("./child_notebook", 60, {"run_date": "2026-01-02"})
print(result)Child:
# In child_notebook
run_date = dbutils.widgets.get("run_date")
dbutils.notebook.exit(f"done {run_date}")The child must declare a widget named run_date. Arguments are matched to widgets by name, and the default is used for any name you don't pass.
Choosing between them
| Need | %run | dbutils.notebook.run |
|---|---|---|
| Share functions and variables | Yes | No |
| Pass parameters | Set variables before the call | Widget arguments |
| Get a result back | Read shared variables | One string from `exit` |
| Timeout control | No | Yes, in seconds |
| Isolation from caller | None | Separate run |
Common errors
- Putting other code in a
%runcell. The magic must stand alone, or the cell fails. - Expecting variables after
dbutils.notebook.run. The child's session ends when it finishes. Return what you need throughexit. - Returning large data.
exitreturns a string with a size limit. Write the data to a table or file and return its path or name. - Timeouts. If the child runs longer than the timeout, the call fails. A value of 0 means no timeout.
- Missing widgets. If the child lacks a widget for an argument, the value is not available to it by
dbutils.widgets.get.
Notebooks chained this way run on the caller's compute. To run each step on its own compute with retries and dependencies, use a job with multiple tasks. Later chapters cover that.
Exercise
You need a notebook to load a date-specific file and tell its caller how many rows it loaded. Should the caller use %run or dbutils.notebook.run, and how does the row count come back?
Show answer
Use dbutils.notebook.run. Pass the date as an argument, which the child reads from a widget. The child calls dbutils.notebook.exit with the row count as a string, and the caller receives it as the return value and converts it to an integer if needed.
Exercise
A cell contains %run ./shared_setup followed by print(helper_value). The cell fails. Why, and how do you fix it?
Show answer
The %run magic must be alone in its cell. Move the print call to a separate cell.