Databricks widgets

Input widgets allow you to add parameters to your notebooks and dashboards. The widget API consists of calls to create various types of input widgets, remove them, and get bound values.

Note

In Databricks Runtime 11.0 and above, you can also use ipywidgets in Databricks notebooks.

Databricks widgets are best for:

  • Building a notebook or dashboard that is re-executed with different parameters

  • Quickly exploring results of a single query with different parameters

Tip

View the documentation for the widget API in Scala, Python, and R with the following command:

dbutils.widgets.help()

Databricks widget types

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.

Widget dropdowns and text boxes appear immediately following the notebook toolbar.

Widget dropdown

Databricks widget API

The widget API is designed to be consistent in Scala, Python, and R. The widget API in SQL is slightly different, but as powerful as the other languages. You manage widgets through the Databricks Utilities interface.

The first argument for all widget types is the widget name. The second argument is the default value. Widgets only accept string values.

dbutils.widgets.dropdown("state", "CA", ["CA", "IL", "MI", "NY", "OR", "VA"])

dbutils.widgets.text("database", "customers_dev")
CREATE WIDGET DROPDOWN state DEFAULT "CA" CHOICES SELECT * FROM (VALUES ("CA"), ("IL"), ("MI"), ("NY"), ("OR"), ("VA"))

CREATE WIDGET TEXT database DEFAULT "customers_dev"

Databricks widget example

To see detailed API documentation for each method, use dbutils.widgets.help("<method-name>"). The help API is identical in all languages. For example:

dbutils.widgets.help("dropdown")

Create a simple dropdown widget.

dbutils.widgets.dropdown("state", "CA", ["CA", "IL", "MI", "NY", "OR", "VA"])
CREATE WIDGET DROPDOWN state DEFAULT "CA" CHOICES SELECT * FROM (VALUES ("CA"), ("IL"), ("MI"), ("NY"), ("OR"), ("VA"))

Interact with the widget from the widget panel.

Interact with widget

You can access the current value of the widget with the call:

dbutils.widgets.get("state")
SELECT "${state}"

Finally, you can remove a widget or all widgets in a notebook:

dbutils.widgets.remove("state")

dbutils.widgets.removeAll()
REMOVE WIDGET state

Important

If you add a command to remove a widget, you cannot add a subsequent command to create a widget in the same cell. You must create the widget in another cell.

Using widget values in Spark SQL

Spark SQL accesses widget values as string literals that can be used in queries.

You can access widgets defined in any language from Spark SQL while executing notebooks interactively. Consider the following workflow:

  1. Create a dropdown widget of all databases in the current catalog:

    dbutils.widgets.dropdown("database", "default", [database[0] for database in spark.catalog.listDatabases()])
    
  2. Create a text widget to manually specify a table name:

    dbutils.widgets.text("table", "")
    
  3. Run a SQL query to see all tables in a database (selected from the dropdown list):

    SHOW TABLES IN ${database}
    
  4. Manually enter a table name into the table widget.

  5. Preview the contents of a table without needing to edit the contents of the query:

    SELECT *
    FROM ${database}.${table}
    LIMIT 100
    

Important

In general, you cannot use widgets to pass arguments between different languages within a notebook. You can create a widget arg1 in a Python cell and use it in a SQL or Scala cell if you run cell by cell. However, it will not work if you execute all the commands using Run All or run the notebook as a job. To work around this limitation, we recommend that you create a notebook for each language and pass the arguments when you run the notebook.

Configure widget settings

You can configure the behavior of widgets when a new value is selected, whether the widget panel is always pinned to the top of the notebook, and change the layout of widgets in the notebook.

  1. Click the gear icon icon at the right end of the Widget panel.

  2. In the pop-up Widget Panel Settings dialog box, choose the widget’s execution behavior.

    Widget settings
    • Run Notebook: Every time a new value is selected, the entire notebook is rerun.

    • Run Accessed Commands: Every time a new value is selected, only cells that retrieve the values for that particular widget are rerun. This is the default setting when you create a widget.

      Note

      SQL cells are not rerun in this configuration.

    • Do Nothing: Every time a new value is selected, nothing is rerun.

  3. To pin the widgets to the top of the notebook or to place the widgets above the first cell, click pin icon. The setting is saved on a per-user basis.

  4. If you have Can Manage permission for notebooks, you can configure the widget layout by clicking edit icon. Each widget’s order and size can be customized. To save or dismiss your changes, click accept and cancel icons.

    Note

    The widget layout is saved with the notebook.

    If the widget layout is configured, new widgets will be added out of alphabetical order.

  5. To reset the widget layout to a default order and size, click gear icon to open the Widget Panel Settings dialog and then click Reset Layout.

    Note

    The widget layout cannot be reset by the removeAll() command.

Example notebook

You can see a demo of how the Run Accessed Commands setting works in the following notebook. The year widget is created with setting 2014 and is used in DataFrame API and SQL commands.

Widgets

When you change the setting of the year widget to 2007, the DataFrame command reruns, but the SQL command is not rerun.

Widget demo notebook

Open notebook in new tab

Databricks widgets in dashboards

When you create a dashboard from a notebook that has input widgets, all the widgets display at the top of the dashboard. In presentation mode, every time you update value of a widget you can click the Update button to re-run the notebook and update your dashboard with new values.

Dashboard with widgets

Use Databricks widgets with %run

If you run a notebook that contains widgets, the specified notebook is run with the widget’s default values. You can also pass in values to widgets. For example:

%run /path/to/notebook $X="10" $Y="1"

This example runs the specified notebook and passes 10 into widget X and 1 into widget Y.