/
DemosLite.svelte
225 lines (197 loc) 路 5.38 KB
/
DemosLite.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<script lang="ts">
import { afterNavigate } from "$app/navigation";
import Code from "@gradio/code";
import Slider from "./Slider.svelte";
import Fullscreen from "./icons/Fullscreen.svelte";
import Close from "./icons/Close.svelte";
export let demos: {
name: string;
dir: string;
code: string;
requirements: string[];
}[];
export let current_selection: string;
export let show_nav = true;
let mounted = false;
let controller: any;
let dummy_elem: any = { classList: { contains: () => false } };
let dummy_gradio: any = { dispatch: (_) => {} };
let requirements =
demos.find((demo) => demo.name === current_selection)?.requirements || [];
let code = demos.find((demo) => demo.name === current_selection)?.code || "";
afterNavigate(() => {
controller = createGradioApp({
target: document.getElementById("lite-demo"),
requirements: demos[0].requirements,
code: demos[0].code,
info: true,
container: true,
isEmbed: true,
initialHeight: "100%",
eager: false,
themeMode: null,
autoScroll: false,
controlPageTitle: false,
appMode: true
});
mounted = true;
});
function update(code: string, requirements: string[]) {
try {
controller.run_code(code);
controller.install(requirements);
} catch (error) {
console.error(error);
}
controller.run_code(code);
controller.install(requirements);
}
let timeout: any;
$: code = demos.find((demo) => demo.name === current_selection)?.code || "";
$: requirements =
demos.find((demo) => demo.name === current_selection)?.requirements || [];
$: if (mounted) {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
update(code, requirements);
}, 1000);
}
let position = 0.5;
let fullscreen = false;
function make_full_screen() {
fullscreen = true;
}
let preview_width = 100;
let lg_breakpoint = false;
$: lg_breakpoint = preview_width - 13 >= 688;
</script>
<svelte:head>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css"
/>
<link rel="stylesheet" href="https://gradio-hello-world.hf.space/theme.css" />
</svelte:head>
<div
class=" absolute top-0 bottom-0 right-0"
style="left:{show_nav ? 200 : 37}px"
>
<Slider bind:position bind:show_nav>
<div class="flex-row min-w-0 h-full" class:flex={!fullscreen}>
{#each demos as demo, i}
<div
hidden={current_selection !== demo.name}
class="code-editor w-full border-r"
style="width: {position * 100}%"
>
<div class="flex justify-between align-middle h-8 border-b pl-4 pr-2">
<h3 class="pt-1">Code</h3>
</div>
<Code
bind:value={demos[i].code}
label=""
language="python"
target={dummy_elem}
gradio={dummy_gradio}
lines={10}
/>
</div>
{/each}
<div
class="preview w-full mx-auto"
style="width: {fullscreen ? 100 : (1 - position) * 100}%"
class:fullscreen
bind:clientWidth={preview_width}
>
<div class="flex justify-between align-middle h-8 border-b pl-4 pr-2 ml-0 sm:ml-2">
<div class="flex align-middle">
<h3 class="pr-2 pt-1">Preview</h3>
<p class="pt-1.5 text-sm text-gray-600 hidden sm:block">{preview_width - 13}px</p>
<p
class:text-orange-300={lg_breakpoint}
class:text-gray-300={!lg_breakpoint}
class="pt-2 text-sm pl-2 w-6 hidden sm:block">
<svg viewBox="0 0 110 100" xmlns="http://www.w3.org/2000/svg">
<rect width="50" height="100" rx="15" fill="currentColor"/>
<rect x="60" width="50" height="100" rx="15" fill="currentColor" />
</svg>
</p>
<p
class:text-orange-300={!lg_breakpoint}
class:text-gray-300={lg_breakpoint}
class="pt-2 text-sm pl-2 w-6 hidden sm:block">
<svg viewBox="0 0 110 110" xmlns="http://www.w3.org/2000/svg">
<rect width="110" height="45" rx="15" fill="currentColor"/>
<rect y="50" width="110" height="45" rx="15" fill="currentColor" />
</svg>
</p>
</div>
<div class="flex">
{#if !fullscreen}<button
class="ml-1 w-[20px] float-right text-gray-600"
on:click={() => (fullscreen = true)}><Fullscreen /></button
>{:else}
<button
class="ml-1 w-[15px] float-right text-gray-600"
on:click={() => (fullscreen = false)}><Close /></button
>
{/if}
</div>
</div>
<div class="lite-demo h-[93%] pl-3" id="lite-demo" />
</div>
</div>
</Slider>
</div>
<style>
:global(div.code-editor div.block) {
height: calc(100% - 2rem);
border-radius: 0;
border: none;
}
:global(div.code-editor div.block .cm-gutters) {
background-color: white;
}
:global(div.code-editor div.block .cm-content) {
width: 0;
}
:global(div.lite-demo div.gradio-container) {
height: 100%;
overflow-y: scroll;
margin: 0 !important;
}
.code-editor :global(label) {
display: none;
}
.code-editor :global(.codemirror-wrappper) {
border-radius: var(--block-radius);
}
.code-editor :global(> .block) {
border: none !important;
}
.code-editor :global(.cm-scroller) {
height: 100% !important;
}
.lite-demo :global(.embed-container) {
border: none !important;
}
.fullscreen {
position: fixed !important;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1000;
background-color: white;
}
/* .preview {
width: 100% !important;
} */
@media (max-width: 640px) {
.preview {
width: 100% !important;
}
}
</style>