Spaces:
Running on Zero
Running on Zero
fix(theme): preserve gradio's --font / --font-mono css vars
Browse filesThe _OnyxAmberBase override redirected self.font into font_str, which
broke _get_theme_css emission of --font / --font-mono. Expose iterable
access via .font_list / .font_mono_list instead, and add a regression
assertion on the generated CSS.
- tests/test_theme.py +14 -3
- theme.py +11 -18
tests/test_theme.py
CHANGED
|
@@ -28,8 +28,19 @@ def test_css_string_contains_critical_selectors():
|
|
| 28 |
|
| 29 |
def test_fonts_geist_and_geist_mono():
|
| 30 |
th = theme.build_theme()
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
assert any("Geist" in f for f in fonts)
|
| 34 |
-
monos = [str(f) for f in th.
|
| 35 |
assert any("Geist Mono" in f for f in monos)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def test_fonts_geist_and_geist_mono():
|
| 30 |
th = theme.build_theme()
|
| 31 |
+
|
| 32 |
+
# (a) Iterable lists: .font_list / .font_mono_list expose the original entries.
|
| 33 |
+
fonts = [str(f) for f in th.font_list]
|
| 34 |
assert any("Geist" in f for f in fonts)
|
| 35 |
+
monos = [str(f) for f in th.font_mono_list]
|
| 36 |
assert any("Geist Mono" in f for f in monos)
|
| 37 |
+
|
| 38 |
+
# (b) CSS variables: _get_theme_css() must emit --font and --font-mono that
|
| 39 |
+
# reference Geist / Geist Mono so the browser actually loads the fonts.
|
| 40 |
+
# This assertion is what catches the original bug where property setters
|
| 41 |
+
# redirected self.font → font_str, causing --font-str to be emitted instead.
|
| 42 |
+
css = th._get_theme_css()
|
| 43 |
+
assert "--font:" in css, "--font CSS variable missing from generated theme CSS"
|
| 44 |
+
assert "--font-mono:" in css, "--font-mono CSS variable missing from generated theme CSS"
|
| 45 |
+
assert "Geist" in css, "Geist font name missing from generated theme CSS"
|
| 46 |
+
assert "Geist Mono" in css, "Geist Mono font name missing from generated theme CSS"
|
theme.py
CHANGED
|
@@ -20,31 +20,24 @@ AMBER: dict[str, str] = {
|
|
| 20 |
|
| 21 |
|
| 22 |
class _OnyxAmberBase(gr.themes.Base):
|
| 23 |
-
"""gr.themes.Base subclass
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
The CSS strings are preserved on self.font_str / self.font_mono_str.
|
| 30 |
"""
|
| 31 |
|
| 32 |
-
@property
|
| 33 |
-
def
|
|
|
|
| 34 |
return self._font
|
| 35 |
|
| 36 |
-
@
|
| 37 |
-
def
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
@property # type: ignore[override]
|
| 41 |
-
def font_mono(self) -> list: # type: ignore[override]
|
| 42 |
return self._font_mono
|
| 43 |
|
| 44 |
-
@font_mono.setter
|
| 45 |
-
def font_mono(self, val: str) -> None:
|
| 46 |
-
self.font_mono_str = val
|
| 47 |
-
|
| 48 |
|
| 49 |
def build_theme() -> gr.themes.Base:
|
| 50 |
"""Return a Gradio theme matching the Onyx Amber palette."""
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
class _OnyxAmberBase(gr.themes.Base):
|
| 23 |
+
"""gr.themes.Base subclass with helpers for iterating over font lists.
|
| 24 |
|
| 25 |
+
Gradio collapses font lists into a CSS string at __init__; we expose the
|
| 26 |
+
original lists via .font_list / .font_mono_list for code that needs to
|
| 27 |
+
iterate. The public self.font / self.font_mono attributes are left alone so
|
| 28 |
+
that _get_theme_css() emits the correct --font / --font-mono CSS variables.
|
|
|
|
| 29 |
"""
|
| 30 |
|
| 31 |
+
@property
|
| 32 |
+
def font_list(self) -> list:
|
| 33 |
+
"""Return the original font list (GoogleFont + str entries)."""
|
| 34 |
return self._font
|
| 35 |
|
| 36 |
+
@property
|
| 37 |
+
def font_mono_list(self) -> list:
|
| 38 |
+
"""Return the original monospace font list (GoogleFont + str entries)."""
|
|
|
|
|
|
|
|
|
|
| 39 |
return self._font_mono
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def build_theme() -> gr.themes.Base:
|
| 43 |
"""Return a Gradio theme matching the Onyx Amber palette."""
|