0% found this document useful (0 votes)
66 views13 pages

Databricks Widgets

Uploaded by

ivandjt226
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views13 pages

Databricks Widgets

Uploaded by

ivandjt226
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CHENCHU’S

🚀 Mastering PySpark
and Databricks 🚀

Part-21
Databricks Widgets
C .R. Anil Kumar Reddy
Associate Developer for Apache Spark 3.0

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

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

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.

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 as shown below. Widgets only accept string values.

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

Widgets utility (dbutils.widgets)


The widgets utility allows you to parameterize notebooks.

The following table lists the available commands for this utility, which you
can retrieve using dbutils.widgets.help().

Let us discuss all the above widgets and commands one by one

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

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.

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

Combobox
Combobox command (dbutils.widgets.combobox)

combobox(name: String, defaultValue: String, choices: Seq, label: String):


void

Creates and displays a combobox widget with the specified programmatic


name, default value, choices, and optional label.

Example

This example creates and displays a combobox widget with the


programmatic name env. It offers the choices dev, test, prod, and is set to
the initial value of test. This combobox widget has an accompanying label
Environment Combobox. This example ends by printing the initial value of
the combobox widget, test.

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

Dropdown
dropdown command (dbutils.widgets.dropdown)

dropdown(name: String, defaultValue: String, choices: Seq, label: String):


void

Creates and displays a dropdown widget with the specified programmatic


name, default value, choices, and optional label.

Example

This example creates and displays a dropdown widget with the


programmatic name env1. It offers the choices dev, test, prod, and is set to
the initial value of dev. This combobox widget has an accompanying label
Environment Dropdown. This example ends by printing the initial value of
the dropdown widget, dev.

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

Multiselect

multiselect command (dbutils.widgets.multiselect)

multiselect(name: String, defaultValue: String, choices: Seq, label: String):


void

Creates and displays a multiselect widget with the specified programmatic


name, default value, choices, and optional label.

Example

This example creates and displays a multiselect widget with the


programmatic name env2. It offers the choices dev, test, prod, and is set to
the initial value of prod. This multiselect widget has an accompanying label
Environment Multiselect. This example ends by printing the initial value of
the multiselect widget, prod.

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

Text

text command (dbutils.widgets.text)

text(name: String, defaultValue: String, label: String): void

Creates and displays a text widget with the specified programmatic


name, default value, and optional label.

Example

This example creates and displays a text widget with the programmatic
name env2. It offers the choices dev, test, prod, and is set to the initial
value of dev. This text widget has an accompanying label Environment Text.
This example ends by printing the initial value of the text widget, dev.

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

get command
get command (dbutils.widgets.get)

get(name: String): String

Gets the current value of the widget with the specified programmatic name.

Example

This example gets the value of the widget that has the programmatic name text .

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

getAll command
getAll command (dbutils.widgets.getAll)

getAll: map
Gets a mapping of all current widget names and values. This can be
especially useful to quickly pass widget values to a spark.sql() query.

This command is available in Databricks Runtime 13.3 LTS and above. It is


only available for Python and Scala.

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

remove command (dbutils.widgets.remove)

Removes the widget with the specified programmatic name.

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

removeAll command (dbutils.widgets.removeAll)

Removes all widgets from the notebook.

Anil Reddy Chenchu


www.linkedin.com/in/chenchuanil
CHENCHU’S

NIL REDDY CHENCHU


Torture the data, and it will confess to anything

DATA ANALYTICS

Happy Learning

SHARE IF YOU LIKE THE POST

Lets Connect to discuss more on Data

www.linkedin.com/in/chenchuanil

You might also like