Published
Edited
Jan 20, 2022
Importers
Insert cell
# File Input Factoring
Insert cell
import { localFileInput } from "@mbostock/localfile"
Insert cell
viewof inputFile0 = localFileInput({
accept: ".gpx",
value: FileAttachment("Ski HR145-2021-09-04T13:07PM.gpx")
})
Insert cell
togeojson = require("@mapbox/togeojson@0.16.0/togeojson.js").catch(
() => window["toGeoJSON"]
)
Insert cell
XML_File = Files.text(await inputFile0.blob())
Insert cell
json_data = {
return togeojson.gpx(
new DOMParser().parseFromString(
await Files.text(await inputFile0.blob()),
"text/xml"
)
);
}
Insert cell
mutable localFileContent = "No File Chosen Yet"
Insert cell
localFileContent
Insert cell
// what does this do? an unnamed function..
// but wait, it seems to reassign the "window.readFile" func with the "readFile" func here
{
function readFile(event) {
const inputElement = event.target;
const inputFile = inputElement.files[0];

var reader = new FileReader();
reader.onload = function (loadedEvent) {
const parsedResult = togeojson.gpx(
new DOMParser().parseFromString(inputFile, "text/xml")
);

// const parsedResult = JSON.parse(loadedEvent.target.result);

mutable localFileContent = json_data;
};

reader.readAsText(inputFile);
}

return (window.readFile = readFile);
}
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