From 33d3aafbd924151fd87a9ef10ac7ba77385e10e5 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 30 May 2026 06:47:46 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Cache=20FAQ=20YAML=20loadin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implemented `_load_faqs` with `@functools.cache` to avoid repeated parsing of the `faqs.yml` file every time `build_faqs` is called. Used `copy.deepcopy` to prevent callers from modifying the global cached object. Co-authored-by: alinelena <3306823+alinelena@users.noreply.github.com> --- ml_peg/app/utils/build_components.py | 29 ++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/ml_peg/app/utils/build_components.py b/ml_peg/app/utils/build_components.py index d5dba5742..dfafc6033 100644 --- a/ml_peg/app/utils/build_components.py +++ b/ml_peg/app/utils/build_components.py @@ -2,6 +2,8 @@ from __future__ import annotations +import copy +import functools from importlib import metadata from pathlib import Path import time @@ -509,6 +511,24 @@ def build_image_download_controls(image_id: str, uris: dict) -> Div: ) +@functools.cache +def _load_faqs() -> list[dict[str, str]] | None: + """ + Load and cache FAQs from the YAML file. + + Returns + ------- + list[dict[str, str]] | None + Loaded FAQs list or None if not found. + """ + faqs_path = Path(__file__).parent / "faqs.yml" + try: + with open(faqs_path, encoding="utf8") as f: + return yaml.safe_load(f) + except FileNotFoundError: + return None + + def build_faqs() -> Div: """ Build FAQ section with collapsible dropdowns from YAML file. @@ -519,12 +539,8 @@ def build_faqs() -> Div: Styled FAQ section with questions as dropdown titles and answers inside. """ # Load FAQs from YAML file - faqs_path = Path(__file__).parent / "faqs.yml" - - try: - with open(faqs_path, encoding="utf8") as f: - faqs_data = yaml.safe_load(f) - except FileNotFoundError: + faqs_data_cached = _load_faqs() + if faqs_data_cached is None: return Div( "FAQs file not found", style={ @@ -533,6 +549,7 @@ def build_faqs() -> Div: "fontStyle": "italic", }, ) + faqs_data = copy.deepcopy(faqs_data_cached) if not faqs_data or not isinstance(faqs_data, list): return Div("No FAQs available")