Editing value of widget parameter within notebook code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2024 08:30 PM
Hi ,
The dbutils.widgets.text() function in Databricks takes three positional arguments. They are:
- name: The programmatic name of the text widget.
- defaultValue: The default value of the text widget.
- 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)
Data engineer at Rsystema
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 08:37 AM
The code you have shared will not work. You can try for yourself.
![](/skins/images/582998B45490C7019731A5B3A872C751/responsive_peak/images/icon_anonymous_message.png)
![](/skins/images/582998B45490C7019731A5B3A872C751/responsive_peak/images/icon_anonymous_message.png)