Skip to content

Commit ef936ea

Browse files
committed
feat(Theme): Add from_brand() method
1 parent b73470f commit ef936ea

File tree

4 files changed

+259
-104
lines changed

4 files changed

+259
-104
lines changed

shiny/ui/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@
155155
update_sidebar,
156156
)
157157
from ._theme import Theme
158-
from ._theme_brand import ThemeBrand
159158
from ._tooltip import tooltip
160159
from ._utils import js_eval
161160
from ._valuebox import (
@@ -325,7 +324,6 @@
325324
"ShowcaseLayout",
326325
# _theme
327326
"Theme",
328-
"ThemeBrand",
329327
# _tooltip
330328
"tooltip",
331329
# _progress

shiny/ui/_theme.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import textwrap
88
from typing import Any, Literal, Optional, Sequence, TypeVar
99

10+
from brand_yml import Brand
1011
from htmltools import HTMLDependency
1112

1213
from .._docstring import add_example
@@ -460,25 +461,29 @@ def _dep_create(self, css_path: str | pathlib.Path) -> HTMLDependency:
460461
"href": css_path.name,
461462
"data-shiny-theme": self.name or self._preset, # type: ignore
462463
},
464+
# Branded themes re-use this tempdir
465+
all_files=False,
463466
)
464467

465468
def _html_dependency_precompiled(self) -> HTMLDependency:
466469
return self._dep_create(css_path=self._dep_css_precompiled_path())
467470

468-
def _html_dependency(self) -> HTMLDependency:
471+
def _html_dependency(self) -> list[HTMLDependency]:
469472
"""
470473
Create an `HTMLDependency` object from the theme.
471474
472475
Returns
473476
-------
474477
:
475-
An :class:`~htmltools.HTMLDependency` object representing the theme. In
476-
most cases, you should not need to call this method directly. Instead, pass
477-
the `Theme` object directly to the `theme` argument of any Shiny page
478-
function.
478+
An list of :class:`~htmltools.HTMLDependency` objects representing the
479+
theme. In most cases, you should not need to call this method directly.
480+
Instead, pass the `Theme` object directly to the `theme` argument of any
481+
Shiny page function.
479482
"""
483+
# Note: return a list so that this method can be overridden in subclasses of
484+
# Theme that want to attach additional dependencies to the theme dependency.
480485
if self._can_use_precompiled():
481-
return self._html_dependency_precompiled()
486+
return [self._html_dependency_precompiled()]
482487

483488
css_name = self._dep_css_name()
484489
css_path = os.path.join(self._get_css_tempdir(), css_name)
@@ -487,7 +492,7 @@ def _html_dependency(self) -> HTMLDependency:
487492
with open(css_path, "w") as css_file:
488493
css_file.write(self.to_css())
489494

490-
return self._dep_create(css_path)
495+
return [self._dep_create(css_path)]
491496

492497
def tagify(self) -> None:
493498
raise SyntaxError(
@@ -497,6 +502,12 @@ def tagify(self) -> None:
497502
"or any `shiny.ui.page_*()` function (Shiny Core)."
498503
)
499504

505+
@classmethod
506+
def from_brand(cls, brand: str | pathlib.Path | Brand):
507+
from ._theme_brand import theme_from_brand # avoid circular import
508+
509+
return theme_from_brand(brand)
510+
500511

501512
def path_pkg_preset(preset: ShinyThemePreset, *args: str) -> str:
502513
"""

0 commit comments

Comments
 (0)