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

3 REPLIES 3

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
Data engineer at Rsystema

anardinelli
Databricks Employee
Databricks Employee

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

The code you have shared will not work. You can try for yourself.

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