Skip to content

reports

Utility class for generating a config file from a jinja template.

https://github.com/oesteban/endofday/blob/f2e79c625d648ef45b08cc1f11fd0bd84342d604/endofday/core/template.py.

Along with other report-related functions.

Classes:

  • Template

    Simplified jinja2 template class from oesteban.

Template

Template(template_str: str)

Simplified jinja2 template class from oesteban.

Methods:

  • compile

    Generate a string with the replacements.

  • generate_conf

    Save the outcome after replacement on the template to file.

Source code in src/pyfsviz/reports.py
47
48
49
50
51
52
53
54
55
56
57
def __init__(self, template_str: str) -> None:
    # Load relative to the template's parent directory so absolute paths work
    # on Windows (FileSystemLoader("/") only works on Unix).
    template_path = Path(template_str).resolve()
    self._template_name = template_path.name
    self._env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(searchpath=str(template_path.parent)),
        trim_blocks=True,
        lstrip_blocks=True,
        autoescape=True,
    )

compile

compile(configs: dict) -> str

Generate a string with the replacements.

Source code in src/pyfsviz/reports.py
59
60
61
62
def compile(self, configs: dict) -> str:
    """Generate a string with the replacements."""
    template = self._env.get_template(self._template_name)
    return template.render(configs)

generate_conf

generate_conf(configs: dict, path: str) -> None

Save the outcome after replacement on the template to file.

Source code in src/pyfsviz/reports.py
64
65
66
67
68
def generate_conf(self, configs: dict, path: str) -> None:
    """Save the outcome after replacement on the template to file."""
    output = self.compile(configs)
    with open(path, "w+", encoding="utf-8") as output_file:
        output_file.write(output)