โ03-20-2024 11:13 AM
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
โ05-18-2024 08:30 PM
Hi ,
The dbutils.widgets.text() function in Databricks takes three positional arguments. They are:
# 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)
โ
โ05-21-2024 08:08 AM
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
โ11-07-2024 08:37 AM
The code you have shared will not work. You can try for yourself.
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