-2

I have a jinja templates (python files) with several variables like this {{ some_variable }}. Then I have a yml files with the defined variable values.

python/jinja template:

import datetime

some_variable = '{{ some_variable }}'

yaml file:

some_variable: 'some value'

desired output:

import datetime

some_variable = 'some value'

What is the fastest/easiest way to fill the templates with the yml variable values?

romanzdk
  • 930
  • 11
  • 30

1 Answers1

0

Ok, ended up with:

from jinja2 import Environment, FileSystemLoader
import click
import yaml

def main(template_file: str, variables_file: str) -> None:
    with open(variables_file) as fin:
        variables = yaml.load(fin, Loader = yaml.SafeLoader)

    env = Environment(loader = FileSystemLoader('./'))
    template = env.get_template(template_file)

    print(template.render(**variables))
romanzdk
  • 930
  • 11
  • 30