jbilcke-hf HF Staff commited on
Commit
19fb8a2
·
1 Parent(s): 39c2fc0
docs/gradio/using-custom-css-and-js.md ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 1. Building With Blocks
2
+ 2. Custom CSS And JS
3
+
4
+ [
5
+
6
+
7
+
8
+ More Blocks Features
9
+
10
+
11
+
12
+ ](../guides/more-blocks-features/)[
13
+
14
+ Using Blocks Like Functions
15
+
16
+
17
+
18
+ ](../guides/using-blocks-like-functions/)
19
+
20
+ Customizing your demo with CSS and Javascript
21
+ =============================================
22
+
23
+ Gradio allows you to customize your demo in several ways. You can customize the layout of your demo, add custom HTML, and add custom theming as well. This tutorial will go beyond that and walk you through how to add custom CSS and JavaScript code to your demo in order to add custom styling, animations, custom UI functionality, analytics, and more.
24
+
25
+ Adding custom CSS to your demo[![](data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20fill='%23808080'%20viewBox='0%200%20640%20512'%3e%3c!--!%20Font%20Awesome%20Pro%206.0.0%20by%20@fontawesome%20-%20https://fontawesome.com%20License%20-%20https://fontawesome.com/license%20(Commercial%20License)%20Copyright%202022%20Fonticons,%20Inc.%20--%3e%3cpath%20d='M172.5%20131.1C228.1%2075.51%20320.5%2075.51%20376.1%20131.1C426.1%20181.1%20433.5%20260.8%20392.4%20318.3L391.3%20319.9C381%20334.2%20361%20337.6%20346.7%20327.3C332.3%20317%20328.9%20297%20339.2%20282.7L340.3%20281.1C363.2%20249%20359.6%20205.1%20331.7%20177.2C300.3%20145.8%20249.2%20145.8%20217.7%20177.2L105.5%20289.5C73.99%20320.1%2073.99%20372%20105.5%20403.5C133.3%20431.4%20177.3%20435%20209.3%20412.1L210.9%20410.1C225.3%20400.7%20245.3%20404%20255.5%20418.4C265.8%20432.8%20262.5%20452.8%20248.1%20463.1L246.5%20464.2C188.1%20505.3%20110.2%20498.7%2060.21%20448.8C3.741%20392.3%203.741%20300.7%2060.21%20244.3L172.5%20131.1zM467.5%20380C411%20436.5%20319.5%20436.5%20263%20380C213%20330%20206.5%20251.2%20247.6%20193.7L248.7%20192.1C258.1%20177.8%20278.1%20174.4%20293.3%20184.7C307.7%20194.1%20311.1%20214.1%20300.8%20229.3L299.7%20230.9C276.8%20262.1%20280.4%20306.9%20308.3%20334.8C339.7%20366.2%20390.8%20366.2%20422.3%20334.8L534.5%20222.5C566%20191%20566%20139.1%20534.5%20108.5C506.7%2080.63%20462.7%2076.99%20430.7%2099.9L429.1%20101C414.7%20111.3%20394.7%20107.1%20384.5%2093.58C374.2%2079.2%20377.5%2059.21%20391.9%2048.94L393.5%2047.82C451%206.731%20529.8%2013.25%20579.8%2063.24C636.3%20119.7%20636.3%20211.3%20579.8%20267.7L467.5%20380z'/%3e%3c/svg%3e)](#adding-custom-css-to-your-demo)
26
+ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
27
+
28
+ Gradio themes are the easiest way to customize the look and feel of your app. You can choose from a variety of themes, or create your own. To do so, pass the `theme=` kwarg to the `Blocks` constructor. For example:
29
+
30
+ with gr.Blocks(theme=gr.themes.Glass()):
31
+ ...
32
+
33
+ Gradio comes with a set of prebuilt themes which you can load from `gr.themes.*`. You can extend these themes or create your own themes from scratch - see the [Theming guide](/guides/theming-guide) for more details.
34
+
35
+ For additional styling ability, you can pass any CSS to your app as a string using the `css=` kwarg. You can also pass a pathlib.Path to a css file or a list of such paths to the `css_paths=` kwarg.
36
+
37
+ **Warning**: The use of query selectors in custom JS and CSS is _not_ guaranteed to work across Gradio versions that bind to Gradio's own HTML elements as the Gradio HTML DOM may change. We recommend using query selectors sparingly.
38
+
39
+ The base class for the Gradio app is `gradio-container`, so here's an example that changes the background color of the Gradio app:
40
+
41
+ with gr.Blocks(css=".gradio-container {background-color: red}") as demo:
42
+ ...
43
+
44
+ If you'd like to reference external files in your css, preface the file path (which can be a relative or absolute path) with `"/gradio_api/file="`, for example:
45
+
46
+ with gr.Blocks(css=".gradio-container {background: url('/gradio_api/file=clouds.jpg')}") as demo:
47
+ ...
48
+
49
+ Note: By default, most files in the host machine are not accessible to users running the Gradio app. As a result, you should make sure that any referenced files (such as `clouds.jpg` here) are either URLs or [allowed paths, as described here](/main/guides/file-access).
50
+
51
+ The `elem_id` and `elem_classes` Arguments[![](data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20fill='%23808080'%20viewBox='0%200%20640%20512'%3e%3c!--!%20Font%20Awesome%20Pro%206.0.0%20by%20@fontawesome%20-%20https://fontawesome.com%20License%20-%20https://fontawesome.com/license%20(Commercial%20License)%20Copyright%202022%20Fonticons,%20Inc.%20--%3e%3cpath%20d='M172.5%20131.1C228.1%2075.51%20320.5%2075.51%20376.1%20131.1C426.1%20181.1%20433.5%20260.8%20392.4%20318.3L391.3%20319.9C381%20334.2%20361%20337.6%20346.7%20327.3C332.3%20317%20328.9%20297%20339.2%20282.7L340.3%20281.1C363.2%20249%20359.6%20205.1%20331.7%20177.2C300.3%20145.8%20249.2%20145.8%20217.7%20177.2L105.5%20289.5C73.99%20320.1%2073.99%20372%20105.5%20403.5C133.3%20431.4%20177.3%20435%20209.3%20412.1L210.9%20410.1C225.3%20400.7%20245.3%20404%20255.5%20418.4C265.8%20432.8%20262.5%20452.8%20248.1%20463.1L246.5%20464.2C188.1%20505.3%20110.2%20498.7%2060.21%20448.8C3.741%20392.3%203.741%20300.7%2060.21%20244.3L172.5%20131.1zM467.5%20380C411%20436.5%20319.5%20436.5%20263%20380C213%20330%20206.5%20251.2%20247.6%20193.7L248.7%20192.1C258.1%20177.8%20278.1%20174.4%20293.3%20184.7C307.7%20194.1%20311.1%20214.1%20300.8%20229.3L299.7%20230.9C276.8%20262.1%20280.4%20306.9%20308.3%20334.8C339.7%20366.2%20390.8%20366.2%20422.3%20334.8L534.5%20222.5C566%20191%20566%20139.1%20534.5%20108.5C506.7%2080.63%20462.7%2076.99%20430.7%2099.9L429.1%20101C414.7%20111.3%20394.7%20107.1%20384.5%2093.58C374.2%2079.2%20377.5%2059.21%20391.9%2048.94L393.5%2047.82C451%206.731%20529.8%2013.25%20579.8%2063.24C636.3%20119.7%20636.3%20211.3%20579.8%20267.7L467.5%20380z'/%3e%3c/svg%3e)](#the-elem-id-and-elem-classes-arguments)
52
+ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
53
+
54
+ You can `elem_id` to add an HTML element `id` to any component, and `elem_classes` to add a class or list of classes. This will allow you to select elements more easily with CSS. This approach is also more likely to be stable across Gradio versions as built-in class names or ids may change (however, as mentioned in the warning above, we cannot guarantee complete compatibility between Gradio versions if you use custom CSS as the DOM elements may themselves change).
55
+
56
+ css = """
57
+ #warning {background-color: #FFCCCB}
58
+ .feedback textarea {font-size: 24px !important}
59
+ """
60
+
61
+ with gr.Blocks(css=css) as demo:
62
+ box1 = gr.Textbox(value="Good Job", elem_classes="feedback")
63
+ box2 = gr.Textbox(value="Failure", elem_id="warning", elem_classes="feedback")
64
+
65
+ The CSS `#warning` ruleset will only target the second Textbox, while the `.feedback` ruleset will target both. Note that when targeting classes, you might need to put the `!important` selector to override the default Gradio styles.
66
+
67
+ Adding custom JavaScript to your demo[![](data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20fill='%23808080'%20viewBox='0%200%20640%20512'%3e%3c!--!%20Font%20Awesome%20Pro%206.0.0%20by%20@fontawesome%20-%20https://fontawesome.com%20License%20-%20https://fontawesome.com/license%20(Commercial%20License)%20Copyright%202022%20Fonticons,%20Inc.%20--%3e%3cpath%20d='M172.5%20131.1C228.1%2075.51%20320.5%2075.51%20376.1%20131.1C426.1%20181.1%20433.5%20260.8%20392.4%20318.3L391.3%20319.9C381%20334.2%20361%20337.6%20346.7%20327.3C332.3%20317%20328.9%20297%20339.2%20282.7L340.3%20281.1C363.2%20249%20359.6%20205.1%20331.7%20177.2C300.3%20145.8%20249.2%20145.8%20217.7%20177.2L105.5%20289.5C73.99%20320.1%2073.99%20372%20105.5%20403.5C133.3%20431.4%20177.3%20435%20209.3%20412.1L210.9%20410.1C225.3%20400.7%20245.3%20404%20255.5%20418.4C265.8%20432.8%20262.5%20452.8%20248.1%20463.1L246.5%20464.2C188.1%20505.3%20110.2%20498.7%2060.21%20448.8C3.741%20392.3%203.741%20300.7%2060.21%20244.3L172.5%20131.1zM467.5%20380C411%20436.5%20319.5%20436.5%20263%20380C213%20330%20206.5%20251.2%20247.6%20193.7L248.7%20192.1C258.1%20177.8%20278.1%20174.4%20293.3%20184.7C307.7%20194.1%20311.1%20214.1%20300.8%20229.3L299.7%20230.9C276.8%20262.1%20280.4%20306.9%20308.3%20334.8C339.7%20366.2%20390.8%20366.2%20422.3%20334.8L534.5%20222.5C566%20191%20566%20139.1%20534.5%20108.5C506.7%2080.63%20462.7%2076.99%20430.7%2099.9L429.1%20101C414.7%20111.3%20394.7%20107.1%20384.5%2093.58C374.2%2079.2%20377.5%2059.21%20391.9%2048.94L393.5%2047.82C451%206.731%20529.8%2013.25%20579.8%2063.24C636.3%20119.7%20636.3%20211.3%20579.8%20267.7L467.5%20380z'/%3e%3c/svg%3e)](#adding-custom-java-script-to-your-demo)
68
+ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
69
+
70
+ There are 3 ways to add javascript code to your Gradio demo:
71
+
72
+ 1. You can add JavaScript code as a string to the `js` parameter of the `Blocks` or `Interface` initializer. This will run the JavaScript code when the demo is first loaded.
73
+
74
+ Below is an example of adding custom js to show an animated welcome message when the demo first loads.
75
+
76
+ import gradio as gr
77
+
78
+ def welcome(name):
79
+ return f"Welcome to Gradio, {name}!"
80
+
81
+ js = """
82
+ function createGradioAnimation() {
83
+ var container = document.createElement('div');
84
+ container.id = 'gradio-animation';
85
+ container.style.fontSize = '2em';
86
+ container.style.fontWeight = 'bold';
87
+ container.style.textAlign = 'center';
88
+ container.style.marginBottom = '20px';
89
+
90
+ var text = 'Welcome to Gradio!';
91
+ for (var i = 0; i < text.length; i++) {
92
+ (function(i){
93
+ setTimeout(function(){
94
+ var letter = document.createElement('span');
95
+ letter.style.opacity = '0';
96
+ letter.style.transition = 'opacity 0.5s';
97
+ letter.innerText = text[i];
98
+
99
+ container.appendChild(letter);
100
+
101
+ setTimeout(function() {
102
+ letter.style.opacity = '1';
103
+ }, 50);
104
+ }, i * 250);
105
+ })(i);
106
+ }
107
+
108
+ var gradioContainer = document.querySelector('.gradio-container');
109
+ gradioContainer.insertBefore(container, gradioContainer.firstChild);
110
+
111
+ return 'Animation created';
112
+ }
113
+ """
114
+ with gr.Blocks(js=js) as demo:
115
+ inp = gr.Textbox(placeholder="What is your name?")
116
+ out = gr.Textbox()
117
+ inp.change(welcome, inp, out)
118
+
119
+ demo.launch()
120
+
121
+
122
+ 2. When using `Blocks` and event listeners, events have a `js` argument that can take a JavaScript function as a string and treat it just like a Python event listener function. You can pass both a JavaScript function and a Python function (in which case the JavaScript function is run first) or only Javascript (and set the Python `fn` to `None`). Take a look at the code below:
123
+
124
+ import gradio as gr
125
+
126
+ blocks = gr.Blocks()
127
+
128
+ with blocks as demo:
129
+ subject = gr.Textbox(placeholder="subject")
130
+ verb = gr.Radio(["ate", "loved", "hated"])
131
+ object = gr.Textbox(placeholder="object")
132
+
133
+ with gr.Row():
134
+ btn = gr.Button("Create sentence.")
135
+ reverse_btn = gr.Button("Reverse sentence.")
136
+ foo_bar_btn = gr.Button("Append foo")
137
+ reverse_then_to_the_server_btn = gr.Button(
138
+ "Reverse sentence and send to server."
139
+ )
140
+
141
+ def sentence_maker(w1, w2, w3):
142
+ return f"{w1} {w2} {w3}"
143
+
144
+ output1 = gr.Textbox(label="output 1")
145
+ output2 = gr.Textbox(label="verb")
146
+ output3 = gr.Textbox(label="verb reversed")
147
+ output4 = gr.Textbox(label="front end process and then send to backend")
148
+
149
+ btn.click(sentence_maker, [subject, verb, object], output1)
150
+ reverse_btn.click(
151
+ None, [subject, verb, object], output2, js="(s, v, o) => o + ' ' + v + ' ' + s"
152
+ )
153
+ verb.change(None, verb, output3, js="(x) => [...x].reverse().join('')")
154
+ foo_bar_btn.click(None, [], subject, js="(x) => x + ' foo'")
155
+
156
+ reverse_then_to_the_server_btn.click(
157
+ None,
158
+ [subject, verb, object],
159
+ output4,
160
+ js="(s, v, o) => [s, v, o].map(x => [...x].reverse().join('')).join(' ')",
161
+ )
162
+
163
+ demo.launch()
164
+
165
+
166
+ 3. Lastly, you can add JavaScript code to the `head` param of the `Blocks` initializer. This will add the code to the head of the HTML document. For example, you can add Google Analytics to your demo like so:
167
+
168
+ head = f"""
169
+ <script async src="https://www.googletagmanager.com/gtag/js?id={google_analytics_tracking_id}"></script>
170
+ <script>
171
+ window.dataLayer = window.dataLayer || [];
172
+ function gtag(){{dataLayer.push(arguments);}}
173
+ gtag('js', new Date());
174
+ gtag('config', '{google_analytics_tracking_id}');
175
+ </script>
176
+ """
177
+
178
+ with gr.Blocks(head=head) as demo:
179
+ gr.HTML("<h1>My App</h1>")
180
+
181
+ demo.launch()
182
+
183
+ The `head` parameter accepts any HTML tags you would normally insert into the `<head>` of a page. For example, you can also include `<meta>` tags to `head` in order to update the social sharing preview for your Gradio app like this:
184
+
185
+ import gradio as gr
186
+
187
+ custom_head = """
188
+ <!-- HTML Meta Tags -->
189
+ <title>Sample App</title>
190
+ <meta name="description" content="An open-source web application showcasing various features and capabilities.">
191
+
192
+ <!-- Facebook Meta Tags -->
193
+ <meta property="og:url" content="https://example.com">
194
+ <meta property="og:type" content="website">
195
+ <meta property="og:title" content="Sample App">
196
+ <meta property="og:description" content="An open-source web application showcasing various features and capabilities.">
197
+ <meta property="og:image" content="https://cdn.britannica.com/98/152298-050-8E45510A/Cheetah.jpg">
198
+
199
+ <!-- Twitter Meta Tags -->
200
+ <meta name="twitter:card" content="summary_large_image">
201
+ <meta name="twitter:creator" content="@example_user">
202
+ <meta name="twitter:title" content="Sample App">
203
+ <meta name="twitter:description" content="An open-source web application showcasing various features and capabilities.">
204
+ <meta name="twitter:image" content="https://cdn.britannica.com/98/152298-050-8E45510A/Cheetah.jpg">
205
+ <meta property="twitter:domain" content="example.com">
206
+ <meta property="twitter:url" content="https://example.com">
207
+ """
208
+
209
+ with gr.Blocks(title="My App", head=custom_head) as demo:
210
+ gr.HTML("<h1>My App</h1>")
211
+
212
+ demo.launch()
213
+
214
+ Note that injecting custom JS can affect browser behavior and accessibility (e.g. keyboard shortcuts may be lead to unexpected behavior if your Gradio app is embedded in another webpage). You should test your interface across different browsers and be mindful of how scripts may interact with browser defaults. Here's an example where pressing `Shift + s` triggers the `click` event of a specific `Button` component if the browser focus is _not_ on an input component (e.g. `Textbox` component):
215
+
216
+ import gradio as gr
217
+
218
+ shortcut_js = """
219
+ <script>
220
+ function shortcuts(e) {
221
+ var event = document.all ? window.event : e;
222
+ switch (e.target.tagName.toLowerCase()) {
223
+ case "input":
224
+ case "textarea":
225
+ break;
226
+ default:
227
+ if (e.key.toLowerCase() == "s" && e.shiftKey) {
228
+ document.getElementById("my_btn").click();
229
+ }
230
+ }
231
+ }
232
+ document.addEventListener('keypress', shortcuts, false);
233
+ </script>
234
+ """
235
+
236
+ with gr.Blocks(head=shortcut_js) as demo:
237
+ action_button = gr.Button(value="Name", elem_id="my_btn")
238
+ textbox = gr.Textbox()
239
+ action_button.click(lambda : "button pressed", None, textbox)
240
+
241
+ demo.launch()
242
+
243
+ [
244
+
245
+
246
+
247
+ More Blocks Features
248
+
249
+
250
+
251
+ ](../guides/more-blocks-features/)[
252
+
253
+ Using Blocks Like Functions
254
+
255
+
256
+
257
+ ](../guides/using-blocks-like-functions/)