In this post, we are going to learn about widgets in Databricks Notebook.
Overview
Let’s take an example that you have created a notebook that required some dynamic parameter. In order to pass these parameters value in the notebook, widgets come into the picture. It basically provides an option to pass the parameter value of any type.
Widgets Type
There are 4 types of widgets:
- Text: A text box to get the input
- Dropdown: A set of options, and choose a value
- Combobox: It is a combination of text and dropbox. It can accept value in text or select from dropdown.
- Multiselect: Choose one or more values.
Widgets API
Create Text Widget
Let’s create a widget named as TextWidget and keep the empty value.
dbutils.widgets.text("TextWidget", "")
Create Drop Down Widget
Let’s create a drop-down widget named as DropDownWidget that will have value from 1 to 10 index.
dbutils.widgets.dropdown("0", "1", [str(x) for x in range(1, 10)], "DropDownWidget")
Get widget value in variable
Read a widget value in a variable.
val textWidget = dbutils.widgets.get("TextWidget") println(textWidget)
Remove Widget
Remove the created widget in the notebook.
dbutils.widgets.remove("TextWidget")
For removing all the widgets of the notebook, use removeAll instead of remove. Similarly, you can create in Scala & R using the same command.
Wrapping Up
In this post, we have learned how to define different widgets in Notebook for passing the parameters. It is important as we used to reuse the same code with multiple dynamic parameters.