提交 929e0040 编写于 作者: W weixin_68179602

Thu Aug 10 20:27:00 CST 2023 inscode

上级 d877dbfd
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<script>
const worker = new Worker("worker.js");
worker.postMessage('import sys\nsys.version');
const promise = new Promise(function (resolve, reject) {
worker.onmessage = function (event) {
var output = event.data;
console.log(output);
resolve();
};
});
promise.then(function () {
var scriptTag = document.querySelector('script[id="script"]');
var pythonCode = 'print(5/2)';
console.log(scriptTag.textContent);
worker.postMessage(pythonCode);
<script src="https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.js"></script>
</head>
worker.onmessage = function (event) {
var output = event.data;
console.log(output);
};
});
/*
(function(){
<body>
<script>
let interpreter;
})()
async function init() {
interpreter = await loadPyodide();
console.log(interpreter.runPython(`import sys\nsys.version`));
main();
}
function main() {
var scriptTag = document.querySelector('script[id="script"]');
var pythonCode = 'print(5/2)';
console.log(scriptTag.textContent);
worker.postMessage(pythonCode);
pyScriptRun();
}
worker.onmessage = function (event) {
var output = event.data;
console.log(output);
};
function pyScriptRun(){
document.querySelectorAll('script[type="text/python"]').forEach(scriptTag => {
interpreter.runPython(scriptTag.innerHTML);
});
}
window.onload = main;
*/
init();
</script>
</head>
<body>
<script id="script" type="text/py">print(5/2)</script>
<script type="text/python">print(5/2)</script>
<script type="text/python">print(5/3)</script>
<py-repl id="repl">
print(5/3)
</py-repl>
</body>
</html>
\ No newline at end of file
importScripts("https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.js");
let interpreter;
async function init(){
interpreter = await loadPyodide();
interpreter.runPython("import sys\nsys.version").then((result)=>{
console.log(result)
})
}
self.addEventListener('message', function(event) {
console.log("1")
var data = event.data;
switch (data.type) {
case 'init':
init();
break;
case 'event2':
console.log("event2");
break;
}
});
/*
async function run() {
let pyodide = await loadPyodide();
var pythonCode = self.pyCode; // 获取主线程发送的 Python 代码
......@@ -13,5 +33,5 @@ self.onmessage = function(event) {
console.log(self.pyCode)
run();
};
*/
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册