Sub topic 5
Parameterizing a notebook with widgets
This chapter assumes you can run Python cells in a Databricks notebook attached to compute, and that you know what a job is. Widgets let one notebook run with different inputs without editing its code.
What a widget is
A widget is an input control shown at the top of a notebook. Each widget has a name, a default value, and an optional label. Code reads the current value with dbutils.widgets.get. Values are always strings.
The same widget works in three situations. You can change it by hand while developing. A job can supply a value when it runs the notebook. Another notebook can pass a value through dbutils.notebook.run.
Creating and reading a text widget
dbutils.widgets.text("run_date", "2026-01-01", "Run date")
run_date = dbutils.widgets.get("run_date")
print(f"Processing {run_date}")The first argument is the name. The second is the default, used when nobody supplies a value. The third is the label shown in the UI. Run the cell and a "Run date" box appears at the top of the notebook. Type a new date and rerun the cell that calls get. The new value is printed.
Other widget types
| Type | Call | Use it for |
|---|---|---|
| Text | `dbutils.widgets.text(name, default, label)` | Free-form values such as dates or paths |
| Dropdown | `dbutils.widgets.dropdown(name, default, choices, label)` | One value from a fixed list |
| Combobox | `dbutils.widgets.combobox(name, default, choices, label)` | A list of suggestions, but typing is allowed |
| Multiselect | `dbutils.widgets.multiselect(name, default, choices, label)` | Several values from a list |
A dropdown limits input to valid choices, so it is a good fit for values like an environment name.
dbutils.widgets.dropdown("env", "dev", ["dev", "test", "prod"], "Environment")
env = dbutils.widgets.get("env")
print(env)How values reach the notebook
A supplied value replaces the default. If a job or parent passes a parameter whose name matches a widget, that value is used. If it passes nothing, the default applies. This is why you should always choose a default that is safe to run.
Common errors
- Reading a widget before creating it.
dbutils.widgets.get("run_date")fails if no widget with that name exists in the session. Define widgets near the top of the notebook. - Treating values as numbers or dates.
getreturns a string. Convert it yourself, for exampleint(dbutils.widgets.get("limit")). - Name mismatch. A job parameter named
rundatedoes not fill a widget namedrun_date. Names must match exactly. - A default that hides mistakes. If the default points at production data, a forgotten parameter in a job silently processes the wrong thing. Prefer safe defaults, or fail when a value is missing.
- Stale widgets. Removed code can leave old widgets in the UI. Use
dbutils.widgets.remove("name")for one widget ordbutils.widgets.removeAll()to clear them.
Exercise
Exercise
A job passes the parameter run_date with the value 2026-03-15 to a notebook that contains dbutils.widgets.text("run_date", "2026-01-01", "Run date"). What does dbutils.widgets.get("run_date") return, and what type is it?
Show answer
It returns the string "2026-03-15". The supplied value replaces the default of "2026-01-01". Widget values are always strings, so convert it (for example with datetime.date.fromisoformat) if you need a date.
Exercise
Why is a dropdown widget often better than a text widget for an env parameter?
Show answer
A dropdown restricts the value to a fixed list such as dev, test, and prod, so a typo cannot reach the code through the UI.