cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Can you delete a widget, or force a value to it?

andresfp14
New Contributor II

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"))

0693f000007OoHmAAK

2 REPLIES 2

andresfp14
New Contributor II

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"))

Gim
Contributor

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!

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.