Notebook Widgets: Adding Interactive Parameters

Notebook widgets add interactive input controls — text boxes, dropdowns, comboboxes, and multiselects — to the top of a Databricks notebook, letting users parameterise execution without editing code. Widgets make notebooks reusable across different datasets, date ranges, and configurations, and their values can be passed as parameters when scheduling notebooks as jobs.

  • Create and use the four widget types: text, dropdown, combobox, and multiselect
  • Access widget values in Python, SQL, Scala, and R
  • Pass widget values as job parameters for scheduled execution

Who this is for: Data engineers and analysts who build reusable, parameterised notebooks for both interactive and scheduled use.

Part of the Databricks Notebooks section of the Databricks tutorial series.

Architecture / Concept Overview: Notebook Widgets: Adding Interactive Parameters

Widgets are registered with the notebook session via dbutils.widgets and rendered as interactive controls in the notebook header. When a cell references a widget value, it reads the current selection at execution time. This lets users re-run the same notebook with different inputs without modifying code. In scheduled jobs, widget values are populated from base_parameters in the job configuration.

%%{init: {"theme":"base","themeVariables":{"background":"#0B0E14","primaryTextColor":"#E0E6ED","lineColor":"#5D6470","darkMode":true,"primaryColor":"#2E4A4A","secondaryColor":"#374151","secondaryTextColor":"#E0E6ED","tertiaryColor":"#111827","tertiaryTextColor":"#E0E6ED","edgeLabelBackground":"#1f2937"}}}%% flowchart LR classDef source fill:#3F4B59,stroke:#9CA3AF,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef ingestion fill:#5A4B36,stroke:#C9A86B,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef processing fill:#535072,stroke:#8E82B4,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef storage fill:#2E4A4A,stroke:#5FAFA8,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef serving fill:#3D5550,stroke:#6BB7AA,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef governance fill:#5A3F52,stroke:#C28BB0,stroke-width:2px,rx:8,ry:8,color:#E0E6ED Create[Create Widget]:::source --> Header[Notebook Header UI]:::serving Header --> UserInput[User Selects Value]:::source UserInput --> Code[Code Reads Value]:::processing Code --> Results[Parameterised Results]:::serving

*Widgets render in the notebook header, accept user input, and provide values to code at execution time.*

%%{init: {"theme":"base","themeVariables":{"background":"#0B0E14","primaryTextColor":"#E0E6ED","lineColor":"#5D6470","darkMode":true,"primaryColor":"#2E4A4A","secondaryColor":"#374151","secondaryTextColor":"#E0E6ED","tertiaryColor":"#111827","tertiaryTextColor":"#E0E6ED","edgeLabelBackground":"#1f2937"}}}%% graph TD classDef source fill:#3F4B59,stroke:#9CA3AF,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef ingestion fill:#5A4B36,stroke:#C9A86B,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef processing fill:#535072,stroke:#8E82B4,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef storage fill:#2E4A4A,stroke:#5FAFA8,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef serving fill:#3D5550,stroke:#6BB7AA,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef governance fill:#5A3F52,stroke:#C28BB0,stroke-width:2px,rx:8,ry:8,color:#E0E6ED Text[Text Widget]:::source Dropdown[Dropdown Widget]:::processing Combo[Combobox Widget]:::serving Multi[Multiselect Widget]:::ingestion Text --> Input[Free-form text input]:::source Dropdown --> Select[Single selection from list]:::processing Combo --> TypeSelect[Type or select from list]:::serving Multi --> MultiSelect[Multiple selections]:::ingestion

*Four widget types serve different input patterns: free text, single select, type-or-select, and multi-select.*

%%{init: {"theme":"base","themeVariables":{"background":"#0B0E14","primaryTextColor":"#E0E6ED","lineColor":"#5D6470","darkMode":true,"primaryColor":"#2E4A4A","secondaryColor":"#374151","secondaryTextColor":"#E0E6ED","tertiaryColor":"#111827","tertiaryTextColor":"#E0E6ED","edgeLabelBackground":"#1f2937"}}}%% flowchart LR classDef source fill:#3F4B59,stroke:#9CA3AF,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef ingestion fill:#5A4B36,stroke:#C9A86B,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef processing fill:#535072,stroke:#8E82B4,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef storage fill:#2E4A4A,stroke:#5FAFA8,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef serving fill:#3D5550,stroke:#6BB7AA,stroke-width:2px,rx:8,ry:8,color:#E0E6ED classDef governance fill:#5A3F52,stroke:#C28BB0,stroke-width:2px,rx:8,ry:8,color:#E0E6ED Interactive[Interactive Use]:::source --> Widget[Widget Value]:::processing Scheduled[Job Parameters]:::governance --> Widget Widget --> Notebook[Notebook Logic]:::serving

*Widgets accept values from interactive UI input or from job parameters in scheduled execution.*

Key Terms

Widget
An interactive input control rendered in the notebook header for parameterising execution.
Text Widget
A free-form text input for arbitrary string values.
Dropdown Widget
A single-select list of predefined choices.
Combobox Widget
A text input with autocomplete suggestions from a predefined list.
Multiselect Widget
A multi-select list allowing multiple choices.
dbutils.widgets
The API for creating, reading, and removing notebook widgets.

Prerequisites and Setup

  • A Databricks notebook attached to compute
  • Understanding of the parameters your notebook needs
  • For scheduled use: a job definition with base_parameters matching widget names
  • Python, SQL, Scala, or R familiarity

Step-by-Step Implementation

    Configuration Reference

    Notebook Widgets: Adding Interactive Parameters configuration options
    Widget TypeMethodUse CaseInput Style
    Textdbutils.widgets.text()Dates, IDs, free-form inputText field
    Dropdowndbutils.widgets.dropdown()Fixed list of optionsSingle select
    Comboboxdbutils.widgets.combobox()Suggested options with custom inputType or select
    Multiselectdbutils.widgets.multiselect()Multiple selectionsCheckboxes
    Get valuedbutils.widgets.get("name")Read current valueN/A
    Removedbutils.widgets.remove("name")Clean upN/A
    Remove alldbutils.widgets.removeAll()Reset all widgetsN/A

    Monitoring, Cost, and Security Considerations

    Monitoring

    Widget values are logged in job run parameters, providing an audit trail of what inputs were used for each execution. Review job run history to see which parameter combinations were used.

    Cost Optimisation

    - Widgets enable one notebook to serve multiple use cases, reducing notebook sprawl and maintenance overhead.

    - Use widgets with dropdown constraints to prevent users from selecting overly broad date ranges or resource-intensive configurations.

    - Test with small data ranges before running full production parameters.

    Security and Governance

    - Widget values are strings; always validate and sanitise inputs before using them in SQL queries to prevent injection.

    - Use parameterised queries or Spark's built-in parameter binding instead of string interpolation where possible.

    - Widget values in job parameters are visible to anyone with job read access.

    Common Pitfalls and Recommended Patterns

    • Using string interpolation for SQL with widget values: this is vulnerable to SQL injection; use getArgument() in SQL or parameterised queries.
    • Creating widgets in the middle of the notebook: place all widget definitions at the top for clarity.
    • Forgetting default values: always set meaningful defaults so the notebook works without manual input.
    • Not validating widget inputs: add validation cells that check widget values before processing.
    • Over-parameterising: too many widgets make notebooks confusing; keep to 3-5 essential parameters.
    • Not documenting widget purposes: add a markdown cell explaining what each parameter controls.

    Frequently Asked Questions

    Do widget values persist between sessions?

    Widget definitions persist in the notebook, and default values are restored when a new session starts. User-selected values persist only during the active session.

    Can I dynamically populate dropdown options?

    Yes. Query a table to get the list of options, then pass the result to dbutils.widgets.dropdown(). The options update when the cell is re-run.

    How do widgets work with scheduled jobs?

    Job base_parameters map directly to widget names. When the job runs, dbutils.widgets.get("param") returns the job parameter value instead of the UI default.

    Can I use widgets in SQL-only notebooks?

    Yes. Use getArgument('widget_name') or :widget_name syntax to reference widget values in SQL cells.