Parameterising Your Notebooks in Databricks

Hello and welcome back!
In this post, I’ll walk you through how to use parameters in Databricks notebooks and share a few tips and tricks. Parameters are a simple but powerful way to make your notebooks dynamic, reusable, and easier to maintain.


What Are Parameters and Why Use Them?

Parameters let you insert variable values at runtime. Instead of hardcoding values into your queries, you can define them once and reuse them throughout your notebook.

For example:

  • If you need to reference the same schema in multiple SQL statements, you can define it once as a parameter and reuse it everywhere.
  • The same applies for catalogs, table names, or even IDs used in queries.

This avoids having to repeat values like catlog.schema_name.bronze_table everywhere, and makes your code:

  • Cleaner
  • Easier to reuse across environments (dev/test/prod)
  • Less error-prone (no need to find & replace values manually)

Types of Parameters in Databricks

Databricks supports several widget types for parameters:

  • text – free text input
  • dropdown – pick one option from a list
  • combobox – pick from a list or type your own value
  • multiselect – select multiple options

These widgets appear at the top of your notebook, and you can customize their defaults, labels, and order.


Demo: Defining and Using Parameters

Define parameters example

# Table name parameters
dbutils.widgets.text("bronze", "bronze_table", "Enter Bronze table name:")
dbutils.widgets.text("silver", "silver_table")

# Catalog and Schema parameters
dbutils.widgets.text("catalog", "main", "Enter Catalog:")
dbutils.widgets.text("schema", "default", "Enter Schema:")

# ID parameter
dbutils.widgets.text("id", "1", "Enter ID:")
# Catalog parameter (combobox - pick or type a value)
dbutils.widgets.combobox("catalog", "main", ["main", "hive_metastore", "dev_catalog"], "Select Catalog:")

# Schema parameter (multiselect - choose one or more schemas)
dbutils.widgets.multiselect("schema", "default", ["default", "sandbox", "analytics"], "Select Schema:")

# Auto parameters
dbutils.widgets.dropdown("database", "default", [database[0] for database in spark.catalog.listDatabases()])

Pass parameters example (or can just use dbutils.widgets.getAll() )

bronze = dbutils.widgets.get("bronze")
silver = dbutils.widgets.get("silver")
catalog = dbutils.widgets.get("catalog")
schema = dbutils.widgets.get("schema")
id_value = dbutils.widgets.get("id")

print(f"catalog: {catalog}, schema: {schema}, bronze: {bronze}, silver: {silver}, id: {id_value}")

Call the parameter:

# Build fully qualified table names
bronze_table = f"{catalog}.{schema}.{bronze}"
silver_table = f"{catalog}.{schema}.{silver}"

# Create Bronze table
spark.sql(f"""
CREATE OR REPLACE TABLE {bronze_table} (
    id INT,
    name STRING
)
""")

# Insert sample data into Bronze + dynamic ID
spark.sql(f"""
INSERT INTO {bronze_table} VALUES
    (1, 'Alice'),
    (2, 'Bob'),
    (3, 'Charlie'),
    ({id_value}, 'DynamicUser')
""")

# Create Silver table from Bronze
spark.sql(f"""
CREATE OR REPLACE TABLE {silver_table} AS
SELECT id, UPPER(name) as name
FROM {bronze_table}
""")

Settings

You can also pin it to the top or customise where the layout of the parameters usings the edit/settings/pin cog in the top right.

Or you can adjust parameters using the UI using the add parameter button and the settings cog.

Functionality and Best Practices

From the demo, here’s what we saw:

  • Parameters can be defined once and reused many times.
  • You can customise widgets (labels, order, defaults) for clarity.
  • You can fetch all parameters at once using dbutils.widgets.getArgument() or dbutils.widgets.getAll().
  • You can remove parameters with dbutils.widgets.remove() or dbutils.widgets.removeAll().

Conclusion

Using parameters in Databricks notebooks is best practice when building reusable, production-ready code. They:

  • Make your code dynamic
  • Help avoid hardcoding values
  • Allow easy reuse across environments
  • Easy usability for team members

In this post we focused on notebooks, but remember parameters can also be used in SQL editor queries, dashboards, and jobs — which I’ll cover in future content.

Leave a Reply

Your email address will not be published. Required fields are marked *