Set theme name from load (#3595)

* Add name + test

* Add theme names

* CHANGELOG

* Delete theme in interface
This commit is contained in:
Freddy Boulton 2023-03-23 17:27:16 -04:00 committed by GitHub
parent 7856382de6
commit 926f3e21bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 37 additions and 7 deletions

View File

@ -27,7 +27,7 @@ No changes to highlight.
* Mobile responsive iframes in themes guide by [@aliabd](https://github.com/aliabd) in [PR 3562](https://github.com/gradio-app/gradio/pull/3562)
* Remove extra $demo from theme guide by [@aliabd](https://github.com/aliabd) in [PR 3563](https://github.com/gradio-app/gradio/pull/3563)
* Set the theme name to be the upstream repo name when loading from the hub by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3595](https://github.com/gradio-app/gradio/pull/3595)
## Contributors Shoutout:

View File

@ -505,7 +505,7 @@ class Blocks(BlockContext):
if not isinstance(theme, Theme):
warnings.warn("Theme should be a class loaded from gradio.themes")
theme = DefaultTheme()
self.theme = theme
self.theme: Theme = theme
self.theme_css = theme._get_theme_css()
self.stylesheets = theme._stylesheets
self.encrypt = False

View File

@ -308,7 +308,6 @@ class Interface(Blocks):
self.article = article
self.thumbnail = thumbnail
self.theme = theme
self.examples = examples
self.num_shap = num_shap

View File

@ -28,6 +28,7 @@ set_documentation_group("themes")
class ThemeClass:
def __init__(self):
self._stylesheets = []
self.name = None
def _get_theme_css(self):
css = {}
@ -101,6 +102,7 @@ class ThemeClass:
not prop.startswith("_")
or prop.startswith("_font")
or prop == "_stylesheets"
or prop == "name"
) and isinstance(getattr(self, prop), (list, str)):
schema["theme"][prop] = getattr(self, prop)
return schema
@ -172,7 +174,9 @@ class ThemeClass:
repo_type="space",
filename=f"themes/theme_schema@{matching_version.version}.json",
)
return cls.load(theme_file)
theme = cls.load(theme_file)
theme.name = name
return theme
@staticmethod
def _get_next_version(space_info: huggingface_hub.hf_api.SpaceInfo) -> str:
@ -357,6 +361,8 @@ class Base(ThemeClass):
font_mono: The monospace font to use for the theme, applies to code. Pass a string for a system font, or a gradio.themes.font.GoogleFont object to load a font from Google Fonts. Pass a list of fonts for fallbacks.
"""
self.name = "base"
def expand_shortcut(shortcut, mode="color", prefix=None):
if not isinstance(shortcut, str):
return shortcut

View File

@ -43,6 +43,7 @@ class Default(Base):
font=font,
font_mono=font_mono,
)
self.name = "default"
super().set(
# Colors
input_background_fill_dark="*neutral_800",

View File

@ -44,6 +44,7 @@ class Glass(Base):
font=font,
font_mono=font_mono,
)
self.name = "glass"
super().set(
body_background_fill_dark="*primary_800",
background_fill_secondary_dark="*primary_800",

View File

@ -43,6 +43,7 @@ class Monochrome(Base):
font=font,
font_mono=font_mono,
)
self.name = "monochrome"
super().set(
# Colors
slider_color="*neutral_900",

View File

@ -43,6 +43,7 @@ class Soft(Base):
font=font,
font_mono=font_mono,
)
self.name = "soft"
super().set(
# Colors
background_fill_primary="*neutral_50",

View File

@ -2598,11 +2598,15 @@ class TestCode:
assert code.preprocess("# hello friends") == "# hello friends"
assert code.preprocess("def fn(a):\n return a") == "def fn(a):\n return a"
assert code.postprocess(
"""
assert (
code.postprocess(
"""
def fn(a):
return a
""") == "def fn(a):\n return a"
"""
)
== "def fn(a):\n return a"
)
test_file_dir = Path(Path(__file__).parent, "test_files")
path = str(Path(test_file_dir, "test_label_json.json"))

View File

@ -169,6 +169,7 @@ dracula = gr.themes.Base(
panel_background_fill="#31395294",
block_background_fill_dark="#31395294",
)
dracula.name = "gradio/dracula_test"
class TestSemverMatch:
@ -259,6 +260,21 @@ class TestGetThemeAssets:
get_theme_assets(space_info)
class TestBuiltInThemes:
@pytest.mark.parametrize(
"theme, name",
[
(gr.themes.Base(), "base"),
(gr.themes.Glass(), "glass"),
(gr.themes.Monochrome(), "monochrome"),
(gr.themes.Soft(), "soft"),
(gr.themes.Default(), "default"),
],
)
def test_theme_name(self, theme, name):
assert theme.name == name
class TestThemeUploadDownload:
@patch("gradio.themes.base.get_theme_assets", return_value=assets)
def test_get_next_version(self, mock):
@ -279,6 +295,7 @@ class TestThemeUploadDownload:
pass
assert demo.theme.to_dict() == dracula.to_dict()
assert demo.theme.name == "gradio/dracula_test"
def test_theme_download_raises_error_if_theme_does_not_exist(self):