gradio-pr-bot commited on
Commit
84d6e8a
·
verified ·
1 Parent(s): 2e82616

Upload folder using huggingface_hub

Browse files
6.0.0-dev.4/fileexplorer/Example.svelte ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value: string[] | string | null;
3
+ export let type: "gallery" | "table";
4
+ export let selected = false;
5
+ </script>
6
+
7
+ <ul
8
+ class:table={type === "table"}
9
+ class:gallery={type === "gallery"}
10
+ class:selected
11
+ >
12
+ {#if value}
13
+ {#each Array.isArray(value) ? value.slice(0, 3) : [value] as path}
14
+ <li><code>./{path}</code></li>
15
+ {/each}
16
+ {#if Array.isArray(value) && value.length > 3}
17
+ <li class="extra">...</li>
18
+ {/if}
19
+ {/if}
20
+ </ul>
21
+
22
+ <style>
23
+ ul {
24
+ white-space: nowrap;
25
+ max-height: 100px;
26
+ list-style: none;
27
+ padding: 0;
28
+ margin: 0;
29
+ }
30
+
31
+ .extra {
32
+ text-align: center;
33
+ }
34
+
35
+ .gallery {
36
+ align-items: center;
37
+ cursor: pointer;
38
+ padding: var(--size-1) var(--size-2);
39
+ text-align: left;
40
+ }
41
+ </style>
6.0.0-dev.4/fileexplorer/Index.svelte ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svelte:options accessors={true} />
2
+
3
+ <script lang="ts">
4
+ import type { FileExplorerProps, FileExplorerEvents } from "./types";
5
+ import { Gradio } from "@gradio/utils";
6
+ import { File } from "@gradio/icons";
7
+
8
+ import { Block, BlockLabel } from "@gradio/atoms";
9
+ import DirectoryExplorer from "./shared/DirectoryExplorer.svelte";
10
+
11
+ import { StatusTracker } from "@gradio/statustracker";
12
+
13
+ import { _ } from "svelte-i18n";
14
+
15
+ const props = $props();
16
+ const gradio = new Gradio<FileExplorerEvents, FileExplorerProps>(props);
17
+
18
+ let old_value = $state(gradio.props.value);
19
+
20
+ let rerender_key = $derived([
21
+ gradio.props.root_dir,
22
+ gradio.props.glob,
23
+ gradio.props.ignore_glob
24
+ ]);
25
+
26
+ $effect(() => {
27
+ if (old_value != 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
+ variant={gradio.props.value === null ? "dashed" : "solid"}
37
+ border_mode={"base"}
38
+ padding={false}
39
+ elem_id={gradio.shared.elem_id}
40
+ elem_classes={gradio.shared.elem_classes}
41
+ container={gradio.shared.container}
42
+ scale={gradio.shared.scale}
43
+ min_width={gradio.shared.min_width}
44
+ allow_overflow={true}
45
+ overflow_behavior="auto"
46
+ height={gradio.props.height}
47
+ max_height={gradio.props.max_height}
48
+ min_height={gradio.props.min_height}
49
+ >
50
+ <StatusTracker
51
+ {...gradio.shared.loading_status}
52
+ autoscroll={gradio.shared.autoscroll}
53
+ i18n={gradio.i18n}
54
+ on:clear_status={() =>
55
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
56
+ />
57
+ <BlockLabel
58
+ show_label={gradio.shared.show_label}
59
+ Icon={File}
60
+ label={gradio.shared.label || "FileExplorer"}
61
+ float={false}
62
+ />
63
+ {#key rerender_key}
64
+ <DirectoryExplorer
65
+ bind:value={gradio.props.value}
66
+ file_count={gradio.props.file_count}
67
+ interactive={gradio.shared.interactive}
68
+ ls_fn={gradio.shared.server.ls}
69
+ />
70
+ {/key}
71
+ </Block>
6.0.0-dev.4/fileexplorer/package.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/fileexplorer",
3
+ "version": "0.5.42-dev.1",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "dependencies": {
9
+ "@gradio/atoms": "workspace:^",
10
+ "@gradio/checkbox": "workspace:^",
11
+ "@gradio/client": "workspace:^",
12
+ "@gradio/file": "workspace:^",
13
+ "@gradio/icons": "workspace:^",
14
+ "@gradio/statustracker": "workspace:^",
15
+ "@gradio/upload": "workspace:^",
16
+ "@gradio/utils": "workspace:^",
17
+ "dequal": "^2.0.3"
18
+ },
19
+ "devDependencies": {
20
+ "@gradio/preview": "workspace:^"
21
+ },
22
+ "main_changeset": true,
23
+ "exports": {
24
+ ".": {
25
+ "gradio": "./Index.svelte",
26
+ "svelte": "./dist/Index.svelte",
27
+ "types": "./dist/Index.svelte.d.ts"
28
+ },
29
+ "./example": {
30
+ "gradio": "./Example.svelte",
31
+ "svelte": "./dist/Example.svelte",
32
+ "types": "./dist/Example.svelte.d.ts"
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "peerDependencies": {
37
+ "svelte": "^5.43.4"
38
+ },
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/gradio-app/gradio.git",
42
+ "directory": "js/fileexplorer"
43
+ }
44
+ }
6.0.0-dev.4/fileexplorer/shared/ArrowIcon.svelte ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svg
2
+ width="100%"
3
+ height="100%"
4
+ viewBox="0 0 14 17"
5
+ version="1.1"
6
+ style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;"
7
+ >
8
+ <g transform="matrix(1,0,0,1,-10.6667,-7.73588)">
9
+ <path
10
+ d="M12.7,24.033C12.256,24.322 11.806,24.339 11.351,24.084C10.896,23.829 10.668,23.434 10.667,22.9L10.667,9.1C10.667,8.567 10.895,8.172 11.351,7.916C11.807,7.66 12.256,7.677 12.7,7.967L23.567,14.867C23.967,15.133 24.167,15.511 24.167,16C24.167,16.489 23.967,16.867 23.567,17.133L12.7,24.033Z"
11
+ style="fill:currentColor;fill-rule:nonzero;"
12
+ />
13
+ </g>
14
+ </svg>
6.0.0-dev.4/fileexplorer/shared/Checkbox.svelte ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher } from "svelte";
3
+ export let value: boolean;
4
+ export let disabled: boolean;
5
+ const dispatch = createEventDispatcher<{ change: boolean }>();
6
+ </script>
7
+
8
+ <input
9
+ bind:checked={value}
10
+ on:input={() => dispatch("change", !value)}
11
+ type="checkbox"
12
+ {disabled}
13
+ class:disabled={disabled && !value}
14
+ />
15
+
16
+ <style>
17
+ input {
18
+ --ring-color: transparent;
19
+ position: relative;
20
+ box-shadow: var(--input-shadow);
21
+ border: 1px solid var(--checkbox-border-color);
22
+ border-radius: var(--radius-xs);
23
+ background-color: var(--checkbox-background-color);
24
+ line-height: var(--line-sm);
25
+ width: 18px !important;
26
+ height: 18px !important;
27
+ }
28
+
29
+ input:checked,
30
+ input:checked:hover,
31
+ input:checked:focus {
32
+ border-color: var(--checkbox-border-color-selected);
33
+ background-image: var(--checkbox-check);
34
+ background-color: var(--checkbox-background-color-selected);
35
+ }
36
+
37
+ input:hover {
38
+ border-color: var(--checkbox-border-color-hover);
39
+ background-color: var(--checkbox-background-color-hover);
40
+ }
41
+
42
+ input:focus {
43
+ border-color: var(--checkbox-border-color-focus);
44
+ background-color: var(--checkbox-background-color-focus);
45
+ }
46
+
47
+ .disabled {
48
+ opacity: 0.8 !important;
49
+ cursor: not-allowed;
50
+ }
51
+ </style>
6.0.0-dev.4/fileexplorer/shared/DirectoryExplorer.svelte ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import FileTree from "./FileTree.svelte";
3
+ import type { FileNode } from "./types";
4
+
5
+ export let interactive: boolean;
6
+ export let file_count: "single" | "multiple" = "multiple";
7
+ export let value: string[][] = [];
8
+ export let ls_fn: (path: string[]) => Promise<FileNode[]>;
9
+ let selected_folders: string[][] = [];
10
+
11
+ const paths_equal = (path: string[], path_2: string[]): boolean => {
12
+ return path.join("/") === path_2.join("/");
13
+ };
14
+
15
+ const path_in_set = (path: string[], set: string[][]): boolean => {
16
+ return set.some((x) => paths_equal(x, path));
17
+ };
18
+
19
+ const path_inside = (path: string[], path_2: string[]): boolean => {
20
+ return path.join("/").startsWith(path_2.join("/"));
21
+ };
22
+ </script>
23
+
24
+ <div class="file-wrap">
25
+ <FileTree
26
+ path={[]}
27
+ selected_files={value}
28
+ {selected_folders}
29
+ {interactive}
30
+ {ls_fn}
31
+ {file_count}
32
+ valid_for_selection={false}
33
+ on:check={(e) => {
34
+ const { path, checked, type } = e.detail;
35
+ if (checked) {
36
+ if (file_count === "single") {
37
+ value = [path];
38
+ } else if (type === "folder") {
39
+ if (!path_in_set(path, selected_folders)) {
40
+ selected_folders = [...selected_folders, path];
41
+ }
42
+ } else {
43
+ if (!path_in_set(path, value)) {
44
+ value = [...value, path];
45
+ }
46
+ }
47
+ } else {
48
+ selected_folders = selected_folders.filter(
49
+ (folder) => !path_inside(path, folder)
50
+ ); // deselect all parent folders
51
+ if (type === "folder") {
52
+ selected_folders = selected_folders.filter(
53
+ (folder) => !path_inside(folder, path)
54
+ ); // deselect all children folders
55
+ value = value.filter((file) => !path_inside(file, path)); // deselect all children files
56
+ } else {
57
+ value = value.filter((x) => !paths_equal(x, path));
58
+ }
59
+ }
60
+ }}
61
+ />
62
+ </div>
63
+
64
+ <style>
65
+ .file-wrap {
66
+ height: calc(100% - 25px);
67
+ overflow-y: scroll;
68
+ }
69
+ </style>
6.0.0-dev.4/fileexplorer/shared/FileTree.svelte ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { FileNode } from "./types";
3
+ import { createEventDispatcher } from "svelte";
4
+
5
+ import Arrow from "./ArrowIcon.svelte";
6
+ import Checkbox from "./Checkbox.svelte";
7
+ import FileIcon from "../icons/light-file.svg";
8
+ import FolderIcon from "../icons/light-folder.svg";
9
+
10
+ export let path: string[] = [];
11
+ export let selected_files: string[][] = [];
12
+ export let selected_folders: string[][] = [];
13
+ export let is_selected_entirely = false;
14
+ export let interactive: boolean;
15
+ export let ls_fn: (path: string[]) => Promise<FileNode[]>;
16
+ export let file_count: "single" | "multiple" = "multiple";
17
+ export let valid_for_selection: boolean;
18
+
19
+ let content: FileNode[] = [];
20
+ let opened_folders: number[] = [];
21
+
22
+ const toggle_open_folder = (i: number): void => {
23
+ if (opened_folders.includes(i)) {
24
+ opened_folders = opened_folders.filter((x) => x !== i);
25
+ } else {
26
+ opened_folders = [...opened_folders, i];
27
+ }
28
+ };
29
+
30
+ const open_folder = (i: number): void => {
31
+ if (!opened_folders.includes(i)) {
32
+ opened_folders = [...opened_folders, i];
33
+ }
34
+ };
35
+
36
+ (async () => {
37
+ content = await ls_fn(path);
38
+ if (valid_for_selection) {
39
+ content = [{ name: ".", type: "file" }, ...content];
40
+ }
41
+ opened_folders = content
42
+ .map((x, i) =>
43
+ x.type === "folder" &&
44
+ (is_selected_entirely || selected_files.some((y) => y[0] === x.name))
45
+ ? i
46
+ : null
47
+ )
48
+ .filter((x): x is number => x !== null);
49
+ })();
50
+
51
+ $: if (is_selected_entirely) {
52
+ content.forEach((x) => {
53
+ dispatch("check", {
54
+ path: [...path, x.name],
55
+ checked: true,
56
+ type: x.type
57
+ });
58
+ });
59
+ }
60
+
61
+ const dispatch = createEventDispatcher<{
62
+ check: { path: string[]; checked: boolean; type: "file" | "folder" };
63
+ }>();
64
+ </script>
65
+
66
+ <ul>
67
+ {#each content as { type, name, valid }, i}
68
+ <li>
69
+ <span class="wrap">
70
+ {#if type === "folder" && file_count === "single"}
71
+ <span class="no-checkbox" aria-hidden="true"></span>
72
+ {:else}
73
+ <Checkbox
74
+ disabled={!interactive}
75
+ value={(type === "file" ? selected_files : selected_folders).some(
76
+ (x) => x[0] === name && x.length === 1
77
+ )}
78
+ on:change={(e) => {
79
+ let checked = e.detail;
80
+ dispatch("check", {
81
+ path: [...path, name],
82
+ checked,
83
+ type
84
+ });
85
+ if (type === "folder" && checked) {
86
+ open_folder(i);
87
+ }
88
+ }}
89
+ />
90
+ {/if}
91
+
92
+ {#if type === "folder"}
93
+ <span
94
+ class="icon"
95
+ class:hidden={!opened_folders.includes(i)}
96
+ on:click|stopPropagation={() => toggle_open_folder(i)}
97
+ role="button"
98
+ aria-label="expand directory"
99
+ tabindex="0"
100
+ on:keydown={({ key }) => {
101
+ if (key === " " || key === "Enter") {
102
+ toggle_open_folder(i);
103
+ }
104
+ }}><Arrow /></span
105
+ >
106
+ {:else}
107
+ <span class="file-icon">
108
+ <img src={name === "." ? FolderIcon : FileIcon} alt="file icon" />
109
+ </span>
110
+ {/if}
111
+ {name}
112
+ </span>
113
+ {#if type === "folder" && opened_folders.includes(i)}
114
+ <svelte:self
115
+ path={[...path, name]}
116
+ selected_files={selected_files
117
+ .filter((x) => x[0] === name)
118
+ .map((x) => x.slice(1))}
119
+ selected_folders={selected_folders
120
+ .filter((x) => x[0] === name)
121
+ .map((x) => x.slice(1))}
122
+ is_selected_entirely={selected_folders.some(
123
+ (x) => x[0] === name && x.length === 1
124
+ )}
125
+ {interactive}
126
+ {ls_fn}
127
+ {file_count}
128
+ valid_for_selection={valid}
129
+ on:check
130
+ />
131
+ {/if}
132
+ </li>
133
+ {/each}
134
+ </ul>
135
+
136
+ <style>
137
+ .icon {
138
+ display: inline-block;
139
+ width: 18px;
140
+ height: 18px;
141
+ padding: 3px 2px 3px 3px;
142
+ margin: 0;
143
+ flex-grow: 0;
144
+ display: inline-flex;
145
+ justify-content: center;
146
+ align-items: center;
147
+ border-radius: 2px;
148
+ cursor: pointer;
149
+ transition: 0.1s;
150
+ flex-shrink: 0;
151
+ }
152
+
153
+ .file-icon {
154
+ display: inline-block;
155
+ height: 20px;
156
+ margin-left: -1px;
157
+ margin: 0;
158
+ flex-grow: 0;
159
+ display: inline-flex;
160
+ justify-content: center;
161
+ align-items: center;
162
+
163
+ transition: 0.1s;
164
+ }
165
+
166
+ .file-icon img {
167
+ width: 100%;
168
+ height: 100%;
169
+ }
170
+
171
+ .icon:hover {
172
+ background: #eee;
173
+ }
174
+
175
+ .icon:hover :global(> *) {
176
+ color: var(--block-info-text-color);
177
+ }
178
+
179
+ .icon :global(> *) {
180
+ transform: rotate(90deg);
181
+ transform-origin: 40% 50%;
182
+ transition: 0.2s;
183
+ color: var(--color-accent);
184
+ }
185
+
186
+ .no-checkbox {
187
+ width: 18px;
188
+ height: 18px;
189
+ }
190
+
191
+ .hidden :global(> *) {
192
+ transform: rotate(0);
193
+ color: var(--body-text-color-subdued);
194
+ }
195
+
196
+ ul {
197
+ margin-left: 26px;
198
+ padding-left: 0;
199
+ list-style: none;
200
+ }
201
+
202
+ li {
203
+ margin-left: 0;
204
+ padding-left: 0;
205
+ align-items: center;
206
+ margin: 8px 0;
207
+ font-family: var(--font-mono);
208
+ font-size: var(--scale-00);
209
+ overflow-wrap: anywhere;
210
+ word-break: break-word;
211
+ }
212
+
213
+ .wrap {
214
+ display: flex;
215
+ gap: 8px;
216
+ align-items: center;
217
+ }
218
+ </style>
6.0.0-dev.4/fileexplorer/shared/types.ts ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ export interface FileNode {
2
+ type: "file" | "folder";
3
+ name: string;
4
+ valid?: boolean;
5
+ }
6.0.0-dev.4/fileexplorer/types.ts ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { LoadingStatus } from "@gradio/statustracker";
2
+ import type { FileNode } from "./shared/types";
3
+
4
+ export interface FileExplorerProps {
5
+ value: string[][];
6
+ height: number | string | undefined;
7
+ min_height: number | string | undefined;
8
+ max_height: number | string | undefined;
9
+ file_count: "single" | "multiple";
10
+ root_dir: string;
11
+ glob: string;
12
+ ignore_glob: string;
13
+ server: {
14
+ ls: (path: string[]) => Promise<FileNode[]>;
15
+ };
16
+ }
17
+
18
+ export interface FileExplorerEvents {
19
+ change: never;
20
+ clear_status: LoadingStatus;
21
+ }