Skip to content

Commit

Permalink
Load upstream theme (#3641)
Browse files Browse the repository at this point in the history
* theme loading

* upstream theme

* version

* format themes

* fixes

* tests

* one more test

* fix test

* address review
  • Loading branch information
abidlabs authored Mar 28, 2023
1 parent a46b180 commit fec4857
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
14 changes: 8 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
- Fixed bug where text for altair plots was not legible in dark mode by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3555](https://github.com/gradio-app/gradio/pull/3555)
- Fixes `Chatbot` and `Image` components so that files passed during processing are added to a directory where they can be served from, by [@abidlabs](https://github.com/abidlabs) in [PR 3523](https://github.com/gradio-app/gradio/pull/3523)
- Use Gradio API server to send telemetry using `huggingface_hub` [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 3488](https://github.com/gradio-app/gradio/pull/3488)
- Fixes an issue where if the Blocks scope was not exited, then State could be shared across sessions, by [@abidlabs](https://github.com/abidlabs) in [PR 3600](https://github.com/gradio-app/gradio/pull/3600)
- Fixes an an issue where if the Blocks scope was not exited, then State could be shared across sessions, by [@abidlabs](https://github.com/abidlabs) in [PR 3600](https://github.com/gradio-app/gradio/pull/3600)
- Ensures that `gr.load()` loads and applies the upstream theme, by [@abidlabs](https://github.com/abidlabs) in [PR 3641](https://github.com/gradio-app/gradio/pull/3641)
- Fixed bug where "or" was not being localized in file upload text by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3599](https://github.com/gradio-app/gradio/pull/3599)
- Fixed bug where chatbot does not autoscroll inside of a tab, row or column by [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 3637](https://github.com/gradio-app/gradio/pull/3637)
- Fixed bug where textbox shrinks when `lines` set to larger than 20 by [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 3637](https://github.com/gradio-app/gradio/pull/3637)
Expand All @@ -37,11 +38,12 @@ No changes to highlight.

## Full Changelog:

* 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)
* Copy everything in website Dockerfile, fix build issues by [@aliabd](https://github.com/aliabd) in [PR 3659](https://github.com/gradio-app/gradio/pull/3659)
* Raise error when an event is queued but the queue is not configured by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3640](https://github.com/gradio-app/gradio/pull/3640)
- 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)
- Copy everything in website Dockerfile, fix build issues by [@aliabd](https://github.com/aliabd) in [PR 3659](https://github.com/gradio-app/gradio/pull/3659)
- Raise error when an event is queued but the queue is not configured by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3640](https://github.com/gradio-app/gradio/pull/3640)
- Allows users to apss in a string name for a built-in theme, by [@abidlabs](https://github.com/abidlabs) in [PR 3641](https://github.com/gradio-app/gradio/pull/3641)


## Contributors Shoutout:
Expand Down
38 changes: 24 additions & 14 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@

from gradio.components import Component

BUILT_IN_THEMES: Dict[str, Theme] = {
t.name: t
for t in [
themes.Base(),
themes.Default(),
themes.Monochrome(),
themes.Soft(),
themes.Glass(),
]
}


class Block:
def __init__(
Expand Down Expand Up @@ -495,6 +506,7 @@ def __init__(
):
"""
Parameters:
theme: a Theme object or a string representing a theme. If a string, will look for a built-in theme with that name (e.g. "soft" or "default"), or will attempt to load a theme from the HF Hub (e.g. "gradio/monochrome"). If None, will use the Default theme.
analytics_enabled: whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable or default to True.
mode: a human-friendly name for the kind of Blocks or Interface being created.
title: The tab title to display when this is opened in a browser window.
Expand All @@ -506,11 +518,14 @@ def __init__(
if theme is None:
theme = DefaultTheme()
elif isinstance(theme, str):
try:
theme = Theme.from_hub(theme)
except Exception as e:
warnings.warn(f"Cannot load {theme}. Caught Exception: {str(e)}")
theme = DefaultTheme()
if theme.lower() in BUILT_IN_THEMES:
theme = BUILT_IN_THEMES[theme.lower()]
else:
try:
theme = Theme.from_hub(theme)
except Exception as e:
warnings.warn(f"Cannot load {theme}. Caught Exception: {str(e)}")
theme = DefaultTheme()
if not isinstance(theme, Theme):
warnings.warn("Theme should be a class loaded from gradio.themes")
theme = DefaultTheme()
Expand Down Expand Up @@ -570,16 +585,9 @@ def __init__(
self.file_directories = []

if self.analytics_enabled:
built_in_themes = [
themes.Base(),
themes.Default(),
themes.Monochrome(),
themes.Soft(),
themes.Glass(),
]
is_custom_theme = not any(
self.theme.to_dict() == built_in_theme.to_dict()
for built_in_theme in built_in_themes
for built_in_theme in BUILT_IN_THEMES.values()
)
data = {
"mode": self.mode,
Expand Down Expand Up @@ -607,6 +615,7 @@ def from_config(
"""
config = copy.deepcopy(config)
components_config = config["components"]
theme = config.get("theme", "default")
original_mapping: Dict[int, Block] = {}

def get_block_instance(id: int) -> Block:
Expand Down Expand Up @@ -644,7 +653,7 @@ def iterate_over_children(children_list):

derived_fields = ["types"]

with Blocks() as blocks:
with Blocks(theme=theme) as blocks:
# ID 0 should be the root Blocks component
original_mapping[0] = Context.root_block or blocks

Expand Down Expand Up @@ -1138,6 +1147,7 @@ def get_config_file(self):
"is_colab": utils.colab_check(),
"stylesheets": self.stylesheets,
"root": self.root,
"theme": self.theme.name,
}

def getLayout(block):
Expand Down
3 changes: 3 additions & 0 deletions gradio/test_data/blocks_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"mode": "blocks",
"dev_mode": True,
"analytics_enabled": False,
"theme": "default",
"components": [
{
"id": 1,
Expand Down Expand Up @@ -260,6 +261,7 @@
"mode": "blocks",
"dev_mode": True,
"analytics_enabled": False,
"theme": "default",
"components": [
{
"id": 1,
Expand Down Expand Up @@ -516,6 +518,7 @@
"mode": "blocks",
"dev_mode": True,
"analytics_enabled": False,
"theme": "default",
"components": [
{
"id": 1,
Expand Down
2 changes: 1 addition & 1 deletion gradio/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.23.1b1
3.23.1b3
3 changes: 3 additions & 0 deletions test/test_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def test_numerical_to_label_space(self):
try:
output = io("male", 77, 10)
assert json.load(open(output))["label"] == "Perishes"
assert io.theme.name == "soft"
except TooManyRequestsError:
pass

Expand Down Expand Up @@ -284,6 +285,7 @@ def test_private_space(self):
try:
output = io("abc")
assert output == "abc"
assert io.theme.name == "gradio/monochrome"
except TooManyRequestsError:
pass

Expand Down Expand Up @@ -312,6 +314,7 @@ def test_loading_files_via_proxy_works(self):
io = gr.load(
"spaces/gradio-tests/test-loading-examples-private", api_key=api_key
)
assert io.theme.name == "default"
app, _, _ = io.launch(prevent_thread_lock=True)
test_client = TestClient(app)
r = test_client.get(
Expand Down

0 comments on commit fec4857

Please sign in to comment.