Skip to content

Commit

Permalink
Merge pull request optuna#887 from keisuke-umezawa/feature/table-view…
Browse files Browse the repository at this point in the history
…er-jsonl

Add TableArtifactViewer for jsonl file
  • Loading branch information
porink0424 authored Jul 3, 2024
2 parents 42339ca + cc0ab0f commit 4b2f831
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions optuna_dashboard/ts/components/Artifact/TableArtifactViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import { DataGrid } from "../DataGrid"

import { Artifact } from "ts/types/optuna"

import axios from "axios"

export const isTableArtifact = (artifact: Artifact): boolean => {
return artifact.filename.endsWith(".csv")
return (
artifact.filename.endsWith(".csv") || artifact.filename.endsWith(".jsonl")
)
}

interface TableArtifactViewerProps {
Expand All @@ -18,7 +22,8 @@ interface TableArtifactViewerProps {
}

type Data = {
[key: string]: string | number
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any
}

export const TableArtifactViewer: React.FC<TableArtifactViewerProps> = (
Expand All @@ -30,10 +35,10 @@ export const TableArtifactViewer: React.FC<TableArtifactViewerProps> = (
useEffect(() => {
const handleFileChange = async () => {
try {
const loadedData = await loadCSV(props)
const loadedData = await loadData(props)
setData(loadedData)
} catch (error: unknown) {
enqueueSnackbar("Failed to load the csv file.", {
enqueueSnackbar(`Failed to load the file. ${error}`, {
variant: "error",
})
}
Expand All @@ -42,10 +47,17 @@ export const TableArtifactViewer: React.FC<TableArtifactViewerProps> = (
}, [props])

const columns = React.useMemo(() => {
const keys = data[0] ? Object.keys(data[0]) : []
const unionSet: Set<string> = new Set()
data.forEach((d) => {
Object.keys(d).forEach((key) => {
unionSet.add(key)
})
})
const keys = Array.from(unionSet)
return keys.map((key) => ({
header: key,
accessorKey: key,
accessorFn: (info: Data) =>
typeof info[key] === "object" ? JSON.stringify(info[key]) : info[key],
enableSorting: true,
enableColumnFilter: false,
}))
Expand Down Expand Up @@ -115,6 +127,16 @@ export const useTableArtifactModal = (): [
return [openModal, renderDeleteStudyDialog]
}

const loadData = (props: TableArtifactViewerProps): Promise<Data[]> => {
if (props.filetype === "csv") {
return loadCSV(props)
} else if (props.filetype === "jsonl") {
return loadJsonl(props)
} else {
return Promise.reject(new Error("Unsupported file type"))
}
}

const loadCSV = (props: TableArtifactViewerProps): Promise<Data[]> => {
return new Promise((resolve, reject) => {
Papa.parse(props.src, {
Expand All @@ -124,8 +146,25 @@ const loadCSV = (props: TableArtifactViewerProps): Promise<Data[]> => {
resolve(results?.data)
},
error: () => {
reject(new Error("csv parse err"))
reject(new Error("CSV parse error"))
},
})
})
}

const loadJsonl = async (props: TableArtifactViewerProps): Promise<Data[]> => {
const response = await axios.get(props.src, { responseType: "text" })
const data = response.data
try {
const jsons = data
.split("\n")
.filter((line: string) => line.trim().length > 0)
.map((line: string) => {
return JSON.parse(line)
})
.filter(Boolean) as Data[]
return jsons
} catch (error) {
throw new Error("JSONL parse error")
}
}

0 comments on commit 4b2f831

Please sign in to comment.