Skip to content

Base

Renderer

Bases: ABC

Base Renderer class

Source code in dvc_render/base.py
12
13
14
15
16
17
18
19
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
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
class Renderer(abc.ABC):
    """Base Renderer class"""

    DIV = """
    <div id="{id}">
      {partial}
    </div>
    """

    EXTENSIONS: Iterable[str] = {}

    def __init__(
        self,
        datapoints: Optional[list] = None,
        name: Optional[str] = None,
        **properties,
    ):
        self.datapoints = datapoints or []
        self.name = name or ""
        self.properties = properties

    @abc.abstractmethod
    def partial_html(self, **kwargs) -> str:
        """
        Us this method to generate HTML content,
        to fill `{partial}` inside self.DIV.
        """
        raise NotImplementedError

    @property
    @abc.abstractmethod
    def TYPE(self):  # noqa: N802
        raise NotImplementedError

    @property
    @abc.abstractmethod
    def SCRIPTS(self):  # noqa: N802
        raise NotImplementedError

    @staticmethod
    def remove_special_chars(string: str) -> str:
        "Ensure string is valid HTML id."
        return string.translate(
            {ord(c): "_" for c in r"!@#$%^&*()[]{};,<>?\/:.|`~=_+ "}
        )

    def generate_html(self, html_path=None) -> str:
        "Return `DIV` formatted with `partial_html`."
        partial = self.partial_html(html_path=html_path)
        if partial:
            div_id = self.remove_special_chars(self.name)

            return self.DIV.format(id=div_id, partial=partial)
        return ""

    def generate_markdown(self, report_path: Optional[StrPath] = None) -> str:  # pylint: disable=missing-function-docstring
        "Generate a markdown element"
        raise NotImplementedError

    @classmethod
    def matches(cls, filename, properties=None) -> bool:  # noqa: ARG003
        "Check if the Renderer is suitable."
        return Path(filename).suffix in cls.EXTENSIONS

generate_html(html_path=None)

Return DIV formatted with partial_html.

Source code in dvc_render/base.py
58
59
60
61
62
63
64
65
def generate_html(self, html_path=None) -> str:
    "Return `DIV` formatted with `partial_html`."
    partial = self.partial_html(html_path=html_path)
    if partial:
        div_id = self.remove_special_chars(self.name)

        return self.DIV.format(id=div_id, partial=partial)
    return ""

generate_markdown(report_path=None)

Generate a markdown element

Source code in dvc_render/base.py
67
68
69
def generate_markdown(self, report_path: Optional[StrPath] = None) -> str:  # pylint: disable=missing-function-docstring
    "Generate a markdown element"
    raise NotImplementedError

matches(filename, properties=None) classmethod

Check if the Renderer is suitable.

Source code in dvc_render/base.py
71
72
73
74
@classmethod
def matches(cls, filename, properties=None) -> bool:  # noqa: ARG003
    "Check if the Renderer is suitable."
    return Path(filename).suffix in cls.EXTENSIONS

partial_html(**kwargs) abstractmethod

Us this method to generate HTML content, to fill {partial} inside self.DIV.

Source code in dvc_render/base.py
33
34
35
36
37
38
39
@abc.abstractmethod
def partial_html(self, **kwargs) -> str:
    """
    Us this method to generate HTML content,
    to fill `{partial}` inside self.DIV.
    """
    raise NotImplementedError

remove_special_chars(string) staticmethod

Ensure string is valid HTML id.

Source code in dvc_render/base.py
51
52
53
54
55
56
@staticmethod
def remove_special_chars(string: str) -> str:
    "Ensure string is valid HTML id."
    return string.translate(
        {ord(c): "_" for c in r"!@#$%^&*()[]{};,<>?\/:.|`~=_+ "}
    )