import pythonWasm from "python-wasm";
import debug from "debug";
const log = debug("browser:python");
export default async function main() {
log("call python init");
const t0 = new Date().valueOf();
const python = await pythonWasm({ noReadline: true });
const tm = new Date().valueOf() - t0;
log("loaded python");
(window as any).python = python;
console.log("set window.python");
const { exec, repr } = python;
log(await repr("2+3"));
const element = document.createElement("pre");
await exec("import sqlite3");
log(await repr("sqlite3"));
await exec("from random import randint; n = randint(0,10**6)");
log("computed n = ", await repr("n"));
await exec("s = sum(range(n))");
await exec("import sys");
element.innerHTML = `Load/startup time: ${tm}ms\n\n${await repr(
"sys.version"
)}\n\nLet's do some math!\n\n1 + 2 + 3 + ... + ${await repr(
"n"
)} = ${await repr("s")}`;
const sql = `
import sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("CREATE TABLE movies(title, year, score)")
cur.execute("INSERT INTO movies values('Red Dawn',1984,50)")
cur.execute("INSERT INTO movies values('Red Dawn',2012,15)")
res = cur.execute("SELECT * FROM movies")
`;
await exec(sql);
element.innerHTML += `<br/><pre>\nHow about some SQL?\n${sql}</pre><br/>${await repr(
"list(res)"
)}`;
document.body.appendChild(element);
}