2022-04-12 04:18:42 +08:00
|
|
|
<script lang="ts">
|
2022-08-31 04:56:59 +08:00
|
|
|
import { csvParseRows, tsvParseRows } from "d3-dsv";
|
2023-10-31 12:46:02 +08:00
|
|
|
import type { Gradio } from "@gradio/utils";
|
2023-01-18 04:47:40 +08:00
|
|
|
|
2023-10-31 12:46:02 +08:00
|
|
|
export let gradio: Gradio;
|
2023-08-04 06:01:18 +08:00
|
|
|
export let value: (string | number)[][] | string;
|
2022-08-31 04:56:59 +08:00
|
|
|
export let samples_dir: string;
|
2023-01-18 04:47:40 +08:00
|
|
|
export let type: "gallery" | "table";
|
2023-08-04 06:01:18 +08:00
|
|
|
export let selected = false;
|
2023-01-18 04:47:40 +08:00
|
|
|
export let index: number;
|
|
|
|
|
2022-08-31 04:56:59 +08:00
|
|
|
let hovered = false;
|
2023-08-04 06:01:18 +08:00
|
|
|
let loaded_value: (string | number)[][] | string = value;
|
2022-12-27 23:48:32 +08:00
|
|
|
let loaded = Array.isArray(loaded_value);
|
2022-08-31 04:56:59 +08:00
|
|
|
|
|
|
|
$: if (!loaded && typeof value === "string" && /\.[a-zA-Z]+$/.test(value)) {
|
|
|
|
fetch(samples_dir + value)
|
|
|
|
.then((v) => v.text())
|
|
|
|
.then((v) => {
|
|
|
|
try {
|
|
|
|
if ((value as string).endsWith("csv")) {
|
|
|
|
const small_df = v
|
|
|
|
.split("\n")
|
|
|
|
.slice(0, 4)
|
|
|
|
.map((v) => v.split(",").slice(0, 4).join(","))
|
|
|
|
.join("\n");
|
|
|
|
|
2022-12-27 23:48:32 +08:00
|
|
|
loaded_value = csvParseRows(small_df);
|
2022-08-31 04:56:59 +08:00
|
|
|
} else if ((value as string).endsWith("tsv")) {
|
|
|
|
const small_df = v
|
|
|
|
.split("\n")
|
|
|
|
.slice(0, 4)
|
|
|
|
.map((v) => v.split("\t").slice(0, 4).join("\t"))
|
|
|
|
.join("\n");
|
|
|
|
|
2022-12-27 23:48:32 +08:00
|
|
|
loaded_value = tsvParseRows(small_df);
|
2022-08-31 04:56:59 +08:00
|
|
|
} else {
|
2023-10-31 12:46:02 +08:00
|
|
|
throw new Error(gradio.i18n("dataframe.incorrect_format"));
|
2022-08-31 04:56:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
loaded = true;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2023-02-07 23:55:51 +08:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
loaded_value = value;
|
|
|
|
loaded = true;
|
2022-08-31 04:56:59 +08:00
|
|
|
});
|
|
|
|
}
|
2022-04-12 04:18:42 +08:00
|
|
|
</script>
|
|
|
|
|
2022-08-31 04:56:59 +08:00
|
|
|
{#if loaded}
|
2023-08-04 06:01:18 +08:00
|
|
|
<!-- TODO: fix-->
|
|
|
|
<!-- svelte-ignore a11y-no-static-element-interactions-->
|
2022-08-31 04:56:59 +08:00
|
|
|
<div
|
2023-01-18 04:47:40 +08:00
|
|
|
class:table={type === "table"}
|
|
|
|
class:gallery={type === "gallery"}
|
|
|
|
class:selected
|
2022-08-31 04:56:59 +08:00
|
|
|
on:mouseenter={() => (hovered = true)}
|
|
|
|
on:mouseleave={() => (hovered = false)}
|
|
|
|
>
|
2023-02-07 23:55:51 +08:00
|
|
|
{#if typeof loaded_value === "string"}
|
|
|
|
{loaded_value}
|
|
|
|
{:else}
|
|
|
|
<table class="">
|
|
|
|
{#each loaded_value.slice(0, 3) as row, i}
|
|
|
|
<tr>
|
|
|
|
{#each row.slice(0, 3) as cell, j}
|
|
|
|
<td>{cell}</td>
|
|
|
|
{/each}
|
|
|
|
{#if row.length > 3}
|
|
|
|
<td>…</td>
|
|
|
|
{/if}
|
|
|
|
</tr>
|
|
|
|
{/each}
|
|
|
|
{#if value.length > 3}
|
|
|
|
<div
|
|
|
|
class="overlay"
|
|
|
|
class:odd={index % 2 != 0}
|
|
|
|
class:even={index % 2 == 0}
|
|
|
|
class:button={type === "gallery"}
|
|
|
|
/>
|
|
|
|
{/if}
|
|
|
|
</table>
|
|
|
|
{/if}
|
2022-08-31 04:56:59 +08:00
|
|
|
</div>
|
|
|
|
{/if}
|
2023-01-18 04:47:40 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
table {
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
|
|
|
|
td {
|
2023-03-07 04:52:31 +08:00
|
|
|
border: 1px solid var(--table-border-color);
|
2023-01-18 04:47:40 +08:00
|
|
|
padding: var(--size-2);
|
2023-03-07 04:52:31 +08:00
|
|
|
font-size: var(--text-sm);
|
2023-01-18 04:47:40 +08:00
|
|
|
font-family: var(--font-mono);
|
|
|
|
}
|
|
|
|
|
2023-03-07 04:52:31 +08:00
|
|
|
.selected td {
|
2023-03-17 22:41:53 +08:00
|
|
|
border-color: var(--border-color-accent);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 04:52:31 +08:00
|
|
|
.table {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0 auto;
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 04:52:31 +08:00
|
|
|
.gallery td:first-child {
|
2023-01-18 04:47:40 +08:00
|
|
|
border-left: none;
|
|
|
|
}
|
|
|
|
|
2023-03-07 04:52:31 +08:00
|
|
|
.gallery tr:first-child td {
|
2023-01-18 04:47:40 +08:00
|
|
|
border-top: none;
|
|
|
|
}
|
|
|
|
|
2023-03-07 04:52:31 +08:00
|
|
|
.gallery td:last-child {
|
2023-01-18 04:47:40 +08:00
|
|
|
border-right: none;
|
|
|
|
}
|
|
|
|
|
2023-03-07 04:52:31 +08:00
|
|
|
.gallery tr:last-child td {
|
2023-01-18 04:47:40 +08:00
|
|
|
border-bottom: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.overlay {
|
|
|
|
--gradient-to: transparent;
|
|
|
|
position: absolute;
|
|
|
|
bottom: 0;
|
|
|
|
background: linear-gradient(to bottom, transparent, var(--gradient-to));
|
|
|
|
width: var(--size-full);
|
|
|
|
height: 50%;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* i dont know what i've done here but it is what it is */
|
|
|
|
.odd {
|
2023-03-18 08:20:55 +08:00
|
|
|
--gradient-to: var(--table-even-background-fill);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.even {
|
2023-03-18 08:20:55 +08:00
|
|
|
--gradient-to: var(--table-odd-background-fill);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.button {
|
2023-03-18 08:20:55 +08:00
|
|
|
--gradient-to: var(--background-fill-primary);
|
2023-01-18 04:47:40 +08:00
|
|
|
}
|
|
|
|
</style>
|