gradio-pr-bot commited on
Commit
199b399
·
verified ·
1 Parent(s): cbe3a8b

Upload folder using huggingface_hub

Browse files
6.1.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.1.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.1.1/html/package.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/html",
3
+ "version": "0.8.1",
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.1.1/html/shared/HTML.svelte ADDED
@@ -0,0 +1,367 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ const newHtml = render_template(html_template, reactiveProps);
231
+ if (element) {
232
+ updateDOM(currentHtml, newHtml);
233
+ }
234
+ currentHtml = newHtml;
235
+ if (autoscroll) {
236
+ scroll_on_html_update();
237
+ }
238
+ }
239
+
240
+ function scheduleRender(): void {
241
+ if (!renderScheduled) {
242
+ renderScheduled = true;
243
+ queueMicrotask(() => {
244
+ renderScheduled = false;
245
+ renderHTML();
246
+ update_css();
247
+ });
248
+ }
249
+ }
250
+
251
+ // Mount effect
252
+ $effect(() => {
253
+ if (!element || mounted) return;
254
+ mounted = true;
255
+
256
+ reactiveProps = new Proxy(
257
+ { ...props },
258
+ {
259
+ set(target, property, value) {
260
+ const oldValue = target[property as string];
261
+ target[property as string] = value;
262
+
263
+ if (oldValue !== value) {
264
+ scheduleRender();
265
+
266
+ if (
267
+ property === "value" ||
268
+ property === "label" ||
269
+ property === "visible"
270
+ ) {
271
+ props[property] = value;
272
+ old_props[property] = value;
273
+ dispatch("update_value", { data: value, property });
274
+ }
275
+ }
276
+ return true;
277
+ }
278
+ }
279
+ );
280
+
281
+ currentHtml = render_template(html_template, reactiveProps);
282
+ element.innerHTML = currentHtml;
283
+ update_css();
284
+
285
+ if (autoscroll) {
286
+ scroll_to_bottom();
287
+ }
288
+ scroll_on_html_update();
289
+
290
+ if (js_on_load && element) {
291
+ try {
292
+ const func = new Function("element", "trigger", "props", js_on_load);
293
+ func(element, trigger, reactiveProps);
294
+ } catch (error) {
295
+ console.error("Error executing js_on_load:", error);
296
+ }
297
+ }
298
+ });
299
+
300
+ // Props update effect
301
+ $effect(() => {
302
+ if (
303
+ reactiveProps &&
304
+ props &&
305
+ JSON.stringify(old_props) !== JSON.stringify(props)
306
+ ) {
307
+ for (const key in props) {
308
+ if (reactiveProps[key] !== props[key]) {
309
+ reactiveProps[key] = props[key];
310
+ }
311
+ }
312
+ old_props = props;
313
+ }
314
+ });
315
+ </script>
316
+
317
+ {#if error_message}
318
+ <div class="error-container">
319
+ <strong class="error-title"
320
+ >Error rendering <code class="error-component-name"
321
+ >{component_class_name}</code
322
+ >:</strong
323
+ >
324
+ <code class="error-message">{error_message}</code>
325
+ </div>
326
+ {:else}
327
+ <div
328
+ bind:this={element}
329
+ id={random_id}
330
+ class="{apply_default_css ? 'prose gradio-style' : ''} {elem_classes.join(
331
+ ' '
332
+ )}"
333
+ class:hide={!visible}
334
+ ></div>
335
+ {/if}
336
+
337
+ <style>
338
+ .hide {
339
+ display: none;
340
+ }
341
+
342
+ .error-container {
343
+ padding: 12px;
344
+ background-color: #fee;
345
+ border: 1px solid #fcc;
346
+ border-radius: 4px;
347
+ color: #c33;
348
+ font-family: monospace;
349
+ font-size: 13px;
350
+ }
351
+
352
+ .error-title {
353
+ display: block;
354
+ margin-bottom: 8px;
355
+ }
356
+
357
+ .error-component-name {
358
+ background-color: #fdd;
359
+ padding: 2px 4px;
360
+ border-radius: 2px;
361
+ }
362
+
363
+ .error-message {
364
+ white-space: pre-wrap;
365
+ word-break: break-word;
366
+ }
367
+ </style>
6.1.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
+ }