gradio-pr-bot commited on
Commit
2fd83d8
·
verified ·
1 Parent(s): ff02dd3

Upload folder using huggingface_hub

Browse files
6.25.0/html/Example.svelte ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ let {
3
+ value,
4
+ type,
5
+ selected = false
6
+ }: {
7
+ value: string;
8
+ type: "gallery" | "table";
9
+ selected?: boolean;
10
+ } = $props();
11
+ </script>
12
+
13
+ <div
14
+ class:table={type === "table"}
15
+ class:gallery={type === "gallery"}
16
+ class:selected
17
+ class="prose"
18
+ >
19
+ {@html value}
20
+ </div>
21
+
22
+ <style>
23
+ .gallery {
24
+ padding: var(--size-2);
25
+ }
26
+ </style>
6.25.0/html/Index.svelte ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script module lang="ts">
2
+ export { default as BaseHTML } from "./shared/HTML.svelte";
3
+ export { default as BaseExample } from "./Example.svelte";
4
+ </script>
5
+
6
+ <script lang="ts">
7
+ import { Gradio } from "@gradio/utils";
8
+ import HTML from "./shared/HTML.svelte";
9
+ import { StatusTracker } from "@gradio/statustracker";
10
+ import { Block, BlockLabel, IconButtonWrapper } from "@gradio/atoms";
11
+ import { Code as CodeIcon } from "@gradio/icons";
12
+ import { css_units } from "@gradio/utils";
13
+ import { prepare_files } from "@gradio/client";
14
+ import type { HTMLProps, HTMLEvents } from "./types.ts";
15
+ import type { Snippet } from "svelte";
16
+
17
+ let props = $props();
18
+ let children: Snippet | undefined = props.children;
19
+ const gradio = new Gradio<HTMLEvents, HTMLProps>(props);
20
+
21
+ let _props = $derived({
22
+ value: gradio.props.value ?? "",
23
+ label: gradio.shared.label,
24
+ visible: gradio.shared.visible,
25
+ ...gradio.props.props
26
+ });
27
+
28
+ let old_value = $state(gradio.props.value);
29
+ $effect(() => {
30
+ if (JSON.stringify(old_value) !== JSON.stringify(gradio.props.value)) {
31
+ old_value = gradio.props.value;
32
+ gradio.dispatch("change");
33
+ }
34
+ });
35
+
36
+ async function upload(file: File): Promise<{ path: string; url: string }> {
37
+ try {
38
+ const file_data = await prepare_files([file]);
39
+ const result = await gradio.shared.client.upload(
40
+ file_data,
41
+ gradio.shared.root,
42
+ undefined,
43
+ gradio.shared.max_file_size ?? undefined
44
+ );
45
+ if (result && result[0]) {
46
+ return { path: result[0].path, url: result[0].url! };
47
+ }
48
+ throw new Error("Upload failed");
49
+ } catch (e) {
50
+ gradio.dispatch("error", e instanceof Error ? e.message : String(e));
51
+ throw e;
52
+ }
53
+ }
54
+ </script>
55
+
56
+ <Block
57
+ visible={gradio.shared.visible}
58
+ elem_id={gradio.shared.elem_id}
59
+ elem_classes={gradio.shared.elem_classes}
60
+ container={gradio.shared.container}
61
+ padding={gradio.shared.padding !== false}
62
+ overflow_behavior="visible"
63
+ >
64
+ {#if gradio.shared.show_label && gradio.props.buttons && gradio.props.buttons.length > 0}
65
+ <IconButtonWrapper
66
+ buttons={gradio.props.buttons}
67
+ on_custom_button_click={(id) => {
68
+ gradio.dispatch("custom_button_click", { id });
69
+ }}
70
+ />
71
+ {/if}
72
+ {#if gradio.shared.show_label}
73
+ <BlockLabel
74
+ Icon={CodeIcon}
75
+ show_label={gradio.shared.show_label}
76
+ label={gradio.shared.label}
77
+ float={true}
78
+ />
79
+ {/if}
80
+
81
+ <StatusTracker
82
+ autoscroll={gradio.shared.autoscroll}
83
+ i18n={gradio.i18n}
84
+ {...gradio.shared.loading_status}
85
+ variant="center"
86
+ on_clear_status={() =>
87
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
88
+ />
89
+ <div
90
+ class="html-container"
91
+ class:pending={gradio.shared.loading_status?.status === "pending" &&
92
+ gradio.shared.loading_status?.show_progress !== "hidden"}
93
+ style:min-height={gradio.props.min_height &&
94
+ gradio.shared.loading_status?.status !== "pending"
95
+ ? css_units(gradio.props.min_height)
96
+ : undefined}
97
+ style:max-height={gradio.props.max_height
98
+ ? css_units(gradio.props.max_height)
99
+ : undefined}
100
+ style:overflow-y={gradio.props.max_height ? "auto" : undefined}
101
+ class:label-padding={gradio.shared.show_label ?? undefined}
102
+ >
103
+ <!-- js_on_load is mount-only, so remount the renderer for a new gr.render component. -->
104
+ {#key gradio.shared.id}
105
+ <HTML
106
+ props={_props}
107
+ html_template={gradio.props.html_template}
108
+ css_template={gradio.props.css_template}
109
+ js_on_load={gradio.props.js_on_load}
110
+ elem_classes={gradio.shared.elem_classes}
111
+ visible={gradio.shared.visible === "hidden"
112
+ ? false
113
+ : gradio.shared.visible}
114
+ autoscroll={gradio.shared.autoscroll}
115
+ apply_default_css={gradio.props.apply_default_css}
116
+ head={gradio.props.head}
117
+ component_class_name={gradio.props.component_class_name}
118
+ {upload}
119
+ server={gradio.shared.server}
120
+ onevent={(detail) => {
121
+ gradio.dispatch(detail.type, detail.data);
122
+ }}
123
+ onupdate_value={(detail) => {
124
+ if (detail.property === "value") {
125
+ gradio.props.value = detail.data;
126
+ } else if (detail.property === "label") {
127
+ gradio.shared.label = detail.data;
128
+ } else if (detail.property === "visible") {
129
+ gradio.shared.visible = detail.data;
130
+ }
131
+ }}
132
+ >
133
+ {@render children?.()}
134
+ </HTML>
135
+ {/key}
136
+ </div>
137
+ </Block>
138
+
139
+ <style>
140
+ .html-container {
141
+ padding: var(--block-padding);
142
+ }
143
+
144
+ .label-padding {
145
+ padding-top: var(--spacing-xxl);
146
+ }
147
+
148
+ div {
149
+ transition: 150ms;
150
+ }
151
+
152
+ .pending {
153
+ opacity: 0.2;
154
+ }
155
+ </style>
6.25.0/html/package.json ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/html",
3
+ "version": "0.13.2",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "main_changeset": true,
10
+ "dependencies": {
11
+ "@gradio/atoms": "workspace:^",
12
+ "@gradio/client": "workspace:^",
13
+ "@gradio/icons": "workspace:^",
14
+ "@gradio/statustracker": "workspace:^",
15
+ "@gradio/utils": "workspace:^",
16
+ "handlebars": "^4.7.9"
17
+ },
18
+ "devDependencies": {
19
+ "@gradio/preview": "workspace:^"
20
+ },
21
+ "exports": {
22
+ "./package.json": "./package.json",
23
+ ".": {
24
+ "gradio": "./Index.svelte",
25
+ "svelte": "./dist/Index.svelte",
26
+ "types": "./dist/Index.svelte.d.ts"
27
+ },
28
+ "./example": {
29
+ "gradio": "./Example.svelte",
30
+ "svelte": "./dist/Example.svelte",
31
+ "types": "./dist/Example.svelte.d.ts"
32
+ },
33
+ "./base": {
34
+ "gradio": "./shared/HTML.svelte",
35
+ "svelte": "./dist/shared/HTML.svelte",
36
+ "types": "./dist/shared/HTML.svelte.d.ts"
37
+ }
38
+ },
39
+ "peerDependencies": {
40
+ "svelte": "^5.48.0"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/gradio-app/gradio.git",
45
+ "directory": "js/html"
46
+ }
47
+ }
6.25.0/html/shared/HTML.svelte ADDED
@@ -0,0 +1,575 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script module lang="ts">
2
+ // Shared across instances so a component whose head script was already
3
+ // added by another instance can await that in-flight load instead of
4
+ // running js_on_load before the script has executed.
5
+ const head_script_loads = new Map<string, Promise<void>>();
6
+ </script>
7
+
8
+ <script lang="ts">
9
+ import { onDestroy } from "svelte";
10
+ import Handlebars from "handlebars";
11
+ import type { Snippet } from "svelte";
12
+
13
+ let {
14
+ elem_classes = [],
15
+ props = {},
16
+ html_template = "${value}",
17
+ css_template = "",
18
+ js_on_load = null,
19
+ head = null,
20
+ visible = true,
21
+ autoscroll = false,
22
+ apply_default_css = true,
23
+ component_class_name = "HTML",
24
+ upload = null,
25
+ server = {},
26
+ onevent,
27
+ onupdate_value,
28
+ children
29
+ }: {
30
+ // every one of these has a default in the destructure above
31
+ elem_classes?: string[];
32
+ props?: Record<string, any>;
33
+ html_template?: string;
34
+ css_template?: string;
35
+ js_on_load?: string | null;
36
+ head?: string | null;
37
+ visible?: boolean;
38
+ autoscroll?: boolean;
39
+ apply_default_css?: boolean;
40
+ component_class_name?: string;
41
+ upload?: ((file: File) => Promise<{ path: string; url: string }>) | null;
42
+ server?: Record<string, (...args: any[]) => Promise<any>>;
43
+ onevent?: (detail: { type: "click" | "submit"; data: any }) => void;
44
+ onupdate_value?: (detail: {
45
+ data: any;
46
+ property: "value" | "label" | "visible";
47
+ }) => void;
48
+ children?: Snippet;
49
+ } = $props();
50
+
51
+ let [has_children, pre_html_template, post_html_template] = $derived.by(
52
+ () => {
53
+ if (html_template.includes("@children") && children) {
54
+ const parts = html_template.split("@children");
55
+ return [true, parts[0] || "", parts.slice(1).join("@children") || ""];
56
+ }
57
+ return [false, html_template, ""];
58
+ }
59
+ );
60
+
61
+ let old_props = $state($state.snapshot(props));
62
+ type WatchEntry = { props: string[]; callback: () => void };
63
+ let watch_entries: WatchEntry[] = [];
64
+
65
+ function watch(propOrProps: string | string[], callback: () => void): void {
66
+ const prop_list = Array.isArray(propOrProps) ? propOrProps : [propOrProps];
67
+ watch_entries.push({ props: prop_list, callback });
68
+ }
69
+
70
+ function fire_watchers(changed_keys: string[]): void {
71
+ const seen = new Set<WatchEntry>();
72
+ for (const entry of watch_entries) {
73
+ if (entry.props.some((k) => changed_keys.includes(k))) {
74
+ seen.add(entry);
75
+ }
76
+ }
77
+ for (const entry of seen) {
78
+ try {
79
+ entry.callback();
80
+ } catch (e) {
81
+ console.error("Error in watch callback:", e);
82
+ }
83
+ }
84
+ }
85
+
86
+ const trigger = (
87
+ event_type: "click" | "submit",
88
+ event_data: any = {}
89
+ ): void => {
90
+ onevent?.({ type: event_type, data: event_data });
91
+ };
92
+
93
+ let element: HTMLDivElement;
94
+ let pre_element: HTMLDivElement;
95
+ let post_element: HTMLDivElement;
96
+ let scrollable_parent: HTMLElement | null = null;
97
+ let random_id = `html-${Math.random().toString(36).substring(2, 11)}`;
98
+ let style_element: HTMLStyleElement | null = null;
99
+ let reactiveProps: Record<string, any> = {};
100
+ let currentHtml = $state("");
101
+ let currentPreHtml = $state("");
102
+ let currentPostHtml = $state("");
103
+ let currentCss = $state("");
104
+ let renderScheduled = $state(false);
105
+ let mounted = $state(false);
106
+ let html_error_message: string | null = $state(null);
107
+ let css_error_message: string | null = $state(null);
108
+
109
+ function get_scrollable_parent(element: HTMLElement): HTMLElement | null {
110
+ let parent = element.parentElement;
111
+ while (parent) {
112
+ const style = window.getComputedStyle(parent);
113
+ if (
114
+ style.overflow === "auto" ||
115
+ style.overflow === "scroll" ||
116
+ style.overflowY === "auto" ||
117
+ style.overflowY === "scroll"
118
+ ) {
119
+ return parent;
120
+ }
121
+ parent = parent.parentElement;
122
+ }
123
+ return null;
124
+ }
125
+
126
+ function is_at_bottom(): boolean {
127
+ if (!element) return true;
128
+ if (!scrollable_parent) {
129
+ return (
130
+ window.innerHeight + window.scrollY >=
131
+ document.documentElement.scrollHeight - 100
132
+ );
133
+ }
134
+ return (
135
+ scrollable_parent.offsetHeight + scrollable_parent.scrollTop >=
136
+ scrollable_parent.scrollHeight - 100
137
+ );
138
+ }
139
+
140
+ function scroll_to_bottom(): void {
141
+ if (!element) return;
142
+ if (scrollable_parent) {
143
+ scrollable_parent.scrollTo(0, scrollable_parent.scrollHeight);
144
+ } else {
145
+ window.scrollTo(0, document.documentElement.scrollHeight);
146
+ }
147
+ }
148
+
149
+ async function scroll_on_html_update(): Promise<void> {
150
+ if (!autoscroll || !element) return;
151
+ if (!scrollable_parent) {
152
+ scrollable_parent = get_scrollable_parent(element);
153
+ }
154
+ if (is_at_bottom()) {
155
+ await new Promise((resolve) => setTimeout(resolve, 300));
156
+ scroll_to_bottom();
157
+ }
158
+ }
159
+
160
+ function render_template(
161
+ template: string,
162
+ props: Record<string, any>,
163
+ language: "html" | "css" = "html"
164
+ ): string {
165
+ try {
166
+ const handlebarsTemplate = Handlebars.compile(template);
167
+ const handlebarsRendered = handlebarsTemplate(props);
168
+
169
+ const propKeys = Object.keys(props);
170
+ const propValues = Object.values(props);
171
+ const templateFunc = new Function(
172
+ ...propKeys,
173
+ `return \`${handlebarsRendered}\`;`
174
+ );
175
+ if (language === "html") {
176
+ html_error_message = null;
177
+ } else if (language === "css") {
178
+ css_error_message = null;
179
+ }
180
+ return templateFunc(...propValues);
181
+ } catch (e) {
182
+ console.error("Error evaluating template:", e);
183
+ if (language === "html") {
184
+ html_error_message = e instanceof Error ? e.message : String(e);
185
+ } else if (language === "css") {
186
+ css_error_message = e instanceof Error ? e.message : String(e);
187
+ }
188
+ return "";
189
+ }
190
+ }
191
+
192
+ function update_css(): void {
193
+ if (typeof document === "undefined") return;
194
+ if (!style_element) {
195
+ style_element = document.createElement("style");
196
+ document.head.appendChild(style_element);
197
+ }
198
+ currentCss = render_template(css_template, reactiveProps, "css");
199
+ if (currentCss) {
200
+ style_element.textContent = `#${random_id} { ${currentCss} }`;
201
+ } else {
202
+ style_element.textContent = "";
203
+ }
204
+ }
205
+
206
+ onDestroy(() => style_element?.remove());
207
+
208
+ function updateDOM(
209
+ _element: HTMLElement | undefined,
210
+ oldHtml: string,
211
+ newHtml: string
212
+ ): void {
213
+ if (!_element || oldHtml === newHtml) return;
214
+
215
+ const tempContainer = document.createElement("div");
216
+ tempContainer.innerHTML = newHtml;
217
+
218
+ const oldNodes = Array.from(_element.childNodes);
219
+ const newNodes = Array.from(tempContainer.childNodes);
220
+
221
+ const maxLength = Math.max(oldNodes.length, newNodes.length);
222
+
223
+ for (let i = 0; i < maxLength; i++) {
224
+ const oldNode = oldNodes[i];
225
+ const newNode = newNodes[i];
226
+
227
+ if (!oldNode && newNode) {
228
+ _element.appendChild(newNode.cloneNode(true));
229
+ } else if (oldNode && !newNode) {
230
+ _element.removeChild(oldNode);
231
+ } else if (oldNode && newNode) {
232
+ updateNode(oldNode, newNode);
233
+ }
234
+ }
235
+ }
236
+
237
+ function updateNode(oldNode: Node, newNode: Node): void {
238
+ if (
239
+ oldNode.nodeType === Node.TEXT_NODE &&
240
+ newNode.nodeType === Node.TEXT_NODE
241
+ ) {
242
+ if (oldNode.textContent !== newNode.textContent) {
243
+ oldNode.textContent = newNode.textContent;
244
+ }
245
+ return;
246
+ }
247
+
248
+ if (
249
+ oldNode.nodeType === Node.ELEMENT_NODE &&
250
+ newNode.nodeType === Node.ELEMENT_NODE
251
+ ) {
252
+ const oldElement = oldNode as Element;
253
+ const newElement = newNode as Element;
254
+
255
+ if (oldElement.tagName !== newElement.tagName) {
256
+ oldNode.parentNode?.replaceChild(newNode.cloneNode(true), oldNode);
257
+ return;
258
+ }
259
+
260
+ const oldAttrs = Array.from(oldElement.attributes);
261
+ const newAttrs = Array.from(newElement.attributes);
262
+
263
+ for (const attr of oldAttrs) {
264
+ if (!newElement.hasAttribute(attr.name)) {
265
+ oldElement.removeAttribute(attr.name);
266
+ }
267
+ }
268
+
269
+ for (const attr of newAttrs) {
270
+ if (oldElement.getAttribute(attr.name) !== attr.value) {
271
+ oldElement.setAttribute(attr.name, attr.value);
272
+
273
+ if (
274
+ attr.name === "value" &&
275
+ (oldElement.tagName === "INPUT" ||
276
+ oldElement.tagName === "TEXTAREA" ||
277
+ oldElement.tagName === "SELECT")
278
+ ) {
279
+ (
280
+ oldElement as
281
+ | HTMLInputElement
282
+ | HTMLTextAreaElement
283
+ | HTMLSelectElement
284
+ ).value = attr.value;
285
+ }
286
+ }
287
+ }
288
+
289
+ const oldChildren = Array.from(oldElement.childNodes);
290
+ const newChildren = Array.from(newElement.childNodes);
291
+ const maxChildren = Math.max(oldChildren.length, newChildren.length);
292
+
293
+ for (let i = 0; i < maxChildren; i++) {
294
+ const oldChild = oldChildren[i];
295
+ const newChild = newChildren[i];
296
+
297
+ if (!oldChild && newChild) {
298
+ oldElement.appendChild(newChild.cloneNode(true));
299
+ } else if (oldChild && !newChild) {
300
+ oldElement.removeChild(oldChild);
301
+ } else if (oldChild && newChild) {
302
+ updateNode(oldChild, newChild);
303
+ }
304
+ }
305
+ } else {
306
+ oldNode.parentNode?.replaceChild(newNode.cloneNode(true), oldNode);
307
+ }
308
+ }
309
+
310
+ function renderHTML(): void {
311
+ if (has_children) {
312
+ const newPreHtml = render_template(
313
+ pre_html_template,
314
+ reactiveProps,
315
+ "html"
316
+ );
317
+ updateDOM(pre_element, currentPreHtml, newPreHtml);
318
+ currentPreHtml = newPreHtml;
319
+ const newPostHtml = render_template(
320
+ post_html_template,
321
+ reactiveProps,
322
+ "html"
323
+ );
324
+ updateDOM(post_element, currentPostHtml, newPostHtml);
325
+ currentPostHtml = newPostHtml;
326
+ } else {
327
+ const newHtml = render_template(html_template, reactiveProps, "html");
328
+ updateDOM(element, currentHtml, newHtml);
329
+ currentHtml = newHtml;
330
+ }
331
+ if (autoscroll) {
332
+ scroll_on_html_update();
333
+ }
334
+ }
335
+
336
+ function scheduleRender(): void {
337
+ if (!renderScheduled) {
338
+ renderScheduled = true;
339
+ queueMicrotask(() => {
340
+ renderScheduled = false;
341
+ renderHTML();
342
+ update_css();
343
+ });
344
+ }
345
+ }
346
+
347
+ async function loadHead(headHtml: string): Promise<void> {
348
+ if (!headHtml) return;
349
+ const parser = new DOMParser();
350
+ const doc = parser.parseFromString(`<head>${headHtml}</head>`, "text/html");
351
+ const promises: Promise<void>[] = [];
352
+ for (const el of Array.from(doc.head.children)) {
353
+ if (el.tagName === "SCRIPT") {
354
+ const parsed_script = el as HTMLScriptElement;
355
+ const src = parsed_script.src;
356
+ if (src) {
357
+ const in_flight = head_script_loads.get(src);
358
+ if (in_flight) {
359
+ promises.push(in_flight);
360
+ continue;
361
+ }
362
+ // selector-free dedupe: author-provided src may contain `]` etc.
363
+ // that would break a querySelector string.
364
+ if (Array.from(document.scripts).some((s) => s.src === src)) continue;
365
+ const script = document.createElement("script");
366
+ // Created scripts default to force-async; copy the author's intent
367
+ // so a plain <script src> keeps document order.
368
+ script.async = (el as HTMLScriptElement).async;
369
+ script.src = src;
370
+ const load = new Promise<void>((resolve, reject) => {
371
+ script.onload = () => resolve();
372
+ script.onerror = () =>
373
+ reject(new Error(`Failed to load script: ${src}`));
374
+ });
375
+ head_script_loads.set(src, load);
376
+ promises.push(load);
377
+ document.head.appendChild(script);
378
+ } else {
379
+ if (
380
+ Array.from(document.scripts).some(
381
+ (s) => !s.src && s.textContent === parsed_script.textContent
382
+ )
383
+ ) {
384
+ continue;
385
+ }
386
+ const script = document.createElement("script");
387
+ script.textContent = parsed_script.textContent;
388
+ document.head.appendChild(script);
389
+ }
390
+ } else {
391
+ // selector-free dedupe, same reason as the script[src] check above.
392
+ const href = el.tagName === "LINK" ? (el as HTMLLinkElement).href : "";
393
+ const existing = href
394
+ ? Array.from(document.querySelectorAll("link")).some(
395
+ (l) => (l as HTMLLinkElement).href === href
396
+ )
397
+ : false;
398
+ if (!existing) {
399
+ document.head.appendChild(el.cloneNode(true));
400
+ }
401
+ }
402
+ }
403
+ await Promise.all(promises);
404
+ }
405
+
406
+ // Mount effect
407
+ $effect(() => {
408
+ if (!element || mounted) return;
409
+ mounted = true;
410
+
411
+ reactiveProps = new Proxy($state.snapshot(props), {
412
+ set(target, property, value) {
413
+ const oldValue = target[property as string];
414
+ target[property as string] = value;
415
+
416
+ if (oldValue !== value) {
417
+ scheduleRender();
418
+
419
+ if (
420
+ property === "value" ||
421
+ property === "label" ||
422
+ property === "visible"
423
+ ) {
424
+ props[property] = value;
425
+ old_props[property] = value;
426
+ onupdate_value?.({ data: value, property });
427
+ }
428
+ }
429
+ return true;
430
+ }
431
+ });
432
+
433
+ if (has_children) {
434
+ currentPreHtml = render_template(
435
+ pre_html_template,
436
+ reactiveProps,
437
+ "html"
438
+ );
439
+ pre_element.innerHTML = currentPreHtml;
440
+ currentPostHtml = render_template(
441
+ post_html_template,
442
+ reactiveProps,
443
+ "html"
444
+ );
445
+ post_element.innerHTML = currentPostHtml;
446
+ } else {
447
+ currentHtml = render_template(html_template, reactiveProps, "html");
448
+ element.innerHTML = currentHtml;
449
+ }
450
+ update_css();
451
+
452
+ if (autoscroll) {
453
+ scroll_to_bottom();
454
+ }
455
+ scroll_on_html_update();
456
+
457
+ (async () => {
458
+ if (head) {
459
+ await loadHead(head);
460
+ }
461
+ if (js_on_load && element) {
462
+ try {
463
+ const upload_func =
464
+ upload ??
465
+ (async (_file: File): Promise<{ path: string; url: string }> => {
466
+ throw new Error("upload is not available in this context");
467
+ });
468
+ const func = new Function(
469
+ "element",
470
+ "trigger",
471
+ "props",
472
+ "server",
473
+ "upload",
474
+ "watch",
475
+ js_on_load
476
+ );
477
+ func(element, trigger, reactiveProps, server, upload_func, watch);
478
+ } catch (error) {
479
+ console.error("Error executing js_on_load:", error);
480
+ }
481
+ }
482
+ })();
483
+ });
484
+
485
+ $effect(() => {
486
+ if (
487
+ reactiveProps &&
488
+ props &&
489
+ JSON.stringify(old_props) !== JSON.stringify(props)
490
+ ) {
491
+ const changedKeys: string[] = [];
492
+ for (const key in props) {
493
+ if (JSON.stringify(reactiveProps[key]) !== JSON.stringify(props[key])) {
494
+ changedKeys.push(key);
495
+ }
496
+ reactiveProps[key] = $state.snapshot(props[key]);
497
+ }
498
+ old_props = props;
499
+ if (changedKeys.length > 0) {
500
+ queueMicrotask(() => fire_watchers(changedKeys));
501
+ }
502
+ }
503
+ });
504
+ </script>
505
+
506
+ {#if html_error_message || css_error_message}
507
+ <div class="error-container">
508
+ <strong class="error-title"
509
+ >Error rendering <code class="error-component-name"
510
+ >{component_class_name}</code
511
+ >:</strong
512
+ >
513
+ <code class="error-message">{html_error_message || css_error_message}</code>
514
+ </div>
515
+ {:else}
516
+ <div
517
+ bind:this={element}
518
+ id={random_id}
519
+ class="{apply_default_css && !has_children
520
+ ? 'prose gradio-style'
521
+ : ''} {elem_classes.join(' ')}"
522
+ class:hide={!visible}
523
+ class:has_children
524
+ >
525
+ {#if has_children}
526
+ <div
527
+ class={apply_default_css ? "prose gradio-style" : ""}
528
+ bind:this={pre_element}
529
+ ></div>
530
+ {@render children?.()}
531
+ <div
532
+ class={apply_default_css ? "prose gradio-style" : ""}
533
+ bind:this={post_element}
534
+ ></div>
535
+ {/if}
536
+ </div>
537
+ {/if}
538
+
539
+ <style>
540
+ .hide {
541
+ display: none;
542
+ }
543
+
544
+ .has_children {
545
+ display: flex;
546
+ flex-direction: column;
547
+ gap: var(--layout-gap);
548
+ }
549
+
550
+ .error-container {
551
+ padding: 12px;
552
+ background-color: #fee;
553
+ border: 1px solid #fcc;
554
+ border-radius: 4px;
555
+ color: #c33;
556
+ font-family: monospace;
557
+ font-size: 13px;
558
+ }
559
+
560
+ .error-title {
561
+ display: block;
562
+ margin-bottom: 8px;
563
+ }
564
+
565
+ .error-component-name {
566
+ background-color: #fdd;
567
+ padding: 2px 4px;
568
+ border-radius: 2px;
569
+ }
570
+
571
+ .error-message {
572
+ white-space: pre-wrap;
573
+ word-break: break-word;
574
+ }
575
+ </style>
6.25.0/html/types.ts ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { CustomButton } from "@gradio/utils";
2
+
3
+ export interface HTMLProps {
4
+ value: string;
5
+ html_template: string;
6
+ css_template: string;
7
+ js_on_load: string | null;
8
+ apply_default_css: boolean;
9
+ show_label: boolean;
10
+ min_height: number | undefined;
11
+ max_height: number | undefined;
12
+ props: Record<string, any>;
13
+ head: string | null;
14
+ component_class_name: string;
15
+ buttons: (string | CustomButton)[] | null;
16
+ }
17
+
18
+ export interface HTMLEvents {
19
+ change: never;
20
+ click: never;
21
+ submit: never;
22
+ custom_button_click: { id: number };
23
+ error: string;
24
+ clear_status: any;
25
+ }