Published
Edited
Jan 16, 2021
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
text = fetch(proxyUrl + electionsResultUrl).then(response => response.text())
Insert cell
doc2 = (new DOMParser()).parseFromString(text, "text/html");
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
candidates = parseCandidates(doc); // key to election results
Insert cell
Insert cell
wardPrecinctCandidateMatrix = parseElectionResult(doc);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
testText = FileAttachment("dataexport.xls").text()
Insert cell
testDoc = (new DOMParser()).parseFromString(testText, "text/html");
Insert cell
testCandidates = parseCandidates(testDoc);
Insert cell
assertEqual(testCandidates.length, 6)
Insert cell
assertEqual(testCandidates[0], "Joseph R. Biden and Kamala D. Harris")
Insert cell
Insert cell
assertEqual(testWardPrecinctCandidateMatrix.length, 51) // 50 wards and an empty 0th element
Insert cell
assertEqual(testWardPrecinctCandidateMatrix[1].length, 45) // 44 precincts in the 1st ward
Insert cell
assertEqual(testWardPrecinctCandidateMatrix[1][1].length, 6) // number of candidates
Insert cell
assertEqual(testWardPrecinctCandidateMatrix[1][1][0], 667) // votes for 1st candidates in 1st precinct of 1st ward
Insert cell
testFlattened = flatten(testWardPrecinctCandidateMatrix, testCandidates)
Insert cell
assertEqual(testFlattened.length, 2069) // number of precincts
Insert cell
assertEqual(testFlattened.find(row => row.ward == 1 && row.precinct == 1)["Joseph R. Biden and Kamala D. Harris"], 667)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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