Skip to content

Commit

Permalink
Merge pull request #6 from silx-kit/csv-export
Browse files Browse the repository at this point in the history
Add CSV export
  • Loading branch information
axelboc authored Jun 3, 2024
2 parents 681e72a + 744a134 commit 56f1324
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/LocalFileViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { H5WasmLocalFileProvider } from '@h5web/h5wasm';

import { getPlugin } from './plugin-utils';
import type { LocalFile } from './stores';
import { buildMailto, FEEDBACK_MESSAGE } from './utils';
import { buildMailto, FEEDBACK_MESSAGE, getExportURL } from './utils';

export const CACHE_KEY = Symbol('bufferFetcher');

Expand All @@ -16,7 +16,11 @@ function LocalFileViewer(props: Props) {
const { resolvedUrl, file: rawFile } = file;

return (
<H5WasmLocalFileProvider file={rawFile} getPlugin={getPlugin}>
<H5WasmLocalFileProvider
file={rawFile}
getExportURL={getExportURL}
getPlugin={getPlugin}
>
<App
key={resolvedUrl}
disableDarkMode
Expand Down
9 changes: 7 additions & 2 deletions src/RemoteFileViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { suspend } from 'suspend-react';
import { fetchBuffer } from './fetch-utils';
import { getPlugin } from './plugin-utils';
import type { RemoteFile } from './stores';
import { buildMailto, FEEDBACK_MESSAGE } from './utils';
import { buildMailto, FEEDBACK_MESSAGE, getExportURL } from './utils';

export const CACHE_KEY = Symbol('bufferFetcher');

Expand All @@ -20,7 +20,12 @@ function RemoteFileViewer(props: Props) {
const buffer = suspend(fetchBuffer, [resolvedUrl, CACHE_KEY]);

return (
<H5WasmProvider filename={name} buffer={buffer} getPlugin={getPlugin}>
<H5WasmProvider
filename={name}
buffer={buffer}
getExportURL={getExportURL}
getPlugin={getPlugin}
>
<App
key={resolvedUrl}
disableDarkMode
Expand Down
42 changes: 42 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { GetExportURL } from '@h5web/app';
import { createSearchParams } from 'react-router-dom';

import {
Expand Down Expand Up @@ -121,3 +122,44 @@ export const FEEDBACK_MESSAGE = `<<
=> To report an issue, please include screenshots, reproduction steps, etc.
=> To suggest a new feature, please describe the needs this feature would fulfill.
>>`;

export const getExportURL: GetExportURL = (
format,
dataset,
selection,
value,
// eslint-disable-next-line sonarjs/cognitive-complexity
) => {
if (format !== 'csv') {
return undefined;
}

return async () => {
// 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 === 0 || 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] as number).toString()}${
j < cols - 1 ? ',' : ''
}`;
}

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

return new Blob([csv]);
};
};

0 comments on commit 56f1324

Please sign in to comment.