Skip to content

Commit 721b1be

Browse files
committed
feat: check that brand_yml is installed before using
1 parent 9591fb2 commit 721b1be

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

shiny/ui/_theme.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import re
66
import tempfile
77
import textwrap
8-
from typing import Any, Literal, Optional, Sequence, TypeVar
8+
from typing import TYPE_CHECKING, Any, Literal, Optional, Sequence, TypeVar
99

10-
from brand_yml import Brand
10+
if TYPE_CHECKING:
11+
from brand_yml import Brand
1112
from htmltools import HTMLDependency
1213

1314
from .._docstring import add_example
@@ -410,7 +411,7 @@ def to_css(
410411
self._css = self._read_precompiled_css()
411412
return self._css
412413

413-
check_libsass_installed()
414+
check_theme_pkg_installed("libsass", "sass")
414415
import sass
415416

416417
args: SassCompileArgs = {} if compile_args is None else compile_args
@@ -525,6 +526,10 @@ def from_brand(cls, brand: str | pathlib.Path | Brand):
525526
A :class:`shiny.ui.Theme` instance with a custom Shiny theme created from
526527
the brand guidelines (see :class:`brand_yml.Brand`).
527528
"""
529+
check_theme_pkg_installed("brand_yml")
530+
531+
from brand_yml import Brand
532+
528533
from ._theme_brand import ThemeBrand # avoid circular import
529534

530535
if not isinstance(brand, Brand):
@@ -559,13 +564,16 @@ def check_is_valid_preset(preset: ShinyThemePreset) -> None:
559564
)
560565

561566

562-
def check_libsass_installed() -> None:
567+
def check_theme_pkg_installed(pkg: str, spec: str | None = None) -> None:
563568
import importlib.util
564569

565-
if importlib.util.find_spec("sass") is None:
570+
if spec is None:
571+
spec = pkg
572+
573+
if importlib.util.find_spec(spec) is None:
566574
raise ImportError(
567-
"The 'libsass' package is required to compile custom themes. "
568-
'Please install it with `pip install libsass` or `pip install "shiny[theme]"`.',
575+
f"The '{pkg}' package is required to compile custom themes. "
576+
'Please install it with `pip install {pkg}` or `pip install "shiny[theme]"`.',
569577
)
570578

571579

0 commit comments

Comments
 (0)