-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #877 from keisuke-umezawa/feature/table-viewer
Add TableArtifactViewer for csv file
- Loading branch information
Showing
5 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
optuna_dashboard/ts/components/Artifact/TableArtifactViewer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import ClearIcon from "@mui/icons-material/Clear" | ||
import { Box, Modal, useTheme } from "@mui/material" | ||
import IconButton from "@mui/material/IconButton" | ||
import { useSnackbar } from "notistack" | ||
import Papa from "papaparse" | ||
import React, { useState, useEffect, ReactNode } from "react" | ||
import { DataGrid } from "../DataGrid" | ||
|
||
import { Artifact } from "ts/types/optuna" | ||
|
||
export const isTableArtifact = (artifact: Artifact): boolean => { | ||
return artifact.filename.endsWith(".csv") | ||
} | ||
|
||
interface TableArtifactViewerProps { | ||
src: string | ||
filetype: string | undefined | ||
} | ||
|
||
type Data = { | ||
[key: string]: string | number | ||
} | ||
|
||
export const TableArtifactViewer: React.FC<TableArtifactViewerProps> = ( | ||
props | ||
) => { | ||
const [data, setData] = useState<Data[]>([]) | ||
const { enqueueSnackbar } = useSnackbar() | ||
|
||
useEffect(() => { | ||
const handleFileChange = async () => { | ||
try { | ||
const loadedData = await loadCSV(props) | ||
setData(loadedData) | ||
} catch (error: unknown) { | ||
enqueueSnackbar("Failed to load the csv file.", { | ||
variant: "error", | ||
}) | ||
} | ||
} | ||
handleFileChange() | ||
}, [props]) | ||
|
||
const columns = React.useMemo(() => { | ||
const keys = data[0] ? Object.keys(data[0]) : [] | ||
return keys.map((key) => ({ | ||
header: key, | ||
accessorKey: key, | ||
enableSorting: true, | ||
enableColumnFilter: false, | ||
})) | ||
}, [data]) | ||
|
||
return <DataGrid data={data} columns={columns} initialRowsPerPage={10} /> | ||
} | ||
|
||
export const useTableArtifactModal = (): [ | ||
(path: string, artifact: Artifact) => void, | ||
() => ReactNode, | ||
] => { | ||
const [open, setOpen] = useState(false) | ||
const [target, setTarget] = useState<[string, Artifact | null]>(["", null]) | ||
const theme = useTheme() | ||
|
||
const openModal = (artifactUrlPath: string, artifact: Artifact) => { | ||
setTarget([artifactUrlPath, artifact]) | ||
setOpen(true) | ||
} | ||
|
||
const renderDeleteStudyDialog = () => { | ||
return ( | ||
<Modal | ||
open={open} | ||
onClose={() => { | ||
setOpen(false) | ||
setTarget(["", null]) | ||
}} | ||
> | ||
<Box | ||
component="div" | ||
sx={{ | ||
position: "absolute", | ||
top: "50%", | ||
left: "50%", | ||
transform: "translate(-50%, -50%)", | ||
bgcolor: "background.paper", | ||
borderRadius: "15px", | ||
width: "80%", | ||
maxHeight: "80%", | ||
overflowY: "auto", | ||
p: 2, | ||
}} | ||
> | ||
<IconButton | ||
sx={{ | ||
position: "absolute", | ||
top: theme.spacing(2), | ||
right: theme.spacing(2), | ||
}} | ||
onClick={() => { | ||
setOpen(false) | ||
setTarget(["", null]) | ||
}} | ||
> | ||
<ClearIcon /> | ||
</IconButton> | ||
<TableArtifactViewer | ||
src={target[0]} | ||
filetype={target[1]?.filename.split(".").pop()} | ||
/> | ||
</Box> | ||
</Modal> | ||
) | ||
} | ||
return [openModal, renderDeleteStudyDialog] | ||
} | ||
|
||
const loadCSV = (props: TableArtifactViewerProps): Promise<Data[]> => { | ||
return new Promise((resolve, reject) => { | ||
Papa.parse(props.src, { | ||
header: true, | ||
download: true, | ||
complete: (results: Papa.ParseResult<Data>) => { | ||
resolve(results?.data) | ||
}, | ||
error: () => { | ||
reject(new Error("csv parse err")) | ||
}, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters