function outf(text) {
var mypre = document.getElementById("output"); //
mypre.innerHTML = mypre.innerHTML + text; //文字输出
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
function runit() {
console.log("runit is going")
var prog = document.getElementById("yourcode").value; //读取用户输入的内容
var mypre = document.getElementById("output"); //获取输出框
mypre.innerHTML = '';
Sk.pre = "output";
Sk.configure({ output: outf, read: builtinRead, __future__: Sk.python3 });
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
var myPromise = Sk.misceval.asyncToPromise(function () {
return Sk.importMainWithBody("", false, prog, true);
});
myPromise.then(function (mod) {
console.log('success'); //执行成功,显示success
},
function (err) {
console.log(err.toString()); //执行失败,在控制台显示 err
});
}
Try This
Run