-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcode.js
42 lines (36 loc) · 1.07 KB
/
code.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
42
var editor = CodeMirror.fromTextArea(document.getElementById("input"), {
lineNumbers: true
});
// execute the placeholder input
$(document).ready(function () {
execute_python();
});
//perpetually update the output
editor.on("change", function () {
execute_python();
});
// executes python using skulpt
function execute_python() {
var prog = window.editor.getValue();
prog += "\n" + document.getElementById("python_code").value;
var output = document.getElementById("output");
output.innerHTML = "";
Sk.pre = "output";
Sk.configure({ output: outf, read: builtinRead });
try {
eval(Sk.importMainWithBody("<stdin>", false, prog));
}
catch (e) {
output = document.getElementById("output");
output.innerHTML = output.innerHTML + e.toString();
}
}
function outf(text) {
var output = document.getElementById("output");
output.innerHTML = output.innerHTML + text;
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}