gradio-pr-bot commited on
Commit
a84b925
·
verified ·
1 Parent(s): ba49148

Upload folder using huggingface_hub

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