Perhaps I’m just a bit dense but as per the docs J...
# prefect-community
t
Perhaps I’m just a bit dense but as per the docs JinjaTemplate() should take **kwargs (https://docs.prefect.io/api/unreleased/tasks/strings.html#jinjatemplate). Yet when I pass it keyword arguments it complains about super().__init__() getting unexpected keyword arguments. Bug?
e
What I understand from the source code, is that task initialization for
JinjaTemplate(Task)
only accepts generic task initialization
**kwargs
, not your formatting params. Formatting params, i.e.
**format_kwargs
are passed strictly at flow runtime, in other words
flow.run()
👍 1
t
Ah, that makes sense!
Thanks!
while I’m at it: How should I refer to variable from the flow context in the jinja templates? I.e. how would I reference a parameter?
e
From the comments in source code:
Copy code
python
    from prefect import Flow
    from prefect.tasks.templates import JinjaTemplate
    message = '''
    Hi {{name}}!  Welcome to Prefect.  Today is {{today}}.
    '''
    msg_task = JinjaTemplate(name="message body", template=message)
    with Flow("string-template") as flow:
            output = msg_task(name="Marvin")
    flow_state = flow.run()
    print(flow_state.result[output].result)
    # Hi Marvin!  Welcome to Prefect.  Today is 2019-08-28.
I’m constantly telling source code this source code that, because I actually haven’t used jinja2 outside of dbt, so no experience with jinja in prefect or python whatsoever 😅 Not trying to be rude.
t
I saw that. But my question wasn’t how to reference variables explicitly passed to
JinjaTemplate()
but how to reference variables from the context, which the docs say is possible
e
oh, my bad. Try the same key name in the jinja template. It looks like it works with
{{today}}
, since the today key is provided by the prefect context.
as in
{{task_name}}
or
{{scheduled_start_time}}