gradio-pr-bot commited on
Commit
8230363
·
verified ·
1 Parent(s): fcbb98a

Upload folder using huggingface_hub

Browse files
6.0.0-dev.4/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.0-dev.4/html/Index.svelte ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.props.show_label}
43
+ <BlockLabel
44
+ Icon={CodeIcon}
45
+ show_label={gradio.props.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
+ on:event={(e) => {
80
+ gradio.dispatch(e.detail.type, e.detail.data);
81
+ }}
82
+ on:update_value={(e) => {
83
+ if (e.detail.property === "value") {
84
+ gradio.props.value = e.detail.data;
85
+ } else if (e.detail.property === "label") {
86
+ gradio.shared.label = e.detail.data;
87
+ } else if (e.detail.property === "visible") {
88
+ gradio.shared.visible = e.detail.data;
89
+ }
90
+ }}
91
+ />
92
+ </div>
93
+ </Block>
94
+
95
+ <style>
96
+ .padding {
97
+ padding: var(--block-padding);
98
+ }
99
+
100
+ div {
101
+ transition: 150ms;
102
+ }
103
+
104
+ .pending {
105
+ opacity: 0.2;
106
+ }
107
+ </style>
6.0.0-dev.4/html/package.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/html",
3
+ "version": "0.8.0-dev.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.0-dev.4/html/shared/HTML.svelte ADDED
@@ -0,0 +1,330 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ } = $props();
15
+
16
+ let old_props = $state(props);
17
+
18
+ const dispatch = createEventDispatcher<{
19
+ event: { type: "click" | "submit"; data: any };
20
+ update_value: { data: any; property: "value" | "label" | "visible" };
21
+ }>();
22
+
23
+ const trigger = (
24
+ event_type: "click" | "submit",
25
+ event_data: any = {}
26
+ ): void => {
27
+ dispatch("event", { type: event_type, data: event_data });
28
+ };
29
+
30
+ let element: HTMLDivElement;
31
+ let scrollable_parent: HTMLElement | null = null;
32
+ let random_id = `html-${Math.random().toString(36).substring(2, 11)}`;
33
+ let style_element: HTMLStyleElement | null = null;
34
+ let reactiveProps: Record<string, any> = {};
35
+ let currentHtml = $state("");
36
+ let currentCss = $state("");
37
+ let renderScheduled = $state(false);
38
+ let mounted = $state(false);
39
+
40
+ function get_scrollable_parent(element: HTMLElement): HTMLElement | null {
41
+ let parent = element.parentElement;
42
+ while (parent) {
43
+ const style = window.getComputedStyle(parent);
44
+ if (
45
+ style.overflow === "auto" ||
46
+ style.overflow === "scroll" ||
47
+ style.overflowY === "auto" ||
48
+ style.overflowY === "scroll"
49
+ ) {
50
+ return parent;
51
+ }
52
+ parent = parent.parentElement;
53
+ }
54
+ return null;
55
+ }
56
+
57
+ function is_at_bottom(): boolean {
58
+ if (!element) return true;
59
+ if (!scrollable_parent) {
60
+ return (
61
+ window.innerHeight + window.scrollY >=
62
+ document.documentElement.scrollHeight - 100
63
+ );
64
+ }
65
+ return (
66
+ scrollable_parent.offsetHeight + scrollable_parent.scrollTop >=
67
+ scrollable_parent.scrollHeight - 100
68
+ );
69
+ }
70
+
71
+ function scroll_to_bottom(): void {
72
+ if (!element) return;
73
+ if (scrollable_parent) {
74
+ scrollable_parent.scrollTo(0, scrollable_parent.scrollHeight);
75
+ } else {
76
+ window.scrollTo(0, document.documentElement.scrollHeight);
77
+ }
78
+ }
79
+
80
+ async function scroll_on_html_update(): Promise<void> {
81
+ if (!autoscroll || !element) return;
82
+ if (!scrollable_parent) {
83
+ scrollable_parent = get_scrollable_parent(element);
84
+ }
85
+ if (is_at_bottom()) {
86
+ await new Promise((resolve) => setTimeout(resolve, 300));
87
+ scroll_to_bottom();
88
+ }
89
+ }
90
+
91
+ function render_template(
92
+ template: string,
93
+ props: Record<string, any>
94
+ ): string {
95
+ try {
96
+ const handlebarsTemplate = Handlebars.compile(template);
97
+ const handlebarsRendered = handlebarsTemplate(props);
98
+
99
+ const propKeys = Object.keys(props);
100
+ const propValues = Object.values(props);
101
+ const templateFunc = new Function(
102
+ ...propKeys,
103
+ `return \`${handlebarsRendered}\`;`
104
+ );
105
+ return templateFunc(...propValues);
106
+ } catch (e) {
107
+ console.error("Error evaluating template:", e);
108
+ return "--- Error rendering template ---";
109
+ }
110
+ }
111
+
112
+ function update_css(): void {
113
+ if (typeof document === "undefined") return;
114
+ if (!style_element) {
115
+ style_element = document.createElement("style");
116
+ document.head.appendChild(style_element);
117
+ }
118
+ currentCss = render_template(css_template, reactiveProps);
119
+ if (currentCss) {
120
+ style_element.textContent = `#${random_id} { ${currentCss} }`;
121
+ } else {
122
+ style_element.textContent = "";
123
+ }
124
+ }
125
+
126
+ function updateDOM(oldHtml: string, newHtml: string): void {
127
+ if (!element || oldHtml === newHtml) return;
128
+
129
+ const tempContainer = document.createElement("div");
130
+ tempContainer.innerHTML = newHtml;
131
+
132
+ const oldNodes = Array.from(element.childNodes);
133
+ const newNodes = Array.from(tempContainer.childNodes);
134
+
135
+ const maxLength = Math.max(oldNodes.length, newNodes.length);
136
+
137
+ for (let i = 0; i < maxLength; i++) {
138
+ const oldNode = oldNodes[i];
139
+ const newNode = newNodes[i];
140
+
141
+ if (!oldNode && newNode) {
142
+ element.appendChild(newNode.cloneNode(true));
143
+ } else if (oldNode && !newNode) {
144
+ element.removeChild(oldNode);
145
+ } else if (oldNode && newNode) {
146
+ updateNode(oldNode, newNode);
147
+ }
148
+ }
149
+ }
150
+
151
+ // eslint-disable-next-line complexity
152
+ function updateNode(oldNode: Node, newNode: Node): void {
153
+ if (
154
+ oldNode.nodeType === Node.TEXT_NODE &&
155
+ newNode.nodeType === Node.TEXT_NODE
156
+ ) {
157
+ if (oldNode.textContent !== newNode.textContent) {
158
+ oldNode.textContent = newNode.textContent;
159
+ }
160
+ return;
161
+ }
162
+
163
+ if (
164
+ oldNode.nodeType === Node.ELEMENT_NODE &&
165
+ newNode.nodeType === Node.ELEMENT_NODE
166
+ ) {
167
+ const oldElement = oldNode as Element;
168
+ const newElement = newNode as Element;
169
+
170
+ if (oldElement.tagName !== newElement.tagName) {
171
+ oldNode.parentNode?.replaceChild(newNode.cloneNode(true), oldNode);
172
+ return;
173
+ }
174
+
175
+ const oldAttrs = Array.from(oldElement.attributes);
176
+ const newAttrs = Array.from(newElement.attributes);
177
+
178
+ for (const attr of oldAttrs) {
179
+ if (!newElement.hasAttribute(attr.name)) {
180
+ oldElement.removeAttribute(attr.name);
181
+ }
182
+ }
183
+
184
+ for (const attr of newAttrs) {
185
+ if (oldElement.getAttribute(attr.name) !== attr.value) {
186
+ oldElement.setAttribute(attr.name, attr.value);
187
+
188
+ if (
189
+ attr.name === "value" &&
190
+ (oldElement.tagName === "INPUT" ||
191
+ oldElement.tagName === "TEXTAREA" ||
192
+ oldElement.tagName === "SELECT")
193
+ ) {
194
+ (
195
+ oldElement as
196
+ | HTMLInputElement
197
+ | HTMLTextAreaElement
198
+ | HTMLSelectElement
199
+ ).value = attr.value;
200
+ }
201
+ }
202
+ }
203
+
204
+ const oldChildren = Array.from(oldElement.childNodes);
205
+ const newChildren = Array.from(newElement.childNodes);
206
+ const maxChildren = Math.max(oldChildren.length, newChildren.length);
207
+
208
+ for (let i = 0; i < maxChildren; i++) {
209
+ const oldChild = oldChildren[i];
210
+ const newChild = newChildren[i];
211
+
212
+ if (!oldChild && newChild) {
213
+ oldElement.appendChild(newChild.cloneNode(true));
214
+ } else if (oldChild && !newChild) {
215
+ oldElement.removeChild(oldChild);
216
+ } else if (oldChild && newChild) {
217
+ updateNode(oldChild, newChild);
218
+ }
219
+ }
220
+ } else {
221
+ oldNode.parentNode?.replaceChild(newNode.cloneNode(true), oldNode);
222
+ }
223
+ }
224
+
225
+ function renderHTML(): void {
226
+ console.trace(
227
+ "re-rendering HTML with props:",
228
+ JSON.stringify(reactiveProps)
229
+ );
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
+ <div
318
+ bind:this={element}
319
+ id={random_id}
320
+ class="{apply_default_css ? 'prose gradio-style' : ''} {elem_classes.join(
321
+ ' '
322
+ )}"
323
+ class:hide={!visible}
324
+ ></div>
325
+
326
+ <style>
327
+ .hide {
328
+ display: none;
329
+ }
330
+ </style>
6.0.0-dev.4/html/types.ts ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }
12
+
13
+ export interface HTMLEvents {
14
+ change: never;
15
+ click: never;
16
+ submit: never;
17
+ }