Published
Edited
Feb 8, 2022
1 fork
Importers
23 stars
Insert cell
Insert cell
Insert cell
url = "https://visionscarto.net/obs/jszip-demo/data.zip"
Insert cell
// Using a CORS proxy is sometimes necessary
A = fetch("https://cors-anywhere.herokuapp.com/" + url).then(zipreader)
Insert cell
Insert cell
penguins = A.get("penguins.csv").then(d3.csvParse)
Insert cell
Insert cell
zipreader = (fileDescriptor, { type = "string" } = {}) =>
fileDescriptor
.arrayBuffer()
.then(d => zip().loadAsync(d))
.then(
({ files }) =>
new Map(Object.keys(files).map(f => [f, files[f].async(type)]))
)
Insert cell
reader = zipreader(FileAttachment("datasets.zip"))
Insert cell
cars = reader.get("cars.csv").then(d3.csvParse)
Insert cell
Insert cell
Insert cell
readerUp = up.arrayBuffer()
.then(d => zip().loadAsync(d))
.then(
({ files }) =>
new Map(Object.keys(files).map(f => [f, files[f].async("string")]))
)
Insert cell
dir = [...readerUp.keys()]
Insert cell
Insert cell
zipreader(FileAttachment("arrow.zip"), { type: "arraybuffer" }).then((d) =>
arrow.Table.from(d.get("data.arrow"))
)
Insert cell
Insert cell
// In this example we save several arrays as csv files, and join them together in a zip file
// note: with this method, they don't seem to be compressed
created = {
const z = zip();
const data = {
hello: [{ a: "1", b: 2 }],
world: [{ some: "good", news: "maybe?" }],
cars,
penguins
};
for (let key in data) z.file(`${key}.csv`, d3.csvFormat(data[key]));
return z.generateAsync({ type: "blob" });
}
Insert cell
button(created, "files.zip")
Insert cell
Insert cell
// "https://bundle.run/jszip@3.5.0"
zip = require(await FileAttachment("jszip-3.5.0").url())
Insert cell
d3 = require("d3-dsv@3")
Insert cell
// https://observablehq.com/@jeremiak/download-data-button
button = (data, filename = 'data') => {
if (!data) throw new Error('Array of data required as first argument');

let downloadData;
if (filename.includes('.csv')) {
downloadData = new Blob([d3.csvFormat(data)], { type: "text/csv" });
} else if (filename.includes('.zip')) {
downloadData = new Blob([data], { type: "application/zip" });
} else {
downloadData = new Blob([JSON.stringify(data, null, 2)], {
type: "application/json"
});
}

const size = (downloadData.size / 1024).toFixed(0);
const button = DOM.download(
downloadData,
filename,
`Download ${filename} (~${size} KB)`
);
return button;
}
Insert cell
arrow = require("apache-arrow@6")
Insert cell
import { file } from "@jashkenas/inputs"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more