This package implements DDF model for datasets made of csv files and a datapackage.json. Once initialised, this reader can receive DDFQL queries, perform data searching, filtering, joinery etc and output an tidy data array for data visualisation.
See live example here https://observablehq.com/@vizabi/ddfcsv-reader
npm i @vizabi/reader-ddfcsv
There are two implementations: for frontend and backend. Additionally, you can customise file reading plugin, so that for example backend implementation would fetch files online instead of reading locally.
-
Backend implementation is at
lib/src/index.js
, also usable via es6 importimport DDFCsvReader from "@vizabi/reader-ddfcsv"
, as aliased by "main" field in package.json -
Frontend implementation is at
lib-web/src/index-web.js
, also bundled and minified todist/reader-ddfcsv.js
//this will import "lib/src/index.js", aliased by "main" in package.json
import DDFCsvReader from "@vizabi/reader-ddfcsv";
//init
const readerInstance = new DDFCsvReader.getDDFCsvReaderObject();
readerInstance.init({path: "path/to/local/dataset/root"});
//test reading some concepts
const concepts = await readerInstance.read({
from: "concepts",
language: "en",
select: {key: ["concept"], value: ["concept_type", "name"]},
where: {}
})
On a plain webpage use
import "@vizabi/reader-ddfcsv/dist/reader-ddfcsv.js";
//init
const ddfReader = new DDFCsvReader.getDDFCsvReaderObject();
reader.init({
path: "https://github.com/open-numbers/ddf--gapminder--fasttrack.git"
})
const data = await readerInstance.read({
from: "datapoints",
language: "en",
select: {key: ["country", "time"], value: ["gdp_pcap", "lex", "pop"]},
where: {}
})
In Observable notebooks use
DDFCsvReader = require('https://unpkg.com/@vizabi/[email protected]')
which will redirect to https://unpkg.com/@vizabi/[email protected]/dist/reader-ddfcsv.js
See the Live demo
Each implementation has its own default file readers built-in:
- FrontendFileReader is a part of
lib-web/src/index-web.js
version, also usable fromdist/reader-ddfcsv.js
- BackendFileReader is a part of
lib/src/index.js
version.
You can find in the source code and import them readers separately
import { FrontendFileReader } from "@vizabi/reader-ddfcsv/lib-web/src/index-web.js";
This reader is designed for file reading via HTTP protocol. It uses fetch
, which since v4.5.2 is no longer polyfilled
This reader is designed for file reading via OS file system. It uses fs
and path
The backend version of DDFCsvReader
defaults to fs
for local files.
To fetch data from GitHub (or any remote source), inject the FrontendFileReader
:
import DDFCsvReader from "@vizabi/reader-ddfcsv";
import { FrontendFileReader } from "@vizabi/reader-ddfcsv/lib-web/src/index-web.js";
//init
const readerInstance = new DDFCsvReader.prepareDDFCsvReaderObject(new FrontendFileReader)();
readerInstance.init({ path: "https://github.com/open-numbers/ddf--gapminder--ai_worldview_benchmark.git" });
//usage example
const concepts = await readerInstance.read({
from: "concepts",
language: "en",
select: {key: ["concept"], value: ["concept_type", "name"]},
where: {}
})
Once you can access concepts of a dataset you can run any other DDFQL query, see DDFQL specs
//typical datapoint query
data = readerInstance.read({
from: "datapoints",
language: "en",
select: {key: ["country", "time"], value: ["gdp_pcap", "lex", "pop"]},
where: {}
})
interactive example: https://observablehq.com/@vizabi/ddfcsv-reader
git clone https://github.com/vizabi/reader-ddfcsv.git
cd reader-ddfcsv
npm i
npm run build
npm test