[docs]@dataclass(frozen=True)classLevelLegendConfig:"""Configuration for the LevelLegend page section. Attributes: template (str | None): Path to a custom template file. If None, defaults to the package template. """template:str=str(Path(__file__).with_name("templates")/"level_legend.html")
[docs]classLevelLegend(PageObj,Configurable[LevelLegendConfig]):"""Renders the Change-Event Level Legend as an HTML fragment. Reads threshold values from the YAML config via `settings`. """def__init__(self,**params:dict[str,Any])->None:"""Initialize the level legend with configuration parameters."""super().__init__(LevelLegendConfig,**params)@propertydeftemplate_path(self)->str:"""Get the path to the template file. Returns: str: The path to the template file. """returnself.config.template
[docs]defrender(self,env:Environment,ctx:Context)->str:# noqa: ARG002"""Renders the level legend template using the provided Jinja2 environment and context. This method checks if the specified template file exists. If not, it logs an error and returns an HTML error message. If the template exists, it ensures the correct Jinja2 environment is used (either the provided one or a new one with the appropriate loader), loads the template, and renders it with threshold values from the application settings. Args: env (Environment): The Jinja2 environment to use for template rendering. ctx (Context): The rendering context (currently unused). Returns: str: The rendered HTML string of the level legend, or an error message if the template is missing. """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>"tmpl=get_local_env(env,self.template_path).get_template(Path(self.template_path).name)th=get_settings().energytrackr.analysis.thresholdsreturntmpl.render(welch_p=th.welch_p,cohen_d=th.cohen_d,pct_change=th.pct_change,practical=th.practical,)