Skip to content

Commit a556a7d

Browse files
committed
sdg download as csv
1 parent 38c6f3a commit a556a7d

File tree

5 files changed

+75
-20
lines changed

5 files changed

+75
-20
lines changed

src/assets/synthetic-data.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,9 @@ def run():
592592
print("Original Data (first 5 rows):", real_data.head())
593593
print("Synthetic Data (first 5 rows):", synthetic_data.head())
594594
595+
# Store synthetic data for export
595596
setOutputData("syntheticData", synthetic_data.to_json(orient='records'))
596597
597-
598598
results = run_diagnostic(real_data, synthetic_data, target_column='gpa')
599599
print('Results:', results)
600600
setResult(json.dumps(

src/lib/utils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,35 @@ import { twMerge } from 'tailwind-merge';
44
export function cn(...inputs: ClassValue[]) {
55
return twMerge(clsx(inputs));
66
}
7+
8+
export function exportToCSV(data: any[], filename: string) {
9+
// Convert data to CSV format
10+
const csvContent = data.map(row => {
11+
return Object.values(row)
12+
.map(value => {
13+
// Handle special characters and wrap in quotes if needed
14+
const stringValue = String(value);
15+
return stringValue.includes(',') ||
16+
stringValue.includes('"') ||
17+
stringValue.includes('\n')
18+
? `"${stringValue.replace(/"/g, '""')}"`
19+
: stringValue;
20+
})
21+
.join(',');
22+
});
23+
24+
// Add headers
25+
const headers = Object.keys(data[0]).join(',');
26+
const csvString = [headers, ...csvContent].join('\n');
27+
28+
// Create and trigger download
29+
const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8;' });
30+
const link = document.createElement('a');
31+
const url = URL.createObjectURL(blob);
32+
link.setAttribute('href', url);
33+
link.setAttribute('download', `${filename}.csv`);
34+
link.style.visibility = 'hidden';
35+
document.body.appendChild(link);
36+
link.click();
37+
document.body.removeChild(link);
38+
}

src/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"syntheticData": {
6060
"exportToPDF": "Download evaluation report as pdf",
6161
"exportToJSON": "Download synthetic data as JSON",
62+
"exportToCSV": "Download synthetic data as CSV",
6263
"form": {
6364
"errors": {
6465
"csvRequired": "Please upload a CSV file.",

src/locales/nl.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"biasSettings": {
1717
"exportToPDF": "Download bias detection rapport als pdf",
1818
"exportToJSON": "Export clusters als JSON",
19+
1920
"form": {
2021
"fieldsets": {
2122
"data": {
@@ -58,6 +59,7 @@
5859
"syntheticData": {
5960
"exportToPDF": "Download evulatie rapport als pdf",
6061
"exportToJSON": "Download synthetische data als JSON",
62+
"exportToCSV": "Download synthetische data als CSV",
6163
"form": {
6264
"errors": {
6365
"csvRequired": "Upload een CSV-bestand.",

src/routes/SyntheticData.tsx

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from '@/components/ui/dropdown-menu';
2121
import { downloadFile } from '@/lib/download-file';
2222
import { SyntheticDataParameters } from '@/components/synthetic-data-interfaces/SyntheticDataParameters';
23+
import { exportToCSV } from '@/lib/utils';
2324

2425
const PAGE_STYLE = `
2526
@page {
@@ -132,6 +133,12 @@ export default function SyntheticDataGeneration() {
132133
});
133134
};
134135

136+
const handleExport = (syntheticData: object[]) => {
137+
if (syntheticData.length > 0) {
138+
exportToCSV(syntheticData, 'synthetic_data');
139+
}
140+
};
141+
135142
return (
136143
<main ref={contentRef} className="gap-4 p-4 flex flex-col">
137144
{!lang && <LanguageSwitcher />}
@@ -172,25 +179,38 @@ export default function SyntheticDataGeneration() {
172179
{t('syntheticData.exportToPDF')}
173180
</DropdownMenuItem>
174181
{clusterInfo && (
175-
<DropdownMenuItem
176-
onClick={() => {
177-
downloadFile(
178-
JSON.stringify(
179-
{
180-
fileName: data.fileName,
181-
...clusterInfo,
182-
},
183-
null,
184-
2
185-
),
186-
`${data.fileName.replace('.csv', '') || 'cluster-info'}-${clusterInfo.date.toISOString()}.json`,
187-
'application/json'
188-
);
189-
}}
190-
>
191-
<Share className="size-3.5 mr-2" />
192-
{t('syntheticData.exportToJSON')}
193-
</DropdownMenuItem>
182+
<>
183+
<DropdownMenuItem
184+
onClick={() => {
185+
downloadFile(
186+
JSON.stringify(
187+
{
188+
fileName:
189+
data.fileName,
190+
...clusterInfo,
191+
},
192+
null,
193+
2
194+
),
195+
`${data.fileName.replace('.csv', '') || 'cluster-info'}-${clusterInfo.date.toISOString()}.json`,
196+
'application/json'
197+
);
198+
}}
199+
>
200+
<Share className="size-3.5 mr-2" />
201+
{t('syntheticData.exportToJSON')}
202+
</DropdownMenuItem>
203+
<DropdownMenuItem
204+
onClick={() => {
205+
handleExport(
206+
clusterInfo.syntheticData as object[]
207+
);
208+
}}
209+
>
210+
<Share className="size-3.5 mr-2" />
211+
{t('syntheticData.exportToCSV')}
212+
</DropdownMenuItem>
213+
</>
194214
)}
195215
</DropdownMenuContent>
196216
</DropdownMenu>

0 commit comments

Comments
 (0)