File size: 1,226 Bytes
a3624d5 |
1 2 3 4 5 6 7 8 9 10 11 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 |
import pathlib
from typing import List
from ray.util.annotations import DeveloperAPI
@DeveloperAPI
class Template:
"""Class which provides basic HTML templating."""
def __init__(self, file: str):
with open(pathlib.Path(__file__).parent / "templates" / file, "r") as f:
self.template = f.read()
def render(self, **kwargs) -> str:
"""Render an HTML template with the given data.
This is done by replacing instances of `{{ key }}` with `value`
from the keyword arguments.
Returns:
HTML template with the keys of the kwargs replaced with corresponding
values.
"""
rendered = self.template
for key, value in kwargs.items():
if isinstance(value, List):
value = "".join(value)
rendered = rendered.replace("{{ " + key + " }}", value if value else "")
return rendered
@staticmethod
def list_templates() -> List[pathlib.Path]:
"""List the available HTML templates.
Returns:
A list of files with .html.j2 extensions inside ../templates/
"""
return (pathlib.Path(__file__).parent / "templates").glob("*.html.j2")
|