-
I was searching for how to combine Jinja2 and pypyr and got a result that said there was a jinja2 step function in pypyr - like the below. Is that a feature that used to be available? `
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @jpdrsn, There isn't a built-in a step for jinja, and there never was. You can however make your own one, it's easy, promise! If you have a custom .py file in your pipeline directory in a import logging
from pathlib import Path
from pypyr.context import Context
from pypyr.utils.asserts import assert_key_has_value
from jinja2 import Environment, FileSystemLoader
# getLogger will grab the parent logger context, so your loglevel and
# formatting automatically will inherit correctly from the pypyr core.
logger = logging.getLogger(__name__)
def run_step(context: Context) -> None:
# friendly error msg if pipeline tries to use this without jinja input key
context.assert_key_has_value(key='jinja', caller=__name__)
# lets you use pypyr {format} expressions on inputs: dir, template, output
step_input = context.get_formatted('jinja')
# friendly err msg if the required child-keys of jinja aren't there, i.e:
# jinja.template, jinja.output
assert_key_has_value(obj=step_input,
key='template',
caller=__name__,
parent='jinja')
assert_key_has_value(obj=step_input,
key='output',
caller=__name__,
parent='jinja')
# defaults to current directory if not set
templates_dir = step_input.get('dir', Path.cwd())
template_file = step_input['template']
outfile = step_input['output']
# Use jinja api to generate output file and write to output file.
file_loader = FileSystemLoader(templates_dir)
env = Environment(loader=file_loader)
template = env.get_template(template_file)
output = template.render(**context)
with open(outfile, 'w', encoding='utf-8') as file:
file.write(output) You can then have steps:
- name: pypyr.steps.set
in:
set:
fruit: apple
vegetable: carrot
arb_input: my-jinja-template.txt
- name: steps.jinja
comment: calling our custom jinja step!
in:
jinja:
template: '{arb_input}'
output: my-output-file.txt Notice you can use the usual pypyr {formatting expressions} on all the inputs to the custom jinja step - so here the The way I did it here is that jinja will use the pypyr context dictionary to find the variables it substitutes. In other words, imagine
|
Beta Was this translation helpful? Give feedback.
Hi @jpdrsn,
There isn't a built-in a step for jinja, and there never was.
You can however make your own one, it's easy, promise!
If you have a custom .py file in your pipeline directory in a
steps
directory, like./steps/jinja.py
: