gradio-pr-bot commited on
Commit
8086cd2
·
verified ·
1 Parent(s): e5f8f74

Upload folder using huggingface_hub

Browse files
6.4.0/json/Example.svelte ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import JSON from "./shared/JSON.svelte";
3
+
4
+ let {
5
+ value,
6
+ theme_mode = "system" as "system" | "light" | "dark",
7
+ type,
8
+ selected = false
9
+ }: {
10
+ value: any;
11
+ theme_mode?: "system" | "light" | "dark";
12
+ type: "gallery" | "table";
13
+ selected?: boolean;
14
+ } = $props();
15
+
16
+ let show_indices = $state(false);
17
+ let label_height = $state(0);
18
+ </script>
19
+
20
+ <div
21
+ class="container"
22
+ class:table={type === "table"}
23
+ class:gallery={type === "gallery"}
24
+ class:selected
25
+ class:border={value}
26
+ >
27
+ {#if value}
28
+ <JSON
29
+ {value}
30
+ open={true}
31
+ {theme_mode}
32
+ {show_indices}
33
+ {label_height}
34
+ interactive={false}
35
+ show_copy_button={false}
36
+ />
37
+ {/if}
38
+ </div>
39
+
40
+ <style>
41
+ .container :global(img) {
42
+ width: 100%;
43
+ height: 100%;
44
+ }
45
+
46
+ .container.selected {
47
+ border-color: var(--border-color-accent);
48
+ }
49
+ .border.table {
50
+ border: 1px solid var(--border-color-primary);
51
+ }
52
+
53
+ .container.table {
54
+ margin: 0 auto;
55
+ border-radius: var(--radius-lg);
56
+ overflow: hidden;
57
+ width: 100%;
58
+ height: 100%;
59
+ max-width: var(--size-40);
60
+ max-height: var(--size-20);
61
+ object-fit: cover;
62
+ }
63
+
64
+ .container.gallery {
65
+ width: 100%;
66
+ max-width: 100%;
67
+ object-fit: cover;
68
+ max-width: var(--size-40);
69
+ max-height: var(--size-20);
70
+ overflow: hidden;
71
+ }
72
+ </style>
6.4.0/json/Index.svelte ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="module" lang="ts">
2
+ export { default as BaseJSON } from "./shared/JSON.svelte";
3
+ </script>
4
+
5
+ <script lang="ts">
6
+ import { Gradio } from "@gradio/utils";
7
+ import JSON from "./shared/JSON.svelte";
8
+ import { Block, BlockLabel } from "@gradio/atoms";
9
+ import { JSON as JSONIcon } from "@gradio/icons";
10
+
11
+ import { StatusTracker } from "@gradio/statustracker";
12
+ import type { JSONProps, JSONEvents } from "./types";
13
+
14
+ const props = $props();
15
+ const gradio = new Gradio<JSONEvents, JSONProps>(props);
16
+
17
+ let old_value = $state(gradio.props.value);
18
+
19
+ $effect(() => {
20
+ if (old_value !== gradio.props.value) {
21
+ old_value = gradio.props.value;
22
+ gradio.dispatch("change");
23
+ }
24
+ });
25
+
26
+ let label_height = $state(0);
27
+ </script>
28
+
29
+ <Block
30
+ visible={gradio.shared.visible}
31
+ test_id="json"
32
+ elem_id={gradio.shared.elem_id}
33
+ elem_classes={gradio.shared.elem_classes}
34
+ container={gradio.shared.container}
35
+ scale={gradio.shared.scale}
36
+ min_width={gradio.shared.min_width}
37
+ padding={false}
38
+ allow_overflow={true}
39
+ overflow_behavior="auto"
40
+ height={gradio.props.height}
41
+ min_height={gradio.props.min_height}
42
+ max_height={gradio.props.max_height}
43
+ >
44
+ <div bind:clientHeight={label_height}>
45
+ {#if gradio.shared.label}
46
+ <BlockLabel
47
+ Icon={JSONIcon}
48
+ show_label={gradio.shared.show_label}
49
+ label={gradio.shared.label}
50
+ float={false}
51
+ disable={gradio.shared.container === false}
52
+ />
53
+ {/if}
54
+ </div>
55
+
56
+ <StatusTracker
57
+ autoscroll={gradio.shared.autoscroll}
58
+ i18n={gradio.i18n}
59
+ {...gradio.shared.loading_status}
60
+ on_clear_status={() =>
61
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
62
+ />
63
+
64
+ <JSON
65
+ value={gradio.props.value}
66
+ open={gradio.props.open}
67
+ theme_mode={gradio.props.theme_mode}
68
+ show_indices={gradio.props.show_indices}
69
+ show_copy_button={gradio.props.buttons == null
70
+ ? true
71
+ : gradio.props.buttons.some(
72
+ (btn) => typeof btn === "string" && btn === "copy"
73
+ )}
74
+ buttons={gradio.props.buttons}
75
+ on_custom_button_click={(id) => {
76
+ gradio.dispatch("custom_button_click", { id });
77
+ }}
78
+ {label_height}
79
+ />
80
+ </Block>
6.4.0/json/package.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/json",
3
+ "version": "0.7.0",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@gradio/atoms": "workspace:^",
11
+ "@gradio/icons": "workspace:^",
12
+ "@gradio/statustracker": "workspace:^",
13
+ "@gradio/utils": "workspace:^"
14
+ },
15
+ "devDependencies": {
16
+ "@gradio/preview": "workspace:^"
17
+ },
18
+ "main": "./Index.svelte",
19
+ "main_changeset": true,
20
+ "exports": {
21
+ ".": {
22
+ "gradio": "./Index.svelte",
23
+ "svelte": "./dist/Index.svelte",
24
+ "types": "./dist/Index.svelte.d.ts"
25
+ },
26
+ "./example": {
27
+ "gradio": "./Example.svelte",
28
+ "svelte": "./dist/Example.svelte",
29
+ "types": "./dist/Example.svelte.d.ts"
30
+ },
31
+ "./package.json": "./package.json"
32
+ },
33
+ "peerDependencies": {
34
+ "svelte": "^5.43.4"
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/gradio-app/gradio.git",
39
+ "directory": "js/json"
40
+ }
41
+ }
6.4.0/json/shared/JSON.svelte ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { JSON as JSONIcon } from "@gradio/icons";
3
+ import { Empty, IconButtonWrapper, IconButton } from "@gradio/atoms";
4
+ import JSONNode from "./JSONNode.svelte";
5
+ import { Copy, Check } from "@gradio/icons";
6
+ import type { CustomButton as CustomButtonType } from "@gradio/utils";
7
+
8
+ let {
9
+ value = {},
10
+ open = false,
11
+ theme_mode = "system" as "system" | "light" | "dark",
12
+ show_indices = false,
13
+ label_height,
14
+ interactive = true,
15
+ show_copy_button = true,
16
+ buttons = null,
17
+ on_custom_button_click = null
18
+ }: {
19
+ value?: any;
20
+ open?: boolean;
21
+ theme_mode?: "system" | "light" | "dark";
22
+ show_indices?: boolean;
23
+ label_height: number;
24
+ interactive?: boolean;
25
+ show_copy_button?: boolean;
26
+ buttons?: (string | CustomButtonType)[] | null;
27
+ on_custom_button_click?: ((id: number) => void) | null;
28
+ } = $props();
29
+
30
+ let json_max_height = $derived(`calc(100% - ${label_height}px)`);
31
+
32
+ let copied = $state(false);
33
+ let timer = $state<NodeJS.Timeout>();
34
+
35
+ function copy_feedback(): void {
36
+ copied = true;
37
+ if (timer) clearTimeout(timer);
38
+ timer = setTimeout(() => {
39
+ copied = false;
40
+ }, 1000);
41
+ }
42
+
43
+ async function handle_copy(): Promise<void> {
44
+ if ("clipboard" in navigator) {
45
+ await navigator.clipboard.writeText(JSON.stringify(value, null, 2));
46
+ copy_feedback();
47
+ }
48
+ }
49
+
50
+ function is_empty(obj: object): boolean {
51
+ return (
52
+ obj &&
53
+ Object.keys(obj).length === 0 &&
54
+ Object.getPrototypeOf(obj) === Object.prototype &&
55
+ JSON.stringify(obj) === JSON.stringify({})
56
+ );
57
+ }
58
+
59
+ $effect(() => {
60
+ return () => {
61
+ if (timer) clearTimeout(timer);
62
+ };
63
+ });
64
+ </script>
65
+
66
+ {#if value && value !== '""' && !is_empty(value)}
67
+ {#if show_copy_button || (buttons && buttons.some((btn) => typeof btn !== "string"))}
68
+ <IconButtonWrapper {buttons} {on_custom_button_click}>
69
+ {#if show_copy_button}
70
+ <IconButton
71
+ show_label={false}
72
+ label={copied ? "Copied" : "Copy"}
73
+ Icon={copied ? Check : Copy}
74
+ on:click={() => handle_copy()}
75
+ />
76
+ {/if}
77
+ </IconButtonWrapper>
78
+ {/if}
79
+ <div class="json-holder" style:max-height={json_max_height}>
80
+ <JSONNode
81
+ {value}
82
+ depth={0}
83
+ is_root={true}
84
+ {open}
85
+ {theme_mode}
86
+ {show_indices}
87
+ {interactive}
88
+ />
89
+ </div>
90
+ {:else}
91
+ <div class="empty-wrapper">
92
+ <Empty>
93
+ <JSONIcon />
94
+ </Empty>
95
+ </div>
96
+ {/if}
97
+
98
+ <style>
99
+ :global(.copied svg) {
100
+ animation: fade ease 300ms;
101
+ animation-fill-mode: forwards;
102
+ }
103
+
104
+ @keyframes fade {
105
+ 0% {
106
+ opacity: 0;
107
+ }
108
+ 100% {
109
+ opacity: 1;
110
+ }
111
+ }
112
+
113
+ .json-holder {
114
+ padding: var(--size-2);
115
+ overflow-y: auto;
116
+ }
117
+
118
+ .empty-wrapper {
119
+ min-height: calc(var(--size-32) - 20px);
120
+ height: 100%;
121
+ }
122
+ </style>
6.4.0/json/shared/JSONNode.svelte ADDED
@@ -0,0 +1,278 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { tick } from "svelte";
3
+
4
+ let {
5
+ value,
6
+ depth = 0,
7
+ is_root = false,
8
+ is_last_item = true,
9
+ key = null,
10
+ open = false,
11
+ theme_mode = "system" as "system" | "light" | "dark",
12
+ show_indices = false,
13
+ interactive = true
14
+ }: {
15
+ value: any;
16
+ depth?: number;
17
+ is_root?: boolean;
18
+ is_last_item?: boolean;
19
+ key?: string | number | null;
20
+ open?: boolean;
21
+ theme_mode?: "system" | "light" | "dark";
22
+ show_indices?: boolean;
23
+ interactive?: boolean;
24
+ } = $props();
25
+
26
+ let root_element = $state<HTMLElement>();
27
+ let collapsed = $state(open ? false : depth >= 3);
28
+ let child_nodes = $state<any[]>([]);
29
+
30
+ function is_collapsible(val: any): boolean {
31
+ return val !== null && (typeof val === "object" || Array.isArray(val));
32
+ }
33
+
34
+ async function toggle_collapse(): Promise<void> {
35
+ collapsed = !collapsed;
36
+ await tick();
37
+ }
38
+
39
+ function get_collapsed_preview(val: any): string {
40
+ if (Array.isArray(val)) return `Array(${val.length})`;
41
+ if (typeof val === "object" && val !== null)
42
+ return `Object(${Object.keys(val).length})`;
43
+ return String(val);
44
+ }
45
+
46
+ $effect(() => {
47
+ if (is_collapsible(value)) {
48
+ child_nodes = Object.entries(value);
49
+ } else {
50
+ child_nodes = [];
51
+ }
52
+ });
53
+
54
+ function updateLineNumbers(): void {
55
+ if (!root_element) return;
56
+ const lines = root_element.querySelectorAll(".line");
57
+ lines.forEach((line, index) => {
58
+ const line_number = line.querySelector(".line-number");
59
+ if (line_number) {
60
+ line_number.setAttribute("data-pseudo-content", (index + 1).toString());
61
+ line_number?.setAttribute(
62
+ "aria-roledescription",
63
+ `Line number ${index + 1}`
64
+ );
65
+ line_number?.setAttribute("title", `Line number ${index + 1}`);
66
+ }
67
+ });
68
+ }
69
+
70
+ $effect(() => {
71
+ if (is_root && root_element) {
72
+ updateLineNumbers();
73
+ }
74
+ });
75
+ </script>
76
+
77
+ <div
78
+ class="json-node"
79
+ class:root={is_root}
80
+ class:dark-mode={theme_mode === "dark"}
81
+ bind:this={root_element}
82
+ style="--depth: {depth};"
83
+ >
84
+ <div class="line" class:collapsed>
85
+ <span class="line-number"></span>
86
+ <span class="content">
87
+ {#if is_collapsible(value)}
88
+ <button
89
+ data-pseudo-content={interactive ? (collapsed ? "▶" : "▼") : ""}
90
+ aria-label={collapsed ? "Expand" : "Collapse"}
91
+ class="toggle"
92
+ disabled={!interactive}
93
+ onclick={toggle_collapse}
94
+ />
95
+ {/if}
96
+ {#if key !== null}
97
+ <span class="key">"{key}"</span><span class="punctuation colon"
98
+ >:
99
+ </span>
100
+ {/if}
101
+ {#if is_collapsible(value)}
102
+ <span
103
+ class="punctuation bracket"
104
+ class:square-bracket={Array.isArray(value)}
105
+ >{Array.isArray(value) ? "[" : "{"}</span
106
+ >
107
+ {#if collapsed}
108
+ <button onclick={toggle_collapse} class="preview">
109
+ {get_collapsed_preview(value)}
110
+ </button>
111
+ <span
112
+ class="punctuation bracket"
113
+ class:square-bracket={Array.isArray(value)}
114
+ >{Array.isArray(value) ? "]" : "}"}</span
115
+ >
116
+ {/if}
117
+ {:else if typeof value === "string"}
118
+ <span class="value string">"{value}"</span>
119
+ {:else if typeof value === "number"}
120
+ <span class="value number">{value}</span>
121
+ {:else if typeof value === "boolean"}
122
+ <span class="value bool">{value.toString()}</span>
123
+ {:else if value === null}
124
+ <span class="value null">null</span>
125
+ {:else}
126
+ <span>{value}</span>
127
+ {/if}
128
+ {#if !is_last_item && (!is_collapsible(value) || collapsed)}
129
+ <span class="punctuation">,</span>
130
+ {/if}
131
+ </span>
132
+ </div>
133
+
134
+ {#if is_collapsible(value)}
135
+ <div class="children" class:hidden={collapsed}>
136
+ {#each child_nodes as [subKey, subVal], i}
137
+ <svelte:self
138
+ value={subVal}
139
+ depth={depth + 1}
140
+ is_last_item={i === child_nodes.length - 1}
141
+ key={Array.isArray(value) && !show_indices ? null : subKey}
142
+ {open}
143
+ {theme_mode}
144
+ {show_indices}
145
+ {interactive}
146
+ />
147
+ {/each}
148
+ <div class="line">
149
+ <span class="line-number"></span>
150
+ <span class="content">
151
+ <span
152
+ class="punctuation bracket"
153
+ class:square-bracket={Array.isArray(value)}
154
+ >{Array.isArray(value) ? "]" : "}"}</span
155
+ >
156
+ {#if !is_last_item}<span class="punctuation">,</span>{/if}
157
+ </span>
158
+ </div>
159
+ </div>
160
+ {/if}
161
+ </div>
162
+
163
+ <style>
164
+ .json-node {
165
+ font-family: var(--font-mono);
166
+ --text-color: #d18770;
167
+ --key-color: var(--text-color);
168
+ --string-color: #ce9178;
169
+ --number-color: #719fad;
170
+
171
+ --bracket-color: #5d8585;
172
+ --square-bracket-color: #be6069;
173
+ --punctuation-color: #8fbcbb;
174
+ --line-number-color: #6a737d;
175
+ --separator-color: var(--line-number-color);
176
+ }
177
+ .json-node.dark-mode {
178
+ --bracket-color: #7eb4b3;
179
+ --number-color: #638d9a;
180
+ }
181
+ .json-node.root {
182
+ position: relative;
183
+ padding-left: var(--size-14);
184
+ }
185
+ .json-node.root::before {
186
+ content: "";
187
+ position: absolute;
188
+ top: 0;
189
+ bottom: 0;
190
+ left: var(--size-11);
191
+ width: 1px;
192
+ background-color: var(--separator-color);
193
+ }
194
+ .line {
195
+ display: flex;
196
+ align-items: flex-start;
197
+ padding: 0;
198
+ margin: 0;
199
+ line-height: var(--line-md);
200
+ }
201
+ .line-number {
202
+ position: absolute;
203
+ left: 0;
204
+ width: calc(var(--size-7));
205
+ text-align: right;
206
+ color: var(--line-number-color);
207
+ user-select: none;
208
+ text-overflow: ellipsis;
209
+ text-overflow: ellipsis;
210
+ direction: rtl;
211
+ overflow: hidden;
212
+ }
213
+ .content {
214
+ flex: 1;
215
+ display: flex;
216
+ align-items: center;
217
+ padding-left: calc(var(--depth) * var(--size-2));
218
+ flex-wrap: wrap;
219
+ }
220
+ .children {
221
+ padding-left: var(--size-4);
222
+ }
223
+ .children.hidden {
224
+ display: none;
225
+ }
226
+ .key {
227
+ color: var(--key-color);
228
+ }
229
+ .string {
230
+ color: var(--string-color);
231
+ }
232
+ .number {
233
+ color: var(--number-color);
234
+ }
235
+ .bool {
236
+ color: var(--text-color);
237
+ }
238
+ .null {
239
+ color: var(--text-color);
240
+ }
241
+ .value {
242
+ margin-left: var(--spacing-md);
243
+ }
244
+ .punctuation {
245
+ color: var(--punctuation-color);
246
+ }
247
+ .bracket {
248
+ margin-left: var(--spacing-sm);
249
+ color: var(--bracket-color);
250
+ }
251
+ .square-bracket {
252
+ margin-left: var(--spacing-sm);
253
+ color: var(--square-bracket-color);
254
+ }
255
+ .toggle,
256
+ .preview {
257
+ background: none;
258
+ border: none;
259
+ color: inherit;
260
+ cursor: pointer;
261
+ padding: 0;
262
+ margin: 0;
263
+ }
264
+ .toggle {
265
+ user-select: none;
266
+ margin-right: var(--spacing-md);
267
+ }
268
+ .preview {
269
+ margin: 0 var(--spacing-sm) 0 var(--spacing-lg);
270
+ }
271
+ .preview:hover {
272
+ text-decoration: underline;
273
+ }
274
+
275
+ :global([data-pseudo-content])::before {
276
+ content: attr(data-pseudo-content);
277
+ }
278
+ </style>
6.4.0/json/types.ts ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { LoadingStatus } from "js/statustracker";
2
+ import type { CustomButton } from "@gradio/utils";
3
+
4
+ export interface JSONProps {
5
+ value: any;
6
+ open: boolean;
7
+ show_indices: boolean;
8
+ height: number | string | undefined;
9
+ min_height: number | string | undefined;
10
+ max_height: number | string | undefined;
11
+ theme_mode: "system" | "light" | "dark";
12
+ buttons: (string | CustomButton)[];
13
+ }
14
+
15
+ export interface JSONEvents {
16
+ change: never;
17
+ clear_status: LoadingStatus;
18
+ custom_button_click: { id: number };
19
+ }