-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyodide_repl.html
59 lines (49 loc) · 2.01 KB
/
pyodide_repl.html
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pyodide v0.21.3 REPL example</title>
<script src="https://cdn.jsdelivr.net/pyodide/v0.21.3/full/pyodide.js"></script>
</head>
<body>
<div id="simple-example">
Output:
<textarea id='output' style='width: 100%;' rows='10' disabled></textarea>
<textarea id='code' rows='3'>
import numpy as np
np.ones((10,))
</textarea>
<button id='run' onclick='evaluatePython()'>Run</button>
<div>You can execute any Python code. Just enter something in the box above and click the button. </div>
<script type='text/javascript'>
const output = document.getElementById("output")
const code = document.getElementById("code")
function addToOutput(s) {
output.value += `${s}\n`
output.scrollTop = output.scrollHeight
}
async function evaluatePython() {
addToOutput(`>>>${code.value}`)
try {
// Since pyodide 0.18.0, you must call loadPackagesFromImports()
// to import any python packages referenced via import statements in your code.
// This function will no longer do it for you.
await pyodide.loadPackagesFromImports(code.value, addToOutput, addToOutput)
let result = await pyodide.runPythonAsync(code.value)
addToOutput(`${result}`)
}
catch (e) {
addToOutput(`${e}`)
}
code.value = ''
}
(async () => { // enable await
output.value = 'Initializing...\n'
// init Pyodide
window.pyodide = await loadPyodide({ stdout: addToOutput, stderr: addToOutput }) // redirect stdout and stderr to addToOutput
output.value += 'Ready!\n'
})(); // execute immediately
</script>
</div>
</body>
</html>