Published
Edited
Jul 7, 2022
Insert cell
Insert cell
Insert cell
dataPackageUrl = 'https://raw.githubusercontent.com/datasets/world-cities/master/datapackage.json'
Insert cell
async function loadDataPackage(dataPackageUrl) {
return await DataPackage.Package.load(dataPackageUrl);
}
Insert cell
dataPackage = loadDataPackage(dataPackageUrl)
Insert cell
resources = dataPackage.resources
Insert cell
resourceNames = dataPackage.resourceNames.filter(resourceName => resourceName !== 'validation_report')
Insert cell
resourceName = resourceNames[0]
Insert cell
resourceInfo = dataPackage.getResource(resourceName)
Insert cell
resourceUrl = `${resourceInfo._basePath}/${resourceInfo.descriptor.path}`
Insert cell
resource = DataPackage.Resource.load({path: resourceUrl})
Insert cell
resourceInfo.schema
Insert cell
resourceInfo.descriptor.schema.fields
Insert cell
Insert cell
viewof searchResults = Inputs.search(resourceData)
Insert cell
Inputs.table(searchResults)
Insert cell
Insert cell
Insert cell
data = new DataPackageClient(dataPackageUrl);
Insert cell
data.describeTables();
Insert cell
data.query()
Insert cell
data
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
data.lastSqlQuery
Insert cell
data.lastQueryArgs
Insert cell
dataTable
Insert cell
class DataPackageClient {
constructor(dataPackageUrl) {
this._dataPackageUrl = dataPackageUrl;
}

async loadDataPackage() {
if (!this._dataPackage) {
this._dataPackage = await DataPackage.Package.load(this._dataPackageUrl);
}
return this._dataPackage;
}
async describeTables() {
const dataPackage = await this.loadDataPackage();
const tables = dataPackage.resourceNames.filter(resourceName => resourceName !== 'validation_report');
return tables.map(resourceName => ({name: resourceName}));
}

async describeColumns({table}={}) {
const dataPackage = await this.loadDataPackage();
const tableResource = dataPackage.getResource(table);
return tableResource.descriptor.schema.fields;
}
async query(string, params, options) {
const dataPackage = await this.loadDataPackage();
const tables = await this.describeTables();
if (tables.length > 0) {
// get first table data for now
const tableResource = dataPackage.getResource(tables[0].name);
let tableRows = [];
tableRows = await tableResource.read({keyed: true});
// tableRows.columns = await this.describeColumns(tables[0].name);
return tableRows;
}
return [];
}
async sql(strings, ...args) {
this.lastSqlQuery = strings;
this.lastQueryArgs = args;
return await this.query(strings, args);
}
}
Insert cell
Insert cell
DataPackage = require('https://unpkg.com/datapackage@1.1.10/dist/datapackage.min.js')
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