Add TableArtifactViewer

This commit is contained in:
keisuke-umezawa
2024-05-08 21:13:22 +09:00
parent 987fc0cdab
commit 656fdeba14
2 changed files with 67 additions and 1 deletions
@@ -2,6 +2,7 @@ import InsertDriveFileIcon from "@mui/icons-material/InsertDriveFile"
import { Box, CardMedia } from "@mui/material"
import React, { FC } from "react"
import { Artifact } from "ts/types/optuna"
import { TableArtifactViewer, isTableArtifact } from "./TableArtifactViewer"
import {
ThreejsArtifactViewer,
isThreejsArtifact,
@@ -13,7 +14,14 @@ export const ArtifactCardMedia: FC<{
urlPath: string
height: string
}> = ({ artifact, urlPath, height }) => {
if (isThreejsArtifact(artifact)) {
if (isTableArtifact(artifact)) {
return (
<TableArtifactViewer
src={urlPath}
filetype={artifact.filename.split(".").pop()}
/>
)
} else if (isThreejsArtifact(artifact)) {
return (
<ThreejsArtifactViewer
src={urlPath}
@@ -0,0 +1,58 @@
import Papa from "papaparse"
import React, { useState } from "react"
import { DataGrid } from "../DataGrid"
import { Artifact } from "ts/types/optuna"
export const isTableArtifact = (artifact: Artifact): boolean => {
return (
artifact.filename.endsWith(".csv") || artifact.filename.endsWith(".jsonl")
)
}
interface TableArtifactViewerProps {
src: string
filetype: string | undefined
}
type Data = {
[key: string]: any
}
export const TableArtifactViewer: React.FC<TableArtifactViewerProps> = (
props
) => {
const [data, setData] = useState<Data[]>([])
const handleFileChange = async () => {
const loadedData = await loadCSV(props)
setData(loadedData)
}
handleFileChange()
console.log(data)
const columns = React.useMemo(() => {
const keys = data[0] ? Object.keys(data[0]) : []
console.log(keys)
return keys.map((key) => ({
accessorKey: key,
header: key,
}))
}, [data])
return <DataGrid data={data} columns={columns} />
}
const loadCSV = (props: TableArtifactViewerProps): any => {
return new Promise((resolve, reject) => {
Papa.parse(props.src, {
header: true,
download: true,
complete: (results: any) => {
resolve(results?.data)
},
error: () => {
reject(new Error("csv parse err"))
},
})
})
}