How to set a project variables with Python
I am trying to set project variables that will be used by scenario recipes as dates. In a project the global variable is in the form of {"start_date": "10/27/2024"}. I am able to this in recipes and dashboards.
Using the python notebook, I am able to change the variables but now I want to make it dynamic based on current dates. Here is the code and I am having 2 issues.
First the format output from python not exactly the same format. I have not tried to use this in downstream recipes because I'm still trying to solve setting the variable.
new date 2024-10-28
Second when I try to replace the variable, I get a TypeError of errors in the python notebook.
TypeError: Object of type date is not JSON serializable
So I'm missing something in my code. Here is the code from Python notebook
import dataiku from datetime import timedelta, date project = dataiku.Project() variables = project.get_variables() x = date.today() days_diff = timedelta(days=-7) new_date = x + days_diff variables["standard"]["start_date"] = new_date project.set_variables(variables)
Thank you in advance for assisting!
Operating system used: Windows
Best Answer
-
Turribeach Dataiku DSS Core Designer, Neuron, Dataiku DSS Adv Designer, Registered, Neuron 2023 Posts: 2,088 Neuron
As the update_variables() method documents the variables must be strings, numbers, booleans, lists or dicts. Dates are not supported. Convert to string and make the type is a string before you try to update the variable.
Answers
-
Marlan Neuron 2020, Neuron, Registered, Dataiku Frontrunner Awards 2021 Finalist, Neuron 2021, Neuron 2022, Dataiku Frontrunner Awards 2021 Participant, Neuron 2023 Posts: 320 Neuron
Hi @me2,
Try converting the date to a string like this: new_date.strftime("%Y-%m-%d").
As a side note, I typically include single quotes in the variable definition, like this {"start_date": "'10/27/2024'"}. The advantage is that I can replace the date literal with expressions like CURRENT_DATE without changing the code that uses the variable (typically SQL in my case).
Marlan
-
I tried converting and it printed "2024-10-28" but I get the same TypeError. I also tried modifying string to get "2024/10/28" with same error.
-
Thank you @Marlan and @Turribeach I was able to get it to work in the Python notebook by changing the format then converting to a string.
# convert to string new_date_str = str(new_date.strftime("%Y/%m/%d")) # update variable variables["standard"]["start_date"] = new_date_str
Now the final step, use it in a scenario step which I hope goes smoother than this did. 😉