Can you delete a widget, or force a value to it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2018 07:46 AM
Hello everyone!
Basically, I have a dropdown widget, and at some point I want to change the options available, but still maintain the same selected option.
The problems that I find are:
- If I want to delete the widget and create a new one, it seems like the object was not deleted and the "index" of the selected value stayed.
- the dbutils.widgets.dropdown receive a defaultValue, not the selected value. (is there a function to assign the value?)
- When I change the list of options with dbutils.widgets.dropdown, I end up viewing (displayed widget), and getting (dbutils.widgets.get()) different values.
I'm attaching some code that contains said problem.
😄 , help?
if not("val" in locals()):
dbutils.widgets.dropdown("A", "4",["1","2","3","4","5","6","7"], "text")
val=dbutils.widgets.get("A")
if (val=="5"):
dbutils.widgets.remove("A")
dbutils.widgets.dropdown("A", "4",["1","3","4","5","6","7"], "text")
print(dbutils.widgets.get("A"))
if (val=="3"):
dbutils.widgets.remove("A")
dbutils.widgets.dropdown("A", "4",["1","2","3","4","5","6","7"], "text")
print(dbutils.widgets.get("A"))
- Labels:
-
Widgets
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2018 07:48 AM
by the way, this is the code:
if not("val" in locals()):
dbutils.widgets.dropdown("A", "4",["1","2","3","4","5","6","7"], "text")
val=dbutils.widgets.get("A")
if (val=="5"):
dbutils.widgets.remove("A")
dbutils.widgets.dropdown("A", "4",["1","3","4","5","6","7"], "text")
print(dbutils.widgets.get("A"))
if (val=="3"):
dbutils.widgets.remove("A")
dbutils.widgets.dropdown("A", "4",["1","2","3","4","5","6","7"], "text")
print(dbutils.widgets.get("A"))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2023 07:16 AM
I know this is 5+ years late but I would like to add an answer (or work around) to this in case anyone else was looking for the same solution.
What I did was to essentially 'reset' the widget whenever I had to assign (or force) a value to it.
#Cell #1
dbutils.widgets.dropdown("ENVIRONMENT", "DEV", Seq('DEV', 'PROD'))
#Cell #2
dbutils.widgets.removeAll()
# Alternatively
# dbutils.widgets.remove('ENVIRONMENT')
# Cell #3
# ENV = <some logic here>
dbutils.widgets.text('ENVIRONMENT', ENV)
Above seems like cheating and messy to be honest but it works for my use case. I leave the 1st cell as is just in case I needed it for interactive debugging within the notebook. After all, cell #1 won't matter once the notebook is plugged into a job/workflow
Honestly, I'm just hoping there is a corresponding <set> method available for widgets alongside the available <get> method.
If there's anyone else out there with a better solution as of 2023, I'm all ears!

