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