Sunday - last edited Sunday
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!
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:
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:
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:
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:
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 ๐
yesterday - last edited yesterday
@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
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
yesterday
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.
yesterday
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 ๐
yesterday
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! ๐
yesterday
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.
yesterday
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!)
yesterday - last edited yesterday
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. ๐
yesterday
@TheOC Congratulations on your very first blog with the Databricks Community! ๐ Looking forward to more such content from you! ๐
yesterday
Thank you @Sujitha! Hopefully plenty more to come ๐ฅณ
yesterday
Hey congratulations mate @TheOC I just now learnt about widgets - it was great refreshing.
Keep sharing. Good day!
2 hours ago
hey @Khaja_Zaffer, thanks for the kind words!
Let me know how you end up using Widgets ๐ฅณ
yesterday
Well written, @TheOC. The way you put this together really draws people in... might even inspire some to start blogging themselves.
2 hours ago
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 ๐
yesterday
Passionate about hosting events and connecting people? Help us grow a vibrant local communityโsign up today to get started!
Sign Up Now