Public
Edited
Dec 18, 2023
1 star
Insert cell
Insert cell
hdf5 = {
let lib = await import("https://cdn.jsdelivr.net/npm/h5wasm@0.7.1/dist/esm/hdf5_hl.js");
await lib.ready;
return lib
}
Insert cell
new_file = new hdf5.File("myfile.h5", "w");
Insert cell
entry = new_file.create_group("entry");
Insert cell
// shape and dtype will match input if omitted
auto_data = entry.create_dataset({name: "auto", data: [3.1, 4.1, 0.0, -1.0]});
Insert cell
auto_data.shape
Insert cell
auto_data.dtype
Insert cell
auto_data.value
Insert cell
// make float array instead of double (shape will still match input if it is set to null)
// format code for float32 is 'f'
float_data = entry.create_dataset({name: "data", data: [3.1, 4.1, 0.0, -1.0], shape: null, dtype: '<f'});
Insert cell
float_data.value
Insert cell
// create a dataset with shape=[2,2]
// The dataset stored in the HDF5 file with the correct shape,
// but no attempt is made to make a 2x2 array out of it in javascript
square_data = entry.create_dataset({name: "square_data", data: [3.1, 4.1, 0.0, -1.0], shape: [2,2], dtype: '<d'});
Insert cell
square_data.shape
Insert cell
square_data.value
Insert cell
## Attributes
Insert cell
// create an attribute (creates a VLEN string by default for a string)
{
entry.create_attribute("myattr", "a string");
return entry.attrs["myattr"];
}
Insert cell
// create fixed-length string array attribute
{
entry.create_attribute("fixed", ["hello", "you"], null, "S5");
return entry.attrs["fixed"];
}
Insert cell
## Closing
Close the file when done... (preventing memory leaks)
Insert cell
// not doing this here, because it will interfere with cells running above...
// new_file.close()
Insert cell
## Download
click button to download the file at any time...
Insert cell
helpers = await import("https://cdn.jsdelivr.net/npm/h5wasm@0.7.1/dist/esm/file_handlers.js");
Insert cell
viewof downloader = Inputs.button("Download new_file", {value: null, reduce: () => helpers.download(new_file)})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more