Source code for energytrackr.plot.builtin_page_sections.base
"""Base page section for energytrackr."""from__future__importannotationsfromdataclassesimportdataclassfrompathlibimportPathfromtypingimportAnyfromjinja2importEnvironmentfromjinja2.environmentimportTemplatefromenergytrackr.plot.configimportget_settingsfromenergytrackr.plot.core.contextimportContextfromenergytrackr.plot.core.interfacesimportConfigurable,PageObjfromenergytrackr.utils.loggerimportloggerfromenergytrackr.utils.utilsimportget_local_env
[docs]@dataclass(frozen=True)classBaseConfig:"""Configuration for the Base page section."""template:str=str(Path(__file__).with_name("templates")/"base.html")
[docs]classBase(PageObj,Configurable[BaseConfig]):"""Base page section for energytrackr. This class serves as a base for creating page sections in the energytrackr application. It provides a template path and a render method to generate HTML content using Jinja2 templates. """def__init__(self,**params:dict[str,Any])->None:"""Initialize the Base page section. Args: params (dict[str, Any]): Configuration parameters for the page section. """super().__init__(BaseConfig,**params)@propertydeftemplate_path(self)->str:"""Get the path to the template file. Returns: str: Path to the template file. """returnself.config.template
[docs]defrender(self,env:Environment,ctx:Context)->str:# noqa: ARG002"""Render the page section using Jinja2 templates. Args: env (Environment): Jinja2 environment for rendering templates. ctx (Context): Context object containing data for rendering. Returns: str: Rendered HTML content. """ifnotPath(self.template_path).is_file():logger.error("LevelLegend: template '%s' not found.",self.template_path)return"<p><strong>Error:</strong> level legend template missing.</p>"settings=get_settings()tmpl:Template=get_local_env(env,self.template_path).get_template(Path(self.template_path).name)returntmpl.render(font=settings.energytrackr.report.font)