gradio-pr-bot commited on
Commit
9d4bed9
·
verified ·
1 Parent(s): 9756299

Upload folder using huggingface_hub

Browse files
6.0.1/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.0.1/html/Index.svelte ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 } from "@gradio/atoms";
11
+ import { Code as CodeIcon } from "@gradio/icons";
12
+ import { css_units } from "@gradio/utils";
13
+ import type { HTMLProps, HTMLEvents } from "./types.ts";
14
+
15
+ let props = $props();
16
+ const gradio = new Gradio<HTMLEvents, HTMLProps>(props);
17
+
18
+ let _props = $derived({
19
+ value: gradio.props.value || "",
20
+ label: gradio.shared.label,
21
+ visible: gradio.shared.visible,
22
+ ...gradio.props.props
23
+ });
24
+
25
+ let old_value = $state(gradio.props.value);
26
+ $effect(() => {
27
+ if (JSON.stringify(old_value) !== JSON.stringify(gradio.props.value)) {
28
+ old_value = gradio.props.value;
29
+ gradio.dispatch("change");
30
+ }
31
+ });
32
+ </script>
33
+
34
+ <Block
35
+ visible={gradio.shared.visible}
36
+ elem_id={gradio.shared.elem_id}
37
+ elem_classes={gradio.shared.elem_classes}
38
+ container={gradio.shared.container}
39
+ padding={true}
40
+ overflow_behavior="visible"
41
+ >
42
+ {#if gradio.shared.show_label}
43
+ <BlockLabel
44
+ Icon={CodeIcon}
45
+ show_label={gradio.shared.show_label}
46
+ label={gradio.shared.label}
47
+ float={false}
48
+ />
49
+ {/if}
50
+
51
+ <StatusTracker
52
+ autoscroll={gradio.shared.autoscroll}
53
+ i18n={gradio.i18n}
54
+ {...gradio.shared.loading_status}
55
+ variant="center"
56
+ on_clear_status={() => gradio.dispatch("clear_status", loading_status)}
57
+ />
58
+ <div
59
+ class="html-container"
60
+ class:padding={gradio.shared.padding}
61
+ class:pending={gradio.shared.loading_status?.status === "pending"}
62
+ style:min-height={gradio.props.min_height &&
63
+ gradio.shared.loading_status?.status !== "pending"
64
+ ? css_units(gradio.props.min_height)
65
+ : undefined}
66
+ style:max-height={gradio.props.max_height
67
+ ? css_units(gradio.props.max_height)
68
+ : undefined}
69
+ >
70
+ <HTML
71
+ props={_props}
72
+ html_template={gradio.props.html_template}
73
+ css_template={gradio.props.css_template}
74
+ js_on_load={gradio.props.js_on_load}
75
+ elem_classes={gradio.shared.elem_classes}
76
+ visible={gradio.shared.visible}
77
+ autoscroll={gradio.shared.autoscroll}
78
+ apply_default_css={gradio.props.apply_default_css}
79
+ component_class_name={gradio.props.component_class_name}
80
+ on:event={(e) => {
81
+ gradio.dispatch(e.detail.type, e.detail.data);
82
+ }}
83
+ on:update_value={(e) => {
84
+ if (e.detail.property === "value") {
85
+ gradio.props.value = e.detail.data;
86
+ } else if (e.detail.property === "label") {
87
+ gradio.shared.label = e.detail.data;
88
+ } else if (e.detail.property === "visible") {
89
+ gradio.shared.visible = e.detail.data;
90
+ }
91
+ }}
92
+ />
93
+ </div>
94
+ </Block>
95
+
96
+ <style>
97
+ .padding {
98
+ padding: var(--block-padding);
99
+ }
100
+
101
+ div {
102
+ transition: 150ms;
103
+ }
104
+
105
+ .pending {
106
+ opacity: 0.2;
107
+ }
108
+ </style>
6.0.1/html/package.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/html",
3
+ "version": "0.8.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/icons": "workspace:^",
13
+ "@gradio/statustracker": "workspace:^",
14
+ "@gradio/utils": "workspace:^",
15
+ "handlebars": "^4.7.8"
16
+ },
17
+ "devDependencies": {
18
+ "@gradio/preview": "workspace:^"
19
+ },
20
+ "exports": {
21
+ "./package.json": "./package.json",
22
+ ".": {
23
+ "gradio": "./Index.svelte",
24
+ "svelte": "./dist/Index.svelte",
25
+ "types": "./dist/Index.svelte.d.ts"
26
+ },
27
+ "./example": {
28
+ "gradio": "./Example.svelte",
29
+ "svelte": "./dist/Example.svelte",
30
+ "types": "./dist/Example.svelte.d.ts"
31
+ },
32
+ "./base": {
33
+ "gradio": "./Index.svelte",
34
+ "svelte": "./dist/Index.svelte",
35
+ "types": "./dist/Index.svelte.d.ts"
36
+ }
37
+ },
38
+ "peerDependencies": {
39
+ "svelte": "^5.43.4"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/gradio-app/gradio.git",
44
+ "directory": "js/html"
45
+ }
46
+ }
6.0.1/html/shared/HTML.svelte ADDED
@@ -0,0 +1,371 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher, tick } from "svelte";
3
+ import Handlebars from "handlebars";
4
+
5
+ let {
6
+ elem_classes = [],
7
+ props = {},
8
+ html_template = "${value}",
9
+ css_template = "",
10
+ js_on_load = null,
11
+ visible = true,
12
+ autoscroll = false,
13
+ apply_default_css = true,
14
+ component_class_name = "HTML"
15
+ } = $props();
16
+
17
+ let old_props = $state(props);
18
+
19
+ const dispatch = createEventDispatcher<{
20
+ event: { type: "click" | "submit"; data: any };
21
+ update_value: { data: any; property: "value" | "label" | "visible" };
22
+ }>();
23
+
24
+ const trigger = (
25
+ event_type: "click" | "submit",
26
+ event_data: any = {}
27
+ ): void => {
28
+ dispatch("event", { type: event_type, data: event_data });
29
+ };
30
+
31
+ let element: HTMLDivElement;
32
+ let scrollable_parent: HTMLElement | null = null;
33
+ let random_id = `html-${Math.random().toString(36).substring(2, 11)}`;
34
+ let style_element: HTMLStyleElement | null = null;
35
+ let reactiveProps: Record<string, any> = {};
36
+ let currentHtml = $state("");
37
+ let currentCss = $state("");
38
+ let renderScheduled = $state(false);
39
+ let mounted = $state(false);
40
+ let error_message: string | null = $state(null);
41
+
42
+ function get_scrollable_parent(element: HTMLElement): HTMLElement | null {
43
+ let parent = element.parentElement;
44
+ while (parent) {
45
+ const style = window.getComputedStyle(parent);
46
+ if (
47
+ style.overflow === "auto" ||
48
+ style.overflow === "scroll" ||
49
+ style.overflowY === "auto" ||
50
+ style.overflowY === "scroll"
51
+ ) {
52
+ return parent;
53
+ }
54
+ parent = parent.parentElement;
55
+ }
56
+ return null;
57
+ }
58
+
59
+ function is_at_bottom(): boolean {
60
+ if (!element) return true;
61
+ if (!scrollable_parent) {
62
+ return (
63
+ window.innerHeight + window.scrollY >=
64
+ document.documentElement.scrollHeight - 100
65
+ );
66
+ }
67
+ return (
68
+ scrollable_parent.offsetHeight + scrollable_parent.scrollTop >=
69
+ scrollable_parent.scrollHeight - 100
70
+ );
71
+ }
72
+
73
+ function scroll_to_bottom(): void {
74
+ if (!element) return;
75
+ if (scrollable_parent) {
76
+ scrollable_parent.scrollTo(0, scrollable_parent.scrollHeight);
77
+ } else {
78
+ window.scrollTo(0, document.documentElement.scrollHeight);
79
+ }
80
+ }
81
+
82
+ async function scroll_on_html_update(): Promise<void> {
83
+ if (!autoscroll || !element) return;
84
+ if (!scrollable_parent) {
85
+ scrollable_parent = get_scrollable_parent(element);
86
+ }
87
+ if (is_at_bottom()) {
88
+ await new Promise((resolve) => setTimeout(resolve, 300));
89
+ scroll_to_bottom();
90
+ }
91
+ }
92
+
93
+ function render_template(
94
+ template: string,
95
+ props: Record<string, any>
96
+ ): string {
97
+ try {
98
+ const handlebarsTemplate = Handlebars.compile(template);
99
+ const handlebarsRendered = handlebarsTemplate(props);
100
+
101
+ const propKeys = Object.keys(props);
102
+ const propValues = Object.values(props);
103
+ const templateFunc = new Function(
104
+ ...propKeys,
105
+ `return \`${handlebarsRendered}\`;`
106
+ );
107
+ error_message = null;
108
+ return templateFunc(...propValues);
109
+ } catch (e) {
110
+ console.error("Error evaluating template:", e);
111
+ error_message = e instanceof Error ? e.message : String(e);
112
+ return "";
113
+ }
114
+ }
115
+
116
+ function update_css(): void {
117
+ if (typeof document === "undefined") return;
118
+ if (!style_element) {
119
+ style_element = document.createElement("style");
120
+ document.head.appendChild(style_element);
121
+ }
122
+ currentCss = render_template(css_template, reactiveProps);
123
+ if (currentCss) {
124
+ style_element.textContent = `#${random_id} { ${currentCss} }`;
125
+ } else {
126
+ style_element.textContent = "";
127
+ }
128
+ }
129
+
130
+ function updateDOM(oldHtml: string, newHtml: string): void {
131
+ if (!element || oldHtml === newHtml) return;
132
+
133
+ const tempContainer = document.createElement("div");
134
+ tempContainer.innerHTML = newHtml;
135
+
136
+ const oldNodes = Array.from(element.childNodes);
137
+ const newNodes = Array.from(tempContainer.childNodes);
138
+
139
+ const maxLength = Math.max(oldNodes.length, newNodes.length);
140
+
141
+ for (let i = 0; i < maxLength; i++) {
142
+ const oldNode = oldNodes[i];
143
+ const newNode = newNodes[i];
144
+
145
+ if (!oldNode && newNode) {
146
+ element.appendChild(newNode.cloneNode(true));
147
+ } else if (oldNode && !newNode) {
148
+ element.removeChild(oldNode);
149
+ } else if (oldNode && newNode) {
150
+ updateNode(oldNode, newNode);
151
+ }
152
+ }
153
+ }
154
+
155
+ // eslint-disable-next-line complexity
156
+ function updateNode(oldNode: Node, newNode: Node): void {
157
+ if (
158
+ oldNode.nodeType === Node.TEXT_NODE &&
159
+ newNode.nodeType === Node.TEXT_NODE
160
+ ) {
161
+ if (oldNode.textContent !== newNode.textContent) {
162
+ oldNode.textContent = newNode.textContent;
163
+ }
164
+ return;
165
+ }
166
+
167
+ if (
168
+ oldNode.nodeType === Node.ELEMENT_NODE &&
169
+ newNode.nodeType === Node.ELEMENT_NODE
170
+ ) {
171
+ const oldElement = oldNode as Element;
172
+ const newElement = newNode as Element;
173
+
174
+ if (oldElement.tagName !== newElement.tagName) {
175
+ oldNode.parentNode?.replaceChild(newNode.cloneNode(true), oldNode);
176
+ return;
177
+ }
178
+
179
+ const oldAttrs = Array.from(oldElement.attributes);
180
+ const newAttrs = Array.from(newElement.attributes);
181
+
182
+ for (const attr of oldAttrs) {
183
+ if (!newElement.hasAttribute(attr.name)) {
184
+ oldElement.removeAttribute(attr.name);
185
+ }
186
+ }
187
+
188
+ for (const attr of newAttrs) {
189
+ if (oldElement.getAttribute(attr.name) !== attr.value) {
190
+ oldElement.setAttribute(attr.name, attr.value);
191
+
192
+ if (
193
+ attr.name === "value" &&
194
+ (oldElement.tagName === "INPUT" ||
195
+ oldElement.tagName === "TEXTAREA" ||
196
+ oldElement.tagName === "SELECT")
197
+ ) {
198
+ (
199
+ oldElement as
200
+ | HTMLInputElement
201
+ | HTMLTextAreaElement
202
+ | HTMLSelectElement
203
+ ).value = attr.value;
204
+ }
205
+ }
206
+ }
207
+
208
+ const oldChildren = Array.from(oldElement.childNodes);
209
+ const newChildren = Array.from(newElement.childNodes);
210
+ const maxChildren = Math.max(oldChildren.length, newChildren.length);
211
+
212
+ for (let i = 0; i < maxChildren; i++) {
213
+ const oldChild = oldChildren[i];
214
+ const newChild = newChildren[i];
215
+
216
+ if (!oldChild && newChild) {
217
+ oldElement.appendChild(newChild.cloneNode(true));
218
+ } else if (oldChild && !newChild) {
219
+ oldElement.removeChild(oldChild);
220
+ } else if (oldChild && newChild) {
221
+ updateNode(oldChild, newChild);
222
+ }
223
+ }
224
+ } else {
225
+ oldNode.parentNode?.replaceChild(newNode.cloneNode(true), oldNode);
226
+ }
227
+ }
228
+
229
+ function renderHTML(): void {
230
+ console.trace(
231
+ "re-rendering HTML with props:",
232
+ JSON.stringify(reactiveProps)
233
+ );
234
+ const newHtml = render_template(html_template, reactiveProps);
235
+ if (element) {
236
+ updateDOM(currentHtml, newHtml);
237
+ }
238
+ currentHtml = newHtml;
239
+ if (autoscroll) {
240
+ scroll_on_html_update();
241
+ }
242
+ }
243
+
244
+ function scheduleRender(): void {
245
+ if (!renderScheduled) {
246
+ renderScheduled = true;
247
+ queueMicrotask(() => {
248
+ renderScheduled = false;
249
+ renderHTML();
250
+ update_css();
251
+ });
252
+ }
253
+ }
254
+
255
+ // Mount effect
256
+ $effect(() => {
257
+ if (!element || mounted) return;
258
+ mounted = true;
259
+
260
+ reactiveProps = new Proxy(
261
+ { ...props },
262
+ {
263
+ set(target, property, value) {
264
+ const oldValue = target[property as string];
265
+ target[property as string] = value;
266
+
267
+ if (oldValue !== value) {
268
+ scheduleRender();
269
+
270
+ if (
271
+ property === "value" ||
272
+ property === "label" ||
273
+ property === "visible"
274
+ ) {
275
+ props[property] = value;
276
+ old_props[property] = value;
277
+ dispatch("update_value", { data: value, property });
278
+ }
279
+ }
280
+ return true;
281
+ }
282
+ }
283
+ );
284
+
285
+ currentHtml = render_template(html_template, reactiveProps);
286
+ element.innerHTML = currentHtml;
287
+ update_css();
288
+
289
+ if (autoscroll) {
290
+ scroll_to_bottom();
291
+ }
292
+ scroll_on_html_update();
293
+
294
+ if (js_on_load && element) {
295
+ try {
296
+ const func = new Function("element", "trigger", "props", js_on_load);
297
+ func(element, trigger, reactiveProps);
298
+ } catch (error) {
299
+ console.error("Error executing js_on_load:", error);
300
+ }
301
+ }
302
+ });
303
+
304
+ // Props update effect
305
+ $effect(() => {
306
+ if (
307
+ reactiveProps &&
308
+ props &&
309
+ JSON.stringify(old_props) !== JSON.stringify(props)
310
+ ) {
311
+ for (const key in props) {
312
+ if (reactiveProps[key] !== props[key]) {
313
+ reactiveProps[key] = props[key];
314
+ }
315
+ }
316
+ old_props = props;
317
+ }
318
+ });
319
+ </script>
320
+
321
+ {#if error_message}
322
+ <div class="error-container">
323
+ <strong class="error-title"
324
+ >Error rendering <code class="error-component-name"
325
+ >{component_class_name}</code
326
+ >:</strong
327
+ >
328
+ <code class="error-message">{error_message}</code>
329
+ </div>
330
+ {:else}
331
+ <div
332
+ bind:this={element}
333
+ id={random_id}
334
+ class="{apply_default_css ? 'prose gradio-style' : ''} {elem_classes.join(
335
+ ' '
336
+ )}"
337
+ class:hide={!visible}
338
+ ></div>
339
+ {/if}
340
+
341
+ <style>
342
+ .hide {
343
+ display: none;
344
+ }
345
+
346
+ .error-container {
347
+ padding: 12px;
348
+ background-color: #fee;
349
+ border: 1px solid #fcc;
350
+ border-radius: 4px;
351
+ color: #c33;
352
+ font-family: monospace;
353
+ font-size: 13px;
354
+ }
355
+
356
+ .error-title {
357
+ display: block;
358
+ margin-bottom: 8px;
359
+ }
360
+
361
+ .error-component-name {
362
+ background-color: #fdd;
363
+ padding: 2px 4px;
364
+ border-radius: 2px;
365
+ }
366
+
367
+ .error-message {
368
+ white-space: pre-wrap;
369
+ word-break: break-word;
370
+ }
371
+ </style>
6.0.1/html/types.ts ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export interface HTMLProps {
2
+ value: string;
3
+ html_template: string;
4
+ css_template: string;
5
+ js_on_load: string | null;
6
+ apply_default_css: boolean;
7
+ show_label: boolean;
8
+ min_height: number | undefined;
9
+ max_height: number | undefined;
10
+ props: Record<string, any>;
11
+ component_class_name: string;
12
+ }
13
+
14
+ export interface HTMLEvents {
15
+ change: never;
16
+ click: never;
17
+ submit: never;
18
+ }