Use a template in a flow notebook
To use a template inside of the flow notebook, start by creating a new template using the syntax in the following example:
time_diff_template = TemplateV2(
name="time_diff", description="Calculate the time difference between two dates",
source="CUSTOM", status="ACTIVE", tags=["UI", "Scalar"]
)
Give your template a name , description , and tags . For now, your source should always be “CUSTOM” and status should always be “ACTIVE”
Next, you will add the transform notebook you have made to a template transform. See the following example for syntax:
time_diff_template_transform = TemplateTransformV2(
type = "python", params=dict(notebookName="timediff.ipynb"))
Then you will add the template transform to the template and publish your template:
time_diff_template.base_transforms = [time_diff_template_transform]
time_diff_template.publish("transforms/timediff.ipynb")
To use your published template as a transform, you then need to create a transform object, assign its templateId for the id of your published template, give it a name, and pass in the values you want to use for your variables:
calculate_age_transform = Transform()
calculate_age_transform.templateId = time_diff_template.id
calculate_age_transform.name='age'
calculate_age_transform.variables = {
'inputDataset': 'employee',
'start_date': 'birth_date',
'end_date': 'start_date',
'how': 'years',
'outputcolumn': 'age',
'outputDataset': 'employee_with_age'
}
Note that the names of the variables were defined previously inside of the transform notebook with the get_or_create…() method.
Now you are ready to add the transform to a recipe and run the recipe.
calculate_age_recipe.add_transform(calculate_age_transform)
calculate_age_recipe.run()