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

BS_THE_ANALYST
Honored Contributor III

@TheOC this is cool! This really unlocks others to be able to interact with your notebook without actually breaking them 😂.

I think the idea around parameterisation is great. You could move from notebooks to SQL Queries and how they support parameters. We've also got parameters in AI/BI dashboards. We can then move into Data Engineering as there's so much around parameterisation with Lakeflow Declarative Pipelines as well: https://docs.databricks.com/aws/en/dlt/parameters 

BS_THE_ANALYST_0-1756715345060.png
There could be a series all around this @TheOC. Happy to help in whatever way I can, should you require another set of hands 👍.

All the best,
BS

 

hey @BS_THE_ANALYST 
Thanks for the support!
Great idea about expanding the particular use-case of parameterisation. I have a few more ideas I'd like to explore with the Level up your Databricks Game series first, but absolutely will be adding that to my list to circle back to. 

Cheers,
TheOC

szymon_dybczak
Esteemed Contributor III

Thank @TheOC , really cool presentation of widget capabilities. And I really appreciate that you put effort to create this. I’m really fed up with LLM-generated content, so it’s refreshing to see a post prepared by a human 😄

hey @szymon_dybczak 
Thanks!
Yeah me too, it's somewhat disheartening to look across the Community and see a sea of AI - nice to know there's still a few real humans around! 😂

Cheers,
TheOC

Pilsner
Contributor

I haven't used widgets much before, but I also hadn't realised the power of them. I will certainly be taking advantage from now on. I am curious if there is a specific date widget that allows you to select a date from a calendar-like table? If not, this would be a cool feature to have.

Thoroughly enjoyed the read, looking forward to the rest of the series, thanks @TheOC.

hey @Pilsner,
Thanks for the kind words!
I heard about a date-time picker widget from a blog written a few years ago, but it appears that this was an AI hallucination in the blog unfortunately.

There is also an idea/discussion for it here:
https://community.databricks.com/t5/data-engineering/datepicker-widget/td-p/23186

But doesn't seem like there's much movement there unfortunately.

There would be ways of doing this in external packages but seems like the widgets are limited to these 4 options (although I'd love to be proven wrong!)

Cheers,
TheOC

That's good to know @TheOC , thank you. If I get time, I might have to play around with some external packages, just to see what's possible. 😄

Sujitha
Databricks Employee
Databricks Employee

@TheOC Congratulations on your very first blog with the Databricks Community! 👏 Looking forward to more such content from you! 🚀

Thank you @Sujitha! Hopefully plenty more to come 🥳

Cheers,
TheOC

Khaja_Zaffer
Contributor

Hey congratulations mate @TheOC I just now learnt about widgets - it was great refreshing.

Keep sharing. Good day!

hey @Khaja_Zaffer, thanks for the kind words!

Let me know how you end up using Widgets 🥳

Cheers,
TheOC

Advika
Databricks Employee
Databricks Employee

Well written, @TheOC. The way you put this together really draws people in... might even inspire some to start blogging themselves.

hey @Advika 
Thank you! 

I hope it does inspire others - blogging can be very fun: Just don't let ChatGPT take away all of the creativity 😉

Cheers,
TheOC

mceleavey
New Contributor II

mceleavey_0-1756735745910.gif

 

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