As of now, we have learned to create a notebook with any default language, pass the parameter using widgets, version control in the notebook. In this post, we are going to learn to execute the created notebook.
Overview
There are ways to run the notebooks:
- Within the Notebook (just by clicking the run for each cell or run all for the entire notebook)
- Using %run command
- Using dbutils.notebook.run API
We are not going to discuss the 1st approach as it is simple and manual which we basically for debugging.
%run
Template:
%run /path/to/notebook $widget1Key="someValue" $widget2Key="someValue"
Here, we want to run the notebook which is available at /path/to/notebook and required 2 parameters named as widgte1Key and widget2Key. With the %run command (in bash cell) at the beginning, it will start execution.
Let’s see some other example, where we want to pass the output of one notebook to another notebook. With this command, we will not be able to achieve. Hence, the other approach is dbutils.notebook.run API comes into the picture.
dbutils.notebook.run
It has 2 APIs:
- run
- exit
#1 run
It takes below 3 arguments:
- path: String type: Path of the notebook
- timeout_seconds: Int type: Controls the timeout of the run (0 indicates no timeout)
- arguments: Map type: Widgets value required in the notebook. For example: “widget1Key”: “someValue”, ….
Template
dbutils.notebook.run("notebook", 60, {"widget1Key": "someValue", "widget2Key": "someValue", ...})
dbutils.notebook.run("notebook", 60, Map("widget1Key" -> "someValue", "widget2Key" -> "someValue", ...))
#2 exit
It takes 1 argument:
- returnValue: String Type: Return this from the calling notebook
Template
dbutils.notebook.exit("returnValue")
Wrapping Up
In this post, we have learned to run the notebook by passing the parameter. We can use the exit to return any value from the notebook or any exception based on the failure.