Published
Edited
Mar 24, 2021
Importers
4 stars
Also listed in…
D3 Class
Insert cell
Insert cell
Table(tableData)
Insert cell
tableData = getTableData(data[0])

Insert cell
getTableData = (data, start = 1) => {
const d = tableToJson(data, start);
const t = d.slice(1);
const columns = d[0];
return t.map(d =>
columns.reduce((acc, e, i) => {
acc[e] = d[i];
return acc;
}, {})
);
}
Insert cell
function tableToJson(table,start=1) {
//Code from : https://stackoverflow.com/questions/9927126/how-to-convert-the-following-table-to-json-with-javascript
var data = [];
for (var i = start; i < table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = [];
for (var j = 0; j < tableRow.cells.length; j++) {
rowData.push(tableRow.cells[j].innerHTML);
}
data.push(rowData);
}
return data;
}
Insert cell
data = scrapeTableData("https://www.cdc.gov/asthma/mmsa/table_2017.html")
Insert cell
async function scrapeTableData(url) {
const response = await soFetch(url);
const text = await response.text();
//const domparser = new DOMParser();
//const dom = domparser.parseFromString(text, "text/html");
const dom = html`${text}`;
const tables = await dom.querySelectorAll('table');
if (tables.length <= 0) {
throw new Error('Could not find any tables.');
}
const tablesLen = tables.length;

const tablesData = [];
for (let i = 0; i < tablesLen; i++) {
let tableEl = tables[i];

tablesData.push(tableEl);
}
return tablesData;
}
Insert cell
Table(getTableData(scrapedPandemicData[0], 0))
Insert cell
scrapedPandemicData = scrapeTableData(
"https://www.visualcapitalist.com/history-of-pandemics-deadliest/"
)
Insert cell
md`### External Libraries and Imports`
Insert cell
import {Table} from "@observablehq/table"
Insert cell
import { soFetch } from '@alecglassford/so-fetch'
Insert cell
d3 = require("d3@6")
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