soft_ben10
soft_ben10

Reputation: 1

Databricks Widget is not getting enabled post execution

I am trying to play around with Widget of dbutils. Post successful execution of code, neither widget is getting displayed or sometimes get displayed but not enabled to insert values.

Trying to run below code:

dbutils.widgets.text("filter_value", "abc","xyz")
print(dbutils.widgets.get("filter_value"))

Widget did not get displayed or not enabled sometimes

Upvotes: 0

Views: 91

Answers (1)

The widget will let you to enter text input dynamically in the notebook interface. You can then use this input in the notebook to perform logic, filter data.

The below is the syntax:

dbutils.widgets.text("widget_name", "default_value", "Label")

I have tried the below:

dbutils.widgets.text("table", "tbl2")

Results:

enter image description here

filter_value = dbutils.widgets.get("filter_value")
if filter_value:
    filtered_df = df.filter(col("Country") == filter_value)
else:
    filtered_df = df
filtered_df.show()

Results:

+-------+----------+
|Country|Population|
+-------+----------+
|  India|      1391|
+-------+----------+

In the above code creating a text widget for filtering by country name & Provding the default value. Getting the value of the widget & applying a filter to the DataFrame based on the widget value.

Upvotes: 0

Related Questions