Skip to content

Html

HTML

Source code in dvc_render/html.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class HTML:
    SCRIPTS_PLACEHOLDER = "scripts"
    PLOTS_PLACEHOLDER = "plot_divs"
    PLOTS_PLACEHOLDER_FORMAT_STR = f"{{{PLOTS_PLACEHOLDER}}}"
    REFRESH_PLACEHOLDER = "refresh_tag"
    REFRESH_TAG = '<meta http-equiv="refresh" content="{}">'

    def __init__(
        self,
        template: Optional[str] = None,
        refresh_seconds: Optional[int] = None,
    ):
        template = template or PAGE_HTML
        if self.PLOTS_PLACEHOLDER_FORMAT_STR not in template:
            raise MissingPlaceholderError(self.PLOTS_PLACEHOLDER_FORMAT_STR)

        self.template = template
        self.elements: list[str] = []
        self.scripts: str = ""
        self.refresh_tag = ""
        if refresh_seconds is not None:
            self.refresh_tag = self.REFRESH_TAG.format(refresh_seconds)

    def with_scripts(self, scripts: str) -> "HTML":
        "Extend scripts element."
        if scripts not in self.scripts:
            self.scripts += f"\n{scripts}"
        return self

    def with_element(self, html: str) -> "HTML":
        "Adds custom html element."
        self.elements.append(html)
        return self

    def embed(self) -> str:
        "Format HTML template with all elements."
        kwargs = {
            self.SCRIPTS_PLACEHOLDER: self.scripts,
            self.PLOTS_PLACEHOLDER: "\n".join(self.elements),
            self.REFRESH_PLACEHOLDER: self.refresh_tag,
        }
        for placeholder, value in kwargs.items():
            self.template = self.template.replace("{" + placeholder + "}", value)
        return self.template

embed()

Format HTML template with all elements.

Source code in dvc_render/html.py
70
71
72
73
74
75
76
77
78
79
def embed(self) -> str:
    "Format HTML template with all elements."
    kwargs = {
        self.SCRIPTS_PLACEHOLDER: self.scripts,
        self.PLOTS_PLACEHOLDER: "\n".join(self.elements),
        self.REFRESH_PLACEHOLDER: self.refresh_tag,
    }
    for placeholder, value in kwargs.items():
        self.template = self.template.replace("{" + placeholder + "}", value)
    return self.template

with_element(html)

Adds custom html element.

Source code in dvc_render/html.py
65
66
67
68
def with_element(self, html: str) -> "HTML":
    "Adds custom html element."
    self.elements.append(html)
    return self

with_scripts(scripts)

Extend scripts element.

Source code in dvc_render/html.py
59
60
61
62
63
def with_scripts(self, scripts: str) -> "HTML":
    "Extend scripts element."
    if scripts not in self.scripts:
        self.scripts += f"\n{scripts}"
    return self

render_html(renderers, output_file, html_template=None, refresh_seconds=None)

Use renderers to fill an HTML template and write to output_file.

Source code in dvc_render/html.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def render_html(
    renderers: list["Renderer"],
    output_file: "StrPath",
    html_template: Optional["StrPath"] = None,
    refresh_seconds: Optional[int] = None,
) -> "StrPath":
    "Use `renderers` to fill an HTML template and write to `output_file`."
    output_path = Path(output_file)
    output_path.parent.mkdir(exist_ok=True)

    page_html: Optional[str] = None
    if html_template and Path(html_template).is_file():
        page_html = Path(html_template).read_text(encoding="utf8")
    elif isinstance(html_template, str):
        page_html = html_template

    document = HTML(page_html, refresh_seconds=refresh_seconds)

    sorted_renderers = sorted(
        renderers,
        key=_order_image_per_step,
    )

    for renderer in sorted_renderers:
        document.with_scripts(renderer.SCRIPTS)
        document.with_element(renderer.generate_html(html_path=output_path))

    output_path.write_text(document.embed(), encoding="utf8")

    return output_file