File size: 7,331 Bytes
a03b3ba |
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
<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";
import { page } from "$app/stores";
import share from "$lib/assets/img/anchor_gray.svg";
import { svgCheck } from "$lib/assets/copy.js";
import { browser } from "$app/environment";
export let demos: {
name: string;
dir: string;
code: string;
requirements: string[];
}[];
export let current_selection: string;
export let show_nav = true;
let new_demo = {
name: "Blank",
dir: "Blank",
code: "# Write your own Gradio code here and see what it looks like!\nimport gradio as gr\n\n",
requirements: []
};
demos.push(new_demo);
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;
let copied_link = false;
async function copy_link(name: string) {
let code_b64 = btoa(code);
name = name.replaceAll(" ", "_");
await navigator.clipboard.writeText(
`${$page.url.href.split("?")[0]}?demo=${name}&code=${code_b64}`
);
copied_link = true;
setTimeout(() => (copied_link = false), 2000);
}
$: 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;
if (browser) {
let linked_demo = $page.url.searchParams.get("demo");
let b64_code = $page.url.searchParams.get("code");
if (linked_demo && b64_code) {
current_selection = linked_demo.replaceAll("_", " ");
let demo = demos.find((demo) => demo.name === current_selection);
if (demo) {
demo.code = atob(b64_code);
}
}
}
</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="flex flex-row" style="position: absolute; top: -6%; right: 0.4%">
<button
class="border border-gray-300 rounded-md mx-2 px-2 py-.5 my-[3px] text-md text-gray-600 hover:bg-gray-50 flex"
on:click={() => copy_link(current_selection)}
>
{#if !copied_link}
<img
class="!w-5 align-text-top inline-block self-center mr-1"
src={share}
/>
<p class="inline-block">Share Your App</p>
{:else}
<div class="inline-block align-text-top !w-5 self-center">
{@html svgCheck}
</div>
<p class="inline-block">Copied Link!</p>
{/if}
</button>
</div>
<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 class="flex float-right"></div>
</div>
<Code
bind:value={demos[i].code}
label=""
language="python"
target={dummy_elem}
gradio={dummy_gradio}
lines={10}
interactive="true"
/>
</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>
|