gradio-pr-bot commited on
Commit
bee3ea5
·
verified ·
1 Parent(s): d4bc84a

Upload folder using huggingface_hub

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