diff --git a/sphinx_needs/directives/needbar.py b/sphinx_needs/directives/needbar.py index 4a73042a5..be69f0076 100644 --- a/sphinx_needs/directives/needbar.py +++ b/sphinx_needs/directives/needbar.py @@ -10,7 +10,7 @@ from sphinx_needs.data import SphinxNeedsData from sphinx_needs.filter_common import FilterBase, filter_needs, prepare_need_list from sphinx_needs.logging import get_logger -from sphinx_needs.utils import add_doc, get_matplotlib, save_matplotlib_figure +from sphinx_needs.utils import add_doc, import_matplotlib, save_matplotlib_figure logger = get_logger(__name__) @@ -74,7 +74,7 @@ def run(self) -> Sequence[nodes.Node]: text_color = text_color.strip() style = self.options.get("style") - matplotlib = get_matplotlib() + matplotlib = import_matplotlib() style = style.strip() if style else (matplotlib.style.use("default") if matplotlib else "default") legend = "legend" in self.options @@ -165,7 +165,7 @@ def process_needbar(app: Sphinx, doctree: nodes.document, fromdocname: str, foun needs_data = SphinxNeedsData(env) needs_config = NeedsSphinxConfig(env.config) - matplotlib = get_matplotlib() + matplotlib = import_matplotlib() if matplotlib is None and found_nodes and needs_config.include_needs: logger.warning( diff --git a/sphinx_needs/directives/needpie.py b/sphinx_needs/directives/needpie.py index 1174f355b..00e8b9ee4 100644 --- a/sphinx_needs/directives/needpie.py +++ b/sphinx_needs/directives/needpie.py @@ -13,7 +13,7 @@ from sphinx_needs.utils import ( add_doc, check_and_get_external_filter_func, - get_matplotlib, + import_matplotlib, save_matplotlib_figure, ) @@ -105,7 +105,7 @@ def process_needpie(app: Sphinx, doctree: nodes.document, fromdocname: str, foun needs_data = SphinxNeedsData(env) needs_config = NeedsSphinxConfig(env.config) - matplotlib = get_matplotlib() + matplotlib = import_matplotlib() if matplotlib is None and found_nodes and needs_config.include_needs: logger.warning( diff --git a/sphinx_needs/utils.py b/sphinx_needs/utils.py index 300d0aac9..7b6c76ed6 100644 --- a/sphinx_needs/utils.py +++ b/sphinx_needs/utils.py @@ -3,7 +3,7 @@ import operator import os import re -from functools import reduce, wraps +from functools import lru_cache, reduce, wraps from re import Pattern from typing import ( TYPE_CHECKING, @@ -384,7 +384,12 @@ def jinja_parse(context: Dict[str, Any], jinja_string: str) -> str: return content -def get_matplotlib() -> Optional["matplotlib"]: +@lru_cache() +def import_matplotlib() -> Optional["matplotlib"]: + """Import and return matplotlib, or return None if it cannot be imported. + + Also sets the interactive backend to ``Agg``, if ``DISPLAY`` is not set. + """ try: import matplotlib import matplotlib.pyplot