-
Notifications
You must be signed in to change notification settings - Fork 39
/
Python.js
41 lines (39 loc) 路 1.19 KB
/
Python.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
41
import Sk from 'skulpt';
export default function CreatePythonDoc(prog) {
function outf(text) {
const mypre = document.getElementById('inner');
let received;
if (text !== '\\n') {
received = true;
} else if (received) {
received = false;
} else {
received = true;
}
if (received) {
mypre.value += `> ${text}`;
}
if (mypre.scrollTop >= mypre.scrollHeight - mypre.offsetHeight - mypre.offsetHeight) {
mypre.scrollTop = mypre.scrollHeight;
}
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles.files[x] === undefined) {
throw Error(`File not found: '${x}'`);
}
return Sk.builtinFiles.files[x];
}
Sk.pre = 'output';
Sk.configure({ output: outf, read: builtinRead, __future__: Sk.python3 });
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'my-canvas';
Sk.misceval
.asyncToPromise(() => Sk.importMainWithBody('<stdin>', false, prog, true))
.catch((err) => {
const b = document.getElementById('output');
if (b) {
b.style.display = 'block';
}
const a = document.getElementById('inner');
a.value += `\nERROR: ${err.toString()}`;
});
}