cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Knowledge Sharing Hub
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:ย 

Editing value of widget parameter within notebook code

DavidOBrien
New Contributor

I have a notebook with a text widget where I want to be able to edit the value of the widget within the notebook and then reference it in SQL code. For example, assuming there is a text widget named Var1 that has input value "Hello", I would want to be able to do the following:

 

originalValue = dbutils.widgets.get('Var1')

dbutils.widgets.text('Var1', originalValue + ", World!")

%sql

select '${Var1}' -> returns 'Hello, World!'

 

However, whenever I try an approach like this in my notebooks, it does not update the value in Var1 and will select the original 'Hello' value. How can I fix this? Thank you

2 REPLIES 2

Hkesharwani
Contributor II

Hi ,

The dbutils.widgets.text() function in Databricks takes three positional arguments. They are:

  1. name: The programmatic name of the text widget.
  2. defaultValue: The default value of the text widget.
  3. label: An optional label for the text widget.
    From the above code you are trying to pass a default value, and you are already passing a value to widget var1, that's why the code always returns 'Hello'.

    I am not aware of a way to directly manipulate the value of widget, but the below code can help you to take the value from widget and then modify and use in the SQL.

 

# Get the parameters
dbutils.widgets.text('param1',"Hello") #Default value as Hello
param1 = dbutils.widgets.get("param1")
# Printing value of param1 
print(f"Parameter 1: {param1}")
#manupualting the value using python
param1=param1+' World!'
print(param1)
#defining spark varaiable with value of modified variable param1 
spark.sql(f"set param1.var='{param1}'")

--------------

%sql
SELECT cast(${param1.var} as string)
โ€‹

Hkesharwani_0-1716089413844.png

 

 

Harshit Kesharwani
Self-taught Data Engineer | Seeking Remote Full-time Opportunities

anardinelli
New Contributor III
New Contributor III

Hi @DavidOBrien, how are you?

You can try the following approach:

# Get the current value of the widget
current_value = dbutils.widgets.get("widget_name")

# Append the new value to the current value
new_value = current_value + "appended_value"

# Set the widget value with the updated string
dbutils.widgets.text("widget_name", new_value)

Please note that this will work if the widget is a text widget. If the widget is of a different type, you might need to convert the values to the appropriate type before appending and setting the new value.

Let me know if it helps.

Best,

Alessandro

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!