Source code for cosapp.tools.views.baseRenderer

import io
import abc
from jinja2.environment import Template

from cosapp.systems import System
from cosapp.utils.helpers import check_arg


[docs] class BaseRenderer(metaclass=abc.ABCMeta): """Base class to export a system as HTML. Parameters ---------- system : System System to export embeddable: bool, optional Is the HTML to be embedded in an existing page? Default: False """ def __init__(self, system: System, embeddable=False): check_arg(system, "system", System) self.system = system self.embeddable = embeddable
[docs] @abc.abstractmethod def get_data(self, **kwarg) -> dict: """Convert `self.system` into a dictionary used to build the HTML page. Returns ------- dict Dictionary containing elements to create an HTML page. """
[docs] @abc.abstractmethod def html_content(self) -> str: """Returns HTML content of renderer's system as a character string."""
[docs] def dump(self, fstream: io.RawIOBase) -> None: """Dump HTML content into writable fstream""" rendered_html = self.html_content() fstream.write(rendered_html)
[docs] def to_file(self, filename: str) -> None: """Dump HTML content into text file `filename`""" with open(filename, "w", encoding="utf-8") as fp: self.dump(fp)
[docs] @classmethod def html_tags(cls) -> dict[str, str]: html_begin_tags = """<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>{{title}}</title> </head> <body>\n """ html_end_tags = """ </body> </html> """ return { "html_begin_tags": html_begin_tags, "html_end_tags": html_end_tags, }
[docs] @classmethod @abc.abstractmethod def html_resources(cls) -> dict[str, str]: """Return the necessary resources to render a Jinja template."""
[docs] @classmethod @abc.abstractmethod def html_template(cls) -> Template: """Return the Jinja template used to create an HTML file."""
[docs] @classmethod def get_globals(cls) -> dict: """Returns a dict containing class-wide environment and HTML data""" return dict( template=cls.html_template(), **cls.html_tags(), **cls.html_resources(), )