Introducing Gradio 5.0
Read MoreIntroducing Gradio 5.0
Read MoreNew to Gradio? Start here: Getting Started
See the Release History
theme=
kwarg to the Blocks
or Interface
constructor. For example:with gr.Blocks(theme=gr.themes.Soft()) as demo:
...
gr.themes.*
. These are:gr.themes.Base()
gr.themes.Default()
gr.themes.Glass()
gr.themes.Monochrome()
gr.themes.Soft()
import gradio as gr
gr.themes.builder()
gr.themes.builder()
.gradio.themes.Color
objects. Internally, these Color objects hold brightness values for the palette of a single hue, ranging from 50, 100, 200…, 800, 900, 950. Other CSS variables are derived from these 3 colors.primary_hue
: This is the color draws attention in your theme. In the default theme, this is set to gradio.themes.colors.orange
.secondary_hue
: This is the color that is used for secondary elements in your theme. In the default theme, this is set to gradio.themes.colors.blue
.neutral_hue
: This is the color that is used for text and other neutral elements in your theme. In the default theme, this is set to gradio.themes.colors.gray
.with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink")) as demo:
...
Color
objects directly, like this:with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.red, secondary_hue=gr.themes.colors.pink)) as demo:
...
slate
gray
zinc
neutral
stone
red
orange
amber
yellow
lime
green
emerald
teal
cyan
sky
blue
indigo
violet
purple
fuchsia
pink
rose
You could also create your own custom Color
objects and pass them in.
The next 3 constructor arguments set the sizing of the theme and are gradio.themes.Size
objects. Internally, these Size objects hold pixel size values that range from xxs
to xxl
. Other CSS variables are derived from these 3 sizes.
spacing_size
: This sets the padding within and spacing between elements. In the default theme, this is set to gradio.themes.sizes.spacing_md
.radius_size
: This sets the roundedness of corners of elements. In the default theme, this is set to gradio.themes.sizes.radius_md
.text_size
: This sets the font size of text. In the default theme, this is set to gradio.themes.sizes.text_md
.with gr.Blocks(theme=gr.themes.Default(spacing_size="sm", radius_size="none")) as demo:
...
Size
objects directly, like this:with gr.Blocks(theme=gr.themes.Default(spacing_size=gr.themes.sizes.spacing_sm, radius_size=gr.themes.sizes.radius_none)) as demo:
...
radius_none
radius_sm
radius_md
radius_lg
spacing_sm
spacing_md
spacing_lg
text_sm
text_md
text_lg
Size
objects and pass them in.gradio.themes.GoogleFont
, the font will be loaded from Google Fonts.font
: This sets the primary font of the theme. In the default theme, this is set to gradio.themes.GoogleFont("Source Sans Pro")
.font_mono
: This sets the monospace font of the theme. In the default theme, this is set to gradio.themes.GoogleFont("IBM Plex Mono")
.with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"])) as demo:
...
.set()
.set()
method of the theme object to get access to the CSS variables. For example:theme = gr.themes.Default(primary_hue="blue").set(
loader_color="#FF0000",
slider_color="#FF0000",
)
with gr.Blocks(theme=theme) as demo:
...
loader_color
and slider_color
variables to #FF0000
, despite the overall primary_color
using the blue color palette. You can set any CSS variable that is defined in the theme in this manner.button_primary_background_fill_hover_dark
! However they follow a common naming convention that makes it easy to understand what they do and to find the variable you’re looking for. Separated by underscores, the variable name is made up of:button
, slider
, or block
.button_primary
, or block_label
.button_primary_background_fill
, or block_label_border_width
.button_primary_background_fill_hover
._dark
. For example, input_border_color_focus_dark
.table_border_color
, or input_shadow
.*primary_
, *secondary_
, or *neutral_
prefix, followed by the brightness value. For example:theme = gr.themes.Default(primary_hue="blue").set(
button_primary_background_fill="*primary_200",
button_primary_background_fill_hover="*primary_300",
)
button_primary_background_fill
and button_primary_background_fill_hover
variables to *primary_200
and *primary_300
. These variables will be set to the 200 and 300 brightness values of the blue primary color palette, respectively.*spacing_
, *radius_
, or *text_
prefix, followed by the size value. For example:theme = gr.themes.Default(radius_size="md").set(
button_primary_border_radius="*radius_xl",
)
button_primary_border_radius
variable to *radius_xl
. This variable will be set to the xl
setting of the medium radius size range.theme = gr.themes.Default().set(
button_primary_background_fill="#FF0000",
button_primary_background_fill_hover="#FF0000",
button_primary_border="#FF0000",
)
button_primary_background_fill
variable in the button_primary_background_fill_hover
and button_primary_border
variables, using a *
prefix.theme = gr.themes.Default().set(
button_primary_background_fill="#FF0000",
button_primary_background_fill_hover="*button_primary_background_fill",
button_primary_border="*button_primary_background_fill",
)
button_primary_background_fill
variable, the button_primary_background_fill_hover
and button_primary_border
variables will automatically update as well.theme = gr.themes.Default().set(
button_primary_background_fill="#FF0000",
button_primary_background_fill_dark="#AAAAAA",
button_primary_border="*button_primary_background_fill",
button_primary_border_dark="*button_primary_background_fill_dark",
)
button_primary_border_dark
will draw its value from button_primary_background_fill_dark
, because dark mode always draw from the dark version of the variable.gradio.themes.Base
, a theme that sets a lot of convenient defaults. Let’s make a simple demo that creates a dummy theme called Seafoam, and make a simple app that uses it.$code_theme_new_step_1
gr.themes.Blue
as it primary color - you’ll note the primary button and the loading animation are both blue as a result. Let’s change the defaults core arguments of our app. We’ll overwrite the constructor and pass new defaults for the core constructor arguments.gr.themes.Emerald
as our primary color, and set secondary and neutral hues to gr.themes.Blue
. We’ll make our text larger using text_lg
. We’ll use Quicksand
as our default font, loaded from Google Fonts.$code_theme_new_step_2
See how the primary button and the loading animation are now green? These CSS variables are tied to the primary_hue
variable.
Let’s modify the theme a bit more directly. We’ll call the set()
method to overwrite CSS variable values explicitly. We can use any CSS logic, and reference our core constructor arguments using the *
prefix.
$code_theme_new_step_3
Look how fun our theme looks now! With just a few variable changes, our theme looks completely different.
You may find it helpful to explore the source code of the other prebuilt themes to see how they modified the base theme. You can also find your browser’s Inspector useful to select elements from the UI and see what CSS variables are being used in the styles panel.
Once you have created a theme, you can upload it to the HuggingFace Hub to let others view it, use it, and build off of it!
There are two ways to upload a theme, via the theme class instance or the command line. We will cover both of them with the previously created seafoam
theme.
Each theme instance has a method called push_to_hub
we can use to upload a theme to the HuggingFace hub.
seafoam.push_to_hub(repo_name="seafoam",
version="0.0.1",
hf_token="<token>")
First save the theme to disk
seafoam.dump(filename="seafoam.json")
Then use the upload_theme
command:
upload_theme\
"seafoam.json"\
"seafoam"\
--version "0.0.1"\
--hf_token "<token>"
In order to upload a theme, you must have a HuggingFace account and pass your Access Token
as the hf_token
argument. However, if you log in via the HuggingFace command line (which comes installed with gradio
),
you can omit the hf_token
argument.
The version
argument lets you specify a valid semantic version string for your theme.
That way your users are able to specify which version of your theme they want to use in their apps. This also lets you publish updates to your theme without worrying
about changing how previously created apps look. The version
argument is optional. If omitted, the next patch version is automatically applied.
By calling push_to_hub
or upload_theme
, the theme assets will be stored in a HuggingFace space.
The theme preview for our seafoam theme is here: seafoam preview.
The Theme Gallery shows all the public gradio themes. After publishing your theme, it will automatically show up in the theme gallery after a couple of minutes.
You can sort the themes by the number of likes on the space and from most to least recently created as well as toggling themes between light and dark mode.
To use a theme from the hub, use the from_hub
method on the ThemeClass
and pass it to your app:
my_theme = gr.Theme.from_hub("gradio/seafoam")
with gr.Blocks(theme=my_theme) as demo:
....
You can also pass the theme string directly to Blocks
or Interface
(gr.Blocks(theme="gradio/seafoam")
)
You can pin your app to an upstream theme version by using semantic versioning expressions.
For example, the following would ensure the theme we load from the seafoam
repo was between versions 0.0.1
and 0.1.0
:
with gr.Blocks(theme="gradio/seafoam@>=0.0.1,<0.1.0") as demo:
....
Enjoy creating your own themes! If you make one you’re proud of, please share it with the world by uploading it to the hub! If you tag us on Twitter we can give your theme a shout out!