Skip to content

Commit

Permalink
Merge pull request #23 from mandrew9/main
Browse files Browse the repository at this point in the history
Enable support for exporting 1D and 2D dataset slices.
  • Loading branch information
axelboc authored Sep 7, 2023
2 parents 67e6538 + 61cf5f4 commit 7f4a9f4
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,52 @@ import { vscode } from './vscode-api.js';
// 2 GB = 2 * 1024 * 1024 * 1024 B
export const MAX_SIZE_IN_BYTES = 2147483648;

export const getExportURL: GetExportURL = (format, dataset, __, value) => {
export const getExportURL: GetExportURL = (
format,
dataset,
selection,
value
) => {
function doExport(payload: string): Blob {
// Send payload to `H5WebViewer` editor provider
vscode.postMessage({
type: MessageType.Export,
data: { format, name: dataset.name, payload },
});

return new Blob(); // doesn't matter as long as it's not falsy
}

if (format === 'json') {
return async () => doExport(JSON.stringify(value, null, 2));
}

if (format === 'csv') {
return async () => {
vscode.postMessage({
type: MessageType.Export,
data: {
format,
name: dataset.name,
payload: JSON.stringify(value, null, 2),
},
});
return new Blob(); // doesn't matter as long as it's not falsy
// Find dimensions of dataset/slice to export
// Note that there is currently no way to know if the dataset/slice is transposed - https://github.com/silx-kit/h5web/issues/1454
const dims = selection
? dataset.shape.filter((_, index) => selection[index * 2] === ':')
: dataset.shape;

if (dims.length < 1 || dims.length > 2) {
throw new Error(
'Expected dataset/slice to export to have 1 or 2 dimensions'
);
}

let csv = '';
const [rows, cols = 1] = dims; // export 1D dataset/slice as column (i.e. 1 value per line)

for (let i = 0; i < rows; i += 1) {
for (let j = 0; j < cols; j += 1) {
csv += `${value[i * cols + j].toString()}${j < cols - 1 ? ',' : ''}`;
}

csv += i < rows - 1 ? '\n' : '';
}

return doExport(csv);
};
}

Expand Down

0 comments on commit 7f4a9f4

Please sign in to comment.