Last active
November 18, 2022 05:43
-
-
Save bgrins/84faca8b860ed0f535868c08c80962b2 to your computer and use it in GitHub Desktop.
deno run -A make-html.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsxImportSource https://esm.sh/preact */ | |
import { readCSVObjects } from "https://deno.land/x/csv/mod.ts"; | |
import { render } from "npm:preact-render-to-string"; | |
import { existsSync } from "https://deno.land/std/fs/mod.ts"; | |
const URL = | |
"https://github.com/simonw/fara-history/raw/master/FARA_All_RegistrantDocs.csv"; | |
const FILENAME = "FARA_All_RegistrantDocs.csv"; | |
if (!existsSync(FILENAME)) { | |
console.log(`Fetching ${FILENAME} from ${URL}`); | |
const res = await fetch(URL); | |
Deno.writeTextFileSync(FILENAME, await res.text()); | |
} | |
async function data() { | |
let rows = []; | |
const f = await Deno.open(FILENAME); | |
let header; | |
for await (const row of readCSVObjects(f, { | |
lineSeparator: "\r\n", | |
})) { | |
if (!header) { | |
header = ( | |
<tr> | |
<td>Num</td> | |
{Object.keys(row).map((k) => ( | |
<th>{k}</th> | |
))} | |
</tr> | |
); | |
} | |
rows.push( | |
<tr> | |
<td>{rows.length}</td> | |
{Object.values(row).map((v) => ( | |
<th>{v}</th> | |
))} | |
</tr> | |
); | |
} | |
f.close(); | |
return { header, rows }; | |
} | |
function Output({ header, rows }, count) { | |
rows = rows.slice(0, count); | |
return ( | |
<table> | |
{header} | |
{rows} | |
</table> | |
); | |
} | |
console.log("Preparing data"); | |
let d = await data(); | |
console.log("Writing tables"); | |
Deno.writeTextFileSync("rows100.html", render(Output(d, 100))); | |
Deno.writeTextFileSync("rows1000.html", render(Output(d, 1000))); | |
Deno.writeTextFileSync("rows10000.html", render(Output(d, 10000))); | |
Deno.writeTextFileSync("rows100000.html", render(Output(d, 100000))); | |
console.log("Done"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsxImportSource https://esm.sh/preact */ | |
import { render } from "npm:preact-render-to-string"; | |
import prettier from "npm:prettier"; | |
import { existsSync } from "https://deno.land/std/fs/mod.ts"; | |
import { parse } from "https://deno.land/std/flags/mod.ts"; | |
import { DB } from "https://deno.land/x/sqlite/mod.ts"; | |
const { url, file } = parse(Deno.args, { | |
default: { | |
url: "https://global-power-plants.datasettes.com/global-power-plants.db", | |
file: "global-power-plants.db", | |
}, | |
}); | |
console.log("args", { url, file }); | |
try { | |
new URL(url); | |
} catch (e) { | |
console.error("Invalid URL"); | |
Deno.exit(); | |
} | |
if (!existsSync(file)) { | |
console.log(`Saving ${file} from ${url}`); | |
const res = await fetch(url); | |
const buffer = await res.arrayBuffer(); | |
Deno.writeFileSync(file, new Uint8Array(buffer)); | |
} | |
const db = new DB(file); | |
let column_names = db | |
.query(`pragma table_info('global-power-plants')`) | |
.map((r) => r[1]); | |
let header = ( | |
<tr> | |
{column_names.map((c) => ( | |
<td>{c}</td> | |
))} | |
</tr> | |
); | |
let rows = db | |
.query(`SELECT * FROM [global-power-plants] LIMIT 1000`) | |
.map((row) => ( | |
<tr> | |
{row.map((c) => ( | |
<td>{c}</td> | |
))} | |
</tr> | |
)); | |
Deno.writeTextFileSync( | |
"p.html", | |
prettier.format( | |
render( | |
<table> | |
{header} | |
{rows} | |
</table> | |
), | |
{ parser: "html" } | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment