class wxflow.Jinja(template_path_or_string: str, data: Dict, allow_missing: bool = True, searchpath: str | List = '/')[source]

Bases: object

Description

A wrapper around jinja2 to render templates

Description

Given a path to a (jinja2) template and a data object, substitute the template file with data. Allow for retaining missing or undefined variables. Also provide additional search paths for templates that may be included in the main template :Parameters: * template_path_or_string (str) – Path to the template file or a templated string

  • data (dict) – Data to be substituted into the template TODO: make “data” optional so that the user can render the same template with different data

  • allow_missing (bool) – If True, allow for missing or undefined variables

  • searchpath (str | list) – Additional search paths for templates (default ‘/’)

static add_filter_to_env(env: Environment, filter_name: str, filter_func: callable) Environment[source]

Description

Add a custom filter to the jinja2 environment :Parameters: * env (jinja2.Environment) – Active jinja2 environment

  • filter_name (str) – name of the filter

  • filter_func (callable) – function that will be called

returns:

env – Active jinja2 environment with the new filter added

rtype:

jinja2.Environment

property dump: None

Description

Render and dump the output to stdout :rtype: None

get_set_env(loader: BaseLoader, filters: Dict[str, callable] = None) Environment[source]

Description

Define the environment for the jinja2 template Any number of filters can be added here. Optionally, a dictionary of external filters can be passed in

Currently, the following filters are defined: strftime: convert a datetime object to a string with a user defined format to_isotime: convert a datetime object to an ISO 8601 string to_fv3time: convert a datetime object to a FV3 time string to_YMDH: convert a datetime object to a YYYYMMDDHH string to_YMD: convert a datetime object to a YYYYMMDD string to_julian: convert a datetime object to a julian day to_f90bool: convert a boolean to a fortran boolean relpath: convert a full path to a relative path based on an input root_path getenv: read variable from environment if defined, else UNDEFINED to_timedelta: convert a string to a timedelta object add_to_datetime: add time to a datetime, return new datetime object

The Expression Statement extension “jinja2.ext.do”, which enables

{% do … %} statements. These are useful for appending to lists. e.g. {{ bar.append(foo) }} would print “None” to the parsed jinja template, but {% do bar.append(foo) %} would not.

Parameters:
  • loader (jinja2.BaseLoader) – An instance of class jinja2.BaseLoader

  • filters (Dict[str, callable] (optional)) – A dictionary of filters to be added to the environment

returns:

env

rtype:

jinja2.Environment

property render: str

Description

Render the Jinja2 template with the data :Parameters: None

returns:
  • rendered (str)

  • Rendered template into text

save(output_file: str) None[source]

Description

Render and save the output to a file :Parameters: * output_file (str)

  • Path to the output file

rtype:

None