For Jinja2 template rendering

embed_local_file(filename, filedir='templates')

Embed local file into template

Source code in microview/rendering.py
 9
10
11
12
13
14
15
16
17
def embed_local_file(filename, filedir="templates"):
    """
    Embed local file into template
    """
    herepath = Path(__file__).parent.resolve()
    fullpath = herepath.joinpath(filedir, filename)

    with open(fullpath, "r") as f:
        return f.read()

render_base(tax_plots, dir_path, output_path)

Render base template

Parameters:
  • tax_plots (dict) –

    Dict containing results from microview.plotting.generate_taxo_plots

  • dir_path (Path) –

    Path to directory containing report files]

  • output_path (Path) –

    Path to output file

Source code in microview/rendering.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def render_base(tax_plots: Dict, dir_path: Path, output_path: Path) -> None:
    """
    Render base template

    Args:
        tax_plots (dict): Dict containing results from
            microview.plotting.generate_taxo_plots
        dir_path (Path): Path to directory containing report files]
        output_path (Path): Path to output file
    """
    JINJA_ENV.globals["embed_local_file"] = embed_local_file

    base_template = JINJA_ENV.get_template("base.html")

    curr_time = datetime.now().strftime("%Y-%m-%d, %H:%M")
    rendered_template = base_template.render(
        tax_plots=tax_plots,
        version=__version__,
        dir_path=str(dir_path.resolve()),
        curr_time=curr_time,
    )

    result_path = (
        output_path
        if output_path.suffix == ".html"
        else output_path.with_suffix(".html")
    )

    with open(result_path, "w", encoding="utf-8") as f:
        f.write(rendered_template)