Commit 5f0fdf3f authored by Boxiang Sun's avatar Boxiang Sun

erp5_notebook: Refine the pyodide initialization

parent 23e13f5c
...@@ -20,29 +20,6 @@ ...@@ -20,29 +20,6 @@
return; return;
}; };
var Module = {}; var Module = {};
var initPyodide = new Promise((resolve, reject) => {
var postRunPromise = new Promise((resolve, reject) => {
Module.postRun = () => {
resolve();
};
});
Promise.all([ postRunPromise, ]).then(() => resolve());
let data_script = document.createElement('script');
data_script.src = `pyodide.asm.data.js`;
data_script.onload = (event) => {
let script = document.createElement('script');
script.src = `pyodide.asm.js`;
script.onload = () => {
window.pyodide = pyodide(Module);
};
document.head.appendChild(script);
};
document.head.appendChild(data_script);
});
// Copied from jio // Copied from jio
function ajax(param) { function ajax(param) {
...@@ -280,13 +257,14 @@ ...@@ -280,13 +257,14 @@
} }
function loadPyodide() { function loadPyodide() {
let wasm_promise = WebAssembly.compileStreaming(fetch(`pyodide.asm.wasm`)); let wasm_promise = WebAssembly.compileStreaming(fetch(`pyodide.asm.wasm`));
Module.instantiateWasm = function (info, receiveInstance) { Module.instantiateWasm = function (info, receiveInstance) {
wasm_promise wasm_promise
.then(module => WebAssembly.instantiate(module, info)) .then(module => WebAssembly.instantiate(module, info))
.then(instance => receiveInstance(instance)); .then(instance => receiveInstance(instance));
return {}; return {};
}; };
window.Module = Module;
} }
function executePyCell(line_list) { function executePyCell(line_list) {
...@@ -321,6 +299,19 @@ ...@@ -321,6 +299,19 @@
} }
} }
function pyodideSetting() {
return new RSVP.Promise(function (resolve, reject) {
window.pyodide = pyodide(Module);
var postRunPromise = new Promise((resolve, reject) => {
Module.postRun = () => {
resolve();
};
});
console.log("Setting postRun");
Promise.all([ postRunPromise, ]).then(() => resolve());
});
}
function executeCell(cell) { function executeCell(cell) {
if (cell._type === 'raw') { if (cell._type === 'raw') {
// Do nothing... // Do nothing...
...@@ -352,38 +343,27 @@ ...@@ -352,38 +343,27 @@
queue.push(function() { queue.push(function() {
console.log("Loading webassembly module"); console.log("Loading webassembly module");
return loadPyodide(); return loadPyodide();
}) })
.push(function () { .push(function () {
window.Module = Module;
console.log("WIndow Module");
console.log(window.Module);
console.log("Module");
console.log(Module);
})
.push(function () {
console.log("Prepare enter initPyodide");
return initPyodide;
});
is_pyodide_loaded = true;
/*
queue.push(function () {
console.log("Loading pyodide.asm.data.js"); console.log("Loading pyodide.asm.data.js");
return loadJSResource(`pyodide.asm.data.js`); return loadJSResource(`pyodide.asm.data.js`);
});*/ })
/* .push(function () {
queue.push(function () {
console.log("Loading pyodide.asm.js"); console.log("Loading pyodide.asm.js");
return loadJSResource('pyodide.asm.js'); return loadJSResource('pyodide.asm.js');
})
.push(function () {
console.log("Prepare to set postRun and pyodide");
return pyodideSetting();
}); });
*/ is_pyodide_loaded = true;
} }
console.log("Fuck!"); console.log("Fuck!");
queue.push(function () { queue.push(function () {
console.log("Executing Python cell"); console.log("Executing Python cell");
console.log(window.pyodide);
return executePyCell(cell._line_list); return executePyCell(cell._line_list);
}); });
return queue; return queue;
} }
return executeUnknownCellType(cell); return executeUnknownCellType(cell);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment