cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

current date as default in a widget while scheduling the notebook

philip
New Contributor

I have a scheduled a notebook. can I keep current date as default in widget whenever the notebook run and also i need the flexibility to change the widget value to any other date based on the ad hoc run that I do.

1 ACCEPTED SOLUTION

Accepted Solutions

Hubert-Dudek
Esteemed Contributor III

second parameter is default value, here is example code:

data = [{"Category": 'Category A', "ID": 1, "Value": 12.40},
        {"Category": 'Category B', "ID": 2, "Value": 30.10},
        {"Category": 'Category C', "ID": 3, "Value": 100.01}
        ]
df = spark.createDataFrame(data)
 
values = [row.ID for row in df.select('ID').collect()]
default_value = str(max(values))
values_str = [str(value) for value in values]
dbutils.widgets.dropdown("ID", default_value, values_str)

View solution in original post

2 REPLIES 2

Hubert-Dudek
Esteemed Contributor III

second parameter is default value, here is example code:

data = [{"Category": 'Category A', "ID": 1, "Value": 12.40},
        {"Category": 'Category B', "ID": 2, "Value": 30.10},
        {"Category": 'Category C', "ID": 3, "Value": 100.01}
        ]
df = spark.createDataFrame(data)
 
values = [row.ID for row in df.select('ID').collect()]
default_value = str(max(values))
values_str = [str(value) for value in values]
dbutils.widgets.dropdown("ID", default_value, values_str)

-werners-
Esteemed Contributor III

So building on the answer of Hubert:

from datetime import date

date_for_widget = date.today()

So if you use date_for_widget as your default value, you are there.

And ofc you can fill this date_for_widget variable with anything you want.

You can even fetch a value from outside databricks, f.e. with Azure Data Factory.

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