Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python backend to theming #2931

Merged
merged 136 commits into from Mar 6, 2023
Merged

Python backend to theming #2931

merged 136 commits into from Mar 6, 2023

Conversation

aliabid94
Copy link
Collaborator

@aliabid94 aliabid94 commented Jan 5, 2023

Created python backend that will allow users to create and modify backends from python. User's can specify existing themes as:

with gr.Blocks(theme=gr.themes.Solid) as demo:
    pass

and they can modify themes, with full type hints, via:

theme = gr.themes.Default()
theme.color_focus_primary = "#aaaaaa"

with gr.Blocks(theme=theme) as demo:
    pass

Here's how it works:

  1. Themes are stored in gradio/theming/configs as jsons
  2. gradio/theming/_gen_themes.py converts these jsons into python classes. Each css variable is converted into an attribute of the class
  3. These python classes can be imported, modified, etc.
  4. When passed a Theme class, Blocks will generate the css from the attributes.
  5. This css file will be accessed via localhost:7860/theme.css

Currently the best way I could think of implementing, lmk what you guys think.

Example running demo/blocks_xray below:
Recording 2023-01-04 at 18 29 06

(for some reason, vscode isn't type hinting the theme attributes for me, will figure out. For now looking for feedback on general implementation_

Closes: #2354

@aliabid94
Copy link
Collaborator Author

Theming ready for review! New API:

  • Import themes from gradio.themes and set them in Blocks/Interface constructor, e.g. gr.Blocks(theme=gr.themes.Soft())
  • Themes have six constructor arguments for high-level control of look and feel.
    • Colors: primary_hue, secondary_hue, and neutral_hue. Each of these is a gr.themes.utils.Color object, which is a palette ranging from 50-900
    • Sizes: spacing_size, text_size, radius_size. Each of these is a gr.themes.utils.Size object, which a size list ranging from xxs to xxl.
    • For example, gr.themes.Default(primary_hue=gr.themes.utils.green)
  • The ~200 theme variables can be set via the set method
    • For example, gr.themes.Default(primary_hue=gr.themes.utils.green).set(block_background="blue")
    • Theme variables can refer to each other, or the six constructor arguments, with an asterisk prefix.
    • For example, gr.themes.Default(primary_hue=gr.themes.utils.green).set(block_background="*primary_500", checkbox_label_background="*block_background")
  • Dark mode variables have a _dark suffix
    • For example gr.themes.Default(primary_hue=gr.themes.utils.green).set(block_background="*primary_500", block_background_dark="*neutral_800")

Also created the blocks_kitchen_sink demo. which showcases all Gradio features, and easy controls to toggle between themes and dark mode.

themes

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aliabid94 This is awesome 👏

I like how you can set high-level theme attributes but then override the values for specific fields.

Creating themes via python is hard (I had to look at the svelte files to know what I was modifying) but that's not really related to the api and the theme builder can take care of making it easier.

Left a couple of nits. Thanks!

gradio/themes/__init__.py Show resolved Hide resolved
@@ -0,0 +1,110 @@
class Size:
def __init__(self, xxs, xs, sm, md, lg, xl, xxl):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would add type hints here and for Color.

demo/blocks_kitchen_sink/run.py Outdated Show resolved Hide resolved
gradio/components.py Show resolved Hide resolved
@@ -89,7 +89,7 @@ No changes to highlight.
No changes to highlight.

## Full Changelog:
No changes to highlight.
* Added backend support for themes by [@aliabid94](https://github.com/aliabid94) in [PR 2931](https://github.com/gradio-app/gradio/pull/2931)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the wrong place.

@abidlabs
Copy link
Member

abidlabs commented Mar 3, 2023

Looks really good @aliabid94! My one feedback (copied from Slack) is:

The theming works really well, no issues I could find. My main pain point was importing the colors and the sizes for the constructor. It would be really, really nice if you could pass in string shortcuts for all of the arguments, and they get mapped appropriately:
"blue" --> gr.themes.utils.blue
"sm" --> gr.themes.utils.size.radius_sm
and so on

Let's merge this in after we've released 3.20!

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new styles look good to me @aliabid94 ! Only thing I noticed missing is the comment about queue=False and not deleting style prop from the component. Not sure what you and @dawoodkhan82 agreed to.

If color_map is deprecated, is there a way to target the chatbot bubble colors with the python theming api? I don't think so but want to make sure.

@aliabid94
Copy link
Collaborator Author

aliabid94 commented Mar 6, 2023

I noticed missing is the comment about queue=False

Sorry, what comment?

and not deleting style prop from the component. Not sure what you and @dawoodkhan82 agreed to.

Restored the style prop to chatbot (without color_map)

If color_map is deprecated, is there a way to target the chatbot bubble colors with the python theming api? I don't think so but want to make sure.

The colors for the chatbot inherit from theme values as such:

.bot,
.pending {
  border-color: var(--color-border-primary);
  background-color: var(--color-background-secondary);
}
.user {
  border-color: var(--color-border-accent);
  background-color: var(--color-accent-soft);
}

Users can modify these theme variables, or for more control, hijack with the CSS kwarg.

@freddyaboulton
Copy link
Collaborator

@aliabid94 Thanks for responding + clarifying the new css variables for the chatbot!

That comment was meant to go to this PR #3370 lol sorry about that 🙈

This PR looks good to me though!

@aliabid94
Copy link
Collaborator Author

Thanks everyone! Finally merging

@aliabid94 aliabid94 merged commit 608d3b6 into main Mar 6, 2023
@aliabid94 aliabid94 deleted the theme_backend branch March 6, 2023 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[theming] create python API for theming (api)
5 participants