Merge pull request #163 from AmitChauhan63390/csv_add

feat: export to csv
This commit is contained in:
Karishma Shukla
2024-11-13 19:38:14 +05:30
committed by GitHub
2 changed files with 56 additions and 23 deletions

0
esbuild.config.js Normal file
View File

View File

@@ -1,14 +1,11 @@
import { Box, Tabs, Typography, Tab, Paper } from "@mui/material"; import { Box, Tabs, Typography, Tab, Paper, Button } from "@mui/material";
import Highlight from "react-highlight"; import Highlight from "react-highlight";
import Button from "@mui/material/Button";
import * as React from "react"; import * as React from "react";
import { Data } from "./RunsTable"; import { Data } from "./RunsTable";
import { TabPanel, TabContext } from "@mui/lab"; import { TabPanel, TabContext } from "@mui/lab";
import SettingsIcon from '@mui/icons-material/Settings';
import ImageIcon from '@mui/icons-material/Image';
import ArticleIcon from '@mui/icons-material/Article'; import ArticleIcon from '@mui/icons-material/Article';
import ImageIcon from '@mui/icons-material/Image';
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import AssignmentIcon from '@mui/icons-material/Assignment';
import Table from '@mui/material/Table'; import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody'; import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell'; import TableCell from '@mui/material/TableCell';
@@ -32,7 +29,7 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
useEffect(() => { useEffect(() => {
setTab(tab); setTab(tab);
}, [interpretationInProgress]) }, [interpretationInProgress]);
useEffect(() => { useEffect(() => {
if (row.serializableOutput && Object.keys(row.serializableOutput).length > 0) { if (row.serializableOutput && Object.keys(row.serializableOutput).length > 0) {
@@ -47,6 +44,29 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
} }
}, [row.serializableOutput]); }, [row.serializableOutput]);
// Function to convert table data to CSV format
const convertToCSV = (data: any[], columns: string[]): string => {
const header = columns.join(',');
const rows = data.map(row =>
columns.map(col => JSON.stringify(row[col], null, 2)).join(',')
);
return [header, ...rows].join('\n');
};
// Function to download CSV file when called
const downloadCSV = () => {
const csvContent = convertToCSV(tableData, columns);
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", "data.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
return ( return (
<Box sx={{ width: '100%' }}> <Box sx={{ width: '100%' }}>
<TabContext value={tab}> <TabContext value={tab}>
@@ -54,7 +74,6 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
<Tabs value={tab} onChange={(e, newTab) => setTab(newTab)} aria-label="run-content-tabs"> <Tabs value={tab} onChange={(e, newTab) => setTab(newTab)} aria-label="run-content-tabs">
<Tab label="Output Data" value='output' /> <Tab label="Output Data" value='output' />
<Tab label="Log" value='log' /> <Tab label="Log" value='log' />
{/* <Tab label="Input" value='input' /> */}
</Tabs> </Tabs>
</Box> </Box>
<TabPanel value='log'> <TabPanel value='log'>
@@ -94,16 +113,30 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
<ArticleIcon sx={{ marginRight: '15px' }} /> <ArticleIcon sx={{ marginRight: '15px' }} />
Captured Data Captured Data
</Typography> </Typography>
{Object.keys(row.serializableOutput).map((key) => { <Box sx={{ display: 'flex', alignItems: 'center', gap: 2, mt: 2 }}>
return ( <Typography>
<div key={`number-of-serializable-output-${key}`}> <a style={{ textDecoration: 'none' }} href={`data:application/json;utf8,${JSON.stringify(row.serializableOutput, null, 2)}`}
<Typography sx={{ margin: '20px 0px 20px 0px' }}> download="data.json">
<a style={{ textDecoration: 'none' }} href={`data:application/json;utf8,${JSON.stringify(row.serializableOutput[key], null, 2)}`} Download as JSON
download={key}>Download as JSON</a> </a>
</Typography> </Typography>
</div> <Typography
) onClick={downloadCSV}
})} sx={{
textTransform: 'none',
color: 'primary.main',
padding: 0,
background: 'none',
'&:hover': {
background: 'none',
},
cursor: 'pointer',
}}
>
Download as CSV
</Typography>
</Box>
{tableData.length > 0 ? ( {tableData.length > 0 ? (
<TableContainer component={Paper} sx={{ maxHeight: 440, marginTop: 2 }}> <TableContainer component={Paper} sx={{ maxHeight: 440, marginTop: 2 }}>
<Table stickyHeader aria-label="sticky table"> <Table stickyHeader aria-label="sticky table">
@@ -139,12 +172,12 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
)} )}
</div> </div>
} }
{row.binaryOutput {row.binaryOutput && Object.keys(row.binaryOutput).length !== 0 &&
&& Object.keys(row.binaryOutput).length !== 0 &&
<div> <div>
<Typography variant='h6' sx={{ display: 'flex', alignItems: 'center' }}> <Typography variant='h6' sx={{ display: 'flex', alignItems: 'center' }}>
<ImageIcon sx={{ marginRight: '15px' }} /> <ImageIcon sx={{ marginRight: '15px' }} />
Captured Screenshot</Typography> Captured Screenshot
</Typography>
{Object.keys(row.binaryOutput).map((key) => { {Object.keys(row.binaryOutput).map((key) => {
try { try {
const imageUrl = row.binaryOutput[key]; const imageUrl = row.binaryOutput[key];
@@ -152,10 +185,10 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
<Box key={`number-of-binary-output-${key}`} sx={{ <Box key={`number-of-binary-output-${key}`} sx={{
width: 'max-content', width: 'max-content',
}}> }}>
<Typography key={`binary-output-key-${key}`} sx={{ margin: '20px 0px 20px 0px' }}> <Typography sx={{ margin: '20px 0px' }}>
<a href={imageUrl} download={key} style={{ textDecoration: 'none' }}>Download Screenshot</a> <a href={imageUrl} download={key} style={{ textDecoration: 'none' }}>Download Screenshot</a>
</Typography> </Typography>
<img key={`image-${key}`} src={imageUrl} alt={key} height='auto' width='700px' /> <img src={imageUrl} alt={key} height='auto' width='700px' />
</Box> </Box>
) )
} catch (e) { } catch (e) {
@@ -171,4 +204,4 @@ export const RunContent = ({ row, currentLog, interpretationInProgress, logEndRe
</TabContext> </TabContext>
</Box> </Box>
); );
} };