Calling a Notebook from Another Notebook in Databricks

Databricks provides two primary methods to call a notebook from another notebook: using the %run command and the dbutils.notebook.run() function.

1. Using the %run Command

The %run command allows you to execute a notebook within the same execution context as the parent notebook. This means any variables or functions defined in the parent notebook are accessible in the child notebook. The command is used as follows:

%run [notebook path] $parameter1="Value1" $parameterN="valueN"

This method is ideal for notebooks that contain shared functions or constants.

2. Using the dbutils.notebook.run() Function

The dbutils.notebook.run() function executes a notebook in a new execution context. It allows you to pass parameters to the child notebook and set a timeout for its execution. The syntax is as follows:

dbutils.notebook.run(notebook_path, timeout_in_seconds, parameters)

For example:

dbutils.notebook.run("notebook_name", 60, {"parameter1": "value1", "parameter2": "value2"})

This method is useful for running notebooks independently while still passing necessary parameters.

Frequently Asked Questions

Bottom Line

Calling notebooks from within other notebooks in Databricks is a powerful way to modularize code, enhance collaboration, and maintain clarity in complex data projects. By using either the %run command or the dbutils.notebook.run() function, you can efficiently manage and execute your data workflows.


👉 Hop on a short call to discover how Fog Solutions helps navigate your sea of data and lights a clear path to grow your business.