Hi Team! I’m currently trying to create a dynamic ...
# questions
g
Hi Team! I’m currently trying to create a dynamic catalog on Kedro with Jinja2 but struggling to import the list on which to build the catalog entries. I’m new to Jinja so the solution might be trivial. For reproducibility sake I’m basing the following examples on the jinja-example 🙂. When considering a hard-coded list in the file
conf/base/catalog.yml
as follows
Copy code
{% for region in ['parasubicular', 'parainsular'] %}

{{ region }}.data_right:
    type: PartitionedDataSet
    path: data/01_raw/ClinicalDTI/R_VIM/seedmasks/
    dataset: pandas.CSVDataSet
    filename_suffix: /{{ region  }}_R_T1.nii.gz

{{ region }}.data_right_output:
    type: pandas.CSVDataSet
    filepath: data/03_primary/{{ region }}_output.csv

{% endfor %}
everything works fine. However, I need to iterate over a list that is not practical to hard-code therefore I was hoping to have something like follows
Copy code
regions:
  - 'parasubicular'
  - 'parainsular'

{% for region in regions %}

{{ region }}.data_right:
    type: PartitionedDataSet
    path: data/01_raw/ClinicalDTI/R_VIM/seedmasks/
    dataset: pandas.CSVDataSet
    filename_suffix: /{{ region  }}_R_T1.nii.gz

{{ region }}.data_right_output:
    type: pandas.CSVDataSet
    filepath: data/03_primary/{{ region }}_output.csv

{% endfor %}
but no matter where I define the
regions
list (i tried to define it in different
.yml
files) I stumble on the same error screen-shotted below. Do you by chance know if I have to save the jinja pattern in a different file, there is a specific place where I have to save the list that I want to read, or if I have to change the parsing somehow? Thank you in advance!!
g
I think you need to set that variable in Jinja, so something like this:
Copy code
{% set regions=['parasubicular', 'parainsular'] %}

{% for region in regions %}

{{ region }}.data_right:
    type: PartitionedDataSet
    path: data/01_raw/ClinicalDTI/R_VIM/seedmasks/
    dataset: pandas.CSVDataSet
    filename_suffix: /{{ region  }}_R_T1.nii.gz

{{ region }}.data_right_output:
    type: pandas.CSVDataSet
    filepath: data/03_primary/{{ region }}_output.csv

{% endfor %}
g
Thank you! This method does work, however I was hoping to somehow be able to pass the list parametrically, without having to modify the file where the Jinja code is 😅 Perhaps it is not possible
n
Sorry for late reply - I believe it doesn't work with
TemplatedConfigLoader
, it should be possible though with
ac_context
but you will have to create a custom config loader (probably similar to TemplatedConfigLoader)
K 1