Databricks Note Widgets
Widgets helps us parameterized notebook code and helps in making more generic.
Input widgets allow you to add parameters to your notebooks and dashboards.
You can add a widget from the Databricks UI or using the widget API.
Databricks widget types
Currently 4 types of widgets are supported
- text: Input a value in a text box.
- dropdown: Select a value from a list of provided values.
- combobox: Combination of text and dropdown. Select a value from a provided list or input one in the text box.
- multiselect: Select one or more values from a list of provided values.
Create widgets in databricks
- Widgets api is simple four parameters function call to dbutils
- The first argument for all widget types is name. This is the name you use to access the widget.
- The second argument is defaultValue, the widget’s default setting.
- The third argument for all widget types (except text) is choices, a list of values the widget can take on. This argument is not used for text type widgets.
- The last argument is label, an optional value for the label shown over the widget text box or dropdown.
Widgets with SQL
CREATE WIDGET DROPDOWN select_catalog DEFAULT "Cleansed" CHOICES SELECT * FROM (VALUES ("Cleansed"), ("Curated"), ("omop_dw"), ("omop_dw_suhas"), ("omop_dw_shinji"), ("omop_dw_rob"))
Widgets with Python
dbutils.widgets.dropdown("Select Catalog", "Cleansed", ["Cleansed", "Curated", "omop_dw_shinji", "omop_dw_suhas", "omop_dw_rob"])
Widgets with Scala
%scala
dbutils.widgets.dropdown("Select Catalog", "Cleansed", ["Cleansed", "Curated", "omop_dw_shinji", "omop_dw_suhas", "omop_dw_rob"])
To use value in code
This widget can be referenced in code using :Select Catalog in SQL or dbutils.widgets.get("Select Catalog") in other languages.