cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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!

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group