-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.js
40 lines (28 loc) · 1.09 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
async function loadPackages(pyodide) {
await pyodide.loadPackage(["micropip", "numpy", "pydantic"]);
}
async function loadVisualize(pyodide) {
let python_script = await fetch("/visualize.py").then((r) => r.text());
return await pyodide.runPythonAsync(python_script);
}
async function main() {
// Set default value for input
document.getElementById("input").value =
"Hello! Guess what? I'm running Python in your browser!\n\nMy name is Clara and I live in Berkeley, California.";
// Load Pyodide and packages
let pyodide = await loadPyodide();
await loadPackages(pyodide);
// Load visualize function
vis_fn = await loadVisualize(pyodide);
// Enable the form button and change label
document.getElementById("submit").disabled = false;
document.getElementById("submit").innerHTML = "Visualize";
// Add event listener to form
document.getElementById("form").addEventListener("submit", (e) => {
e.preventDefault();
let input = document.getElementById("input").value;
let html = vis_fn(input);
document.getElementById("output").innerHTML = html;
});
}
main();