How to set a project variables with Python

me2
me2 Registered Posts: 54 ✭✭✭✭✭

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

Answers

Setup Info
    Tags
      Help me…