Back to all posts

Databricks widgets

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.  If y…

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. 

If you are running Databricks Runtime 11.3 LTS or above, you can also use ipywidgets in Databricks notebooks.

Python
dbutils.widgets.help()

There are 4 types of widgets:

  • 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.

Widgets only accept string values.

SQL
dbutils.widgets.text(defaultValue="50",label="Type EmployeeCode",name="EmployeeCode")
#dbutils.widgets.get(name="EmployeeCode") # '50'
#dbutils.widgets.remove(name="EmployeeCode")

Statelist = ["RJ","UP","MP","UK"]
dbutils.widgets.dropdown(name="State", label="Select State", choices=Statelist, defaultValue="RJ")
#dbutils.widgets.get(name="State")
#dbutils.widgets.remove(name="State")

Statelist = ["RJ","UP","MP","UK"]
dbutils.widgets.combobox(name="State_", label="Select or Type", choices=Statelist, defaultValue="RJ")
#dbutils.widgets.get(name="State")
#dbutils.widgets.remove(name="State")


Statelist = ["RJ","UP","MP","UK"]
dbutils.widgets.multiselect(name="State__", label="Choose multi State", choices=Statelist, defaultValue="RJ")
#dbutils.widgets.get(name="State")
#dbutils.widgets.remove(name="State")

# getArgument() get direct value from widget (not need to dbutils.widgets).
getArgument("EmployeeCode")

#dbutils.widgets.removeAll()

PHP
database_choices = [database[0] for database in spark.catalog.listDatabases()]
default_value = "Default"

if default_value not in database_choices:
    database_choices.append(default_value)

dbutils.widgets.dropdown(name="ChooseDatabase", label="Choose Database", defaultValue=default_value, choices=database_choices)

Bash
 dbutils.widgets.text("database", "")
 dbutils.widgets.text("table", "")
 dbutils.widgets.text("filter_value", "100")


SELECT *
FROM ${database}.${table}
WHERE col == ${filter_value}
LIMIT 100

Keep building your data skillset

Explore more SQL, Python, analytics, and engineering tutorials.