cancel
Showing results for 
Search instead for 
Did you mean: 
Community Articles
Dive into a collaborative space where members like YOU can exchange knowledge, tips, and best practices. Join the conversation today and unlock a wealth of collective wisdom to enhance your experience and drive success.
cancel
Showing results for 
Search instead for 
Did you mean: 

Level Up Your Databricks Game - Episode 1: Widgets

TheOC
Contributor

Hi all,
TheOC here with my first of (hopefully!) many blogs on the Databricks Community. I'm hoping in this series to share quick, practical tips to help you get the most out of Databricks. Today's topic is: Widgets.

If you're anything like me, you've also fallen into the trap of building notebooks with hard-coded values (dates, environments, column names?). This works fine at first, but as soon as something changes, or we want to share our notebooks, it can become a pain. This is where we should look to use widgets!

TheOC_0-1756659655433.gif
Widgets are interactive inputs that can be added to your notebook. This completely negates the need to dig through your code to look for the particular hard-coded value, as now you can simply use the interface you have made to change this. It may feel a small thing, but it can easily make your life so much easier when looking to productionise notebooks.

Here are our options:

  1. Text Box (Free-text input, useful for things like dates or titles)
    dbutils.widgets.text()
  2. Dropdown (Single-selection list input, useful for environments)
    dbutils.widgets.dropdown()
  3. Combobox (Dropdown, plus the option to type in your own value)
    dbutils.widgets.combobox()
  4. Multiselect (Dropdown, but allows for multiple options to be selected)
    dbutils.widgets.multiselect()

TheOC_1-1756659909004.gif
Let's look at creating some Widgets, to show you what that looks like!

I'm going to create 4 widgets with the following code:

# 1. Text box 
dbutils.widgets.text("date", "2025-01-01", "Select Date")

# 2. Dropdown 
dbutils.widgets.dropdown("environment", "dev", ["dev", "test", "prod"], "Environment")

# 3. Combobox
dbutils.widgets.combobox("region", "NA", ["NA", "EMEA", "APAC", "LATAM"], "Region")

# 4. Multiselect
dbutils.widgets.multiselect("columns", "sales", ["sales", "profit", "cost"], "Columns")

Let's see what that does:
ezgif-589e531c99345c.gif
As you can see, inserting this code and hitting run presents the user with 4 inputs: 'Select Date', 'Environment', 'Region', and 'Columns'. 
Getting these into the notebook is great, but now we need to actually use these values entered. This can be done through:
dbutils.widgets.get
(It's worth knowing that for multiselects, the value is returned as a comma separated list.)

Let's have a look at an example:
ezgif-5d5cf85fdf3734.gif
How cool is that?  In just a few lines we've been able to create a user interface for our notebook, and utilise the values entered. I can't wait to use these on my production workflows to make them easier to reuse and share, and much more future-proofed.
These can easily be used to:

  • Switch between Dev, Test, and Prod data sources
  • Select Reporting Periods (P01, P02)
  • Adjust SQL query filters or limits easily

TheOC_5-1756662846682.gif

That's it for the first instalment of Level Up Your Databricks Game. Widgets may seem a simple feature, but once you start using them you'll really unlock value in your projects.
I'd love to hear how you're already using widgets - or where you think you could use them. 
Also, do let me know if there's anything specific you'd like me to cover in this series! I have my own ideas but I'd love to hear yours 😀

Cheers,
TheOC
17 REPLIES 17

giphy

Cheers,
TheOC

Rishabh_Tiwari
Databricks Employee
Databricks Employee

This was such a fun read, @TheOC! Can’t wait to see what you share next 👏🔥

TheOC
Contributor

hey @Rishabh_Tiwari,
Thank you!
Watch this space for the next instalment 👀

Cheers,
TheOC

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now