Commit 9c428be4 authored by Boxiang Sun's avatar Boxiang Sun

erp5_notebook: Support standard pyodide notebook grammar

Simply ignore the section "meta" and "plugin", support "%%code {"language" : "py"}"
instead "%% py"
parent 515c3a84
/*global window, console, RSVP, document, URL, eval, XMLHttpRequest, marked, pyodide */ /*global window, console, RSVP, document, URL, eval, XMLHttpRequest, marked, pyodide, WebAssembly, fetch*/
/*jslint nomen: true, indent: 2, maxerr: 3 */ /*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window) { (function (window) {
"use strict"; "use strict";
...@@ -11,15 +11,16 @@ ...@@ -11,15 +11,16 @@
this._line_list = line_list; this._line_list = line_list;
}, },
split_line_regex = /[\r\n|\n|\r]/, split_line_regex = /[\r\n|\n|\r]/,
cell_type_regexp = /^\%\% (\w+)$/, cell_type_regexp = /^\%\% (\w+)\b/,
is_pyodide_loaded = false; language_type_regexp = /\{[\S\s]+\}/,
is_pyodide_loaded = false,
Module = {};
window.xiodide = new IODide(); window.iodide = new IODide();
IODide.prototype.addOutputHandler = function () { IODide.prototype.addOutputHandler = function () {
return; return;
}; };
var Module = {};
// Copied from jio // Copied from jio
function ajax(param) { function ajax(param) {
...@@ -71,6 +72,7 @@ ...@@ -71,6 +72,7 @@
len = line_list.length, len = line_list.length,
current_line, current_line,
current_type, current_type,
language_type,
current_text_list, current_text_list,
next_type, next_type,
cell_list = []; cell_list = [];
...@@ -87,6 +89,15 @@ ...@@ -87,6 +89,15 @@
next_type = current_line.match(cell_type_regexp); next_type = current_line.match(cell_type_regexp);
if (next_type) { if (next_type) {
// New type detexted // New type detexted
if (next_type[1] === 'code') {
// language detected
try {
language_type = JSON.parse(current_line.match(language_type_regexp)).language;
} catch (e) {
throw e;
}
next_type[1] = next_type[1] + '_' + language_type;
}
pushNewCell(); pushNewCell();
current_type = next_type; current_type = next_type;
current_text_list = []; current_text_list = [];
...@@ -256,37 +267,24 @@ ...@@ -256,37 +267,24 @@
}); });
} }
function loadPyodide() { function loadPyodide(info, receiveInstance) {
let wasm_promise = WebAssembly.compileStreaming(fetch(`pyodide.asm.wasm`)); var queue = new RSVP.Queue();
Module.instantiateWasm = function (info, receiveInstance) { queue.push(function () {
wasm_promise return WebAssembly.compileStreaming(fetch("pyodide.asm.wasm"));
.then(module => WebAssembly.instantiate(module, info)) })
.then(instance => receiveInstance(instance)); .push(function (module) {
return {}; return WebAssembly.instantiate(module, info);
}; })
window.Module = Module; .push(function (instance) {
} return receiveInstance(instance);
});
function executePyCell(line_list) { return queue;
var result_text, code_text = line_list.join('\n');
try {
result_text = pyodide.runPython(line_list.join('\n'));
} catch (e) {
result_text = e.message;
}
if (typeof(result_text) === 'undefined') {
result_text = 'undefined';
}
renderCodeblock(result_text, 'python');
} }
function renderCodeblock(result_text, language) { function renderCodeblock(result_text) {
var div = document.createElement('div'), var div = document.createElement('div'),
pre = document.createElement('pre'), pre = document.createElement('pre'),
result = document.createElement('code'); result = document.createElement('code');
div.style.border = '1px solid #C3CCD0'; div.style.border = '1px solid #C3CCD0';
div.style.margin = '40px 10px'; div.style.margin = '40px 10px';
div.style.paddingLeft = '10px'; div.style.paddingLeft = '10px';
...@@ -299,23 +297,32 @@ ...@@ -299,23 +297,32 @@
} }
} }
function executePyCell(line_list) {
var result_text, code_text = line_list.join('\n');
try {
result_text = pyodide.runPython(code_text);
} catch (e) {
result_text = e.message;
}
renderCodeblock(result_text, 'python');
}
function pyodideSetting() { function pyodideSetting() {
window.pyodide = pyodide(Module); window.pyodide = pyodide(Module);
var defer = RSVP.defer(), promise=defer.promise; var defer = RSVP.defer(), promise = defer.promise;
console.log("Setting postRun");
window.pyodide.loadPackage = pyodideLoadPackage;
Module.postRun = defer.resolve; Module.postRun = defer.resolve;
promise.then(function() { promise.then(function () {
console.log("postRun get called"); console.log("postRun get called");
}); });
console.log("Setting postRun finished");
return defer.promise; return defer.promise;
} }
function executeCell(cell) { function executeCell(cell) {
if (cell._type === 'raw') { if (['raw', 'meta', 'plugin'].indexOf(cell._type) !== -1) {
// Do nothing... // Do nothing...
return; return;
} }
...@@ -334,30 +341,37 @@ ...@@ -334,30 +341,37 @@
if (cell._type === 'css') { if (cell._type === 'css') {
return executeCssCell(cell._line_list); return executeCssCell(cell._line_list);
} }
if (cell._type === 'py') { if (cell._type === 'code_py') {
console.log("Py cell"); if (cell._line_list.length === 0) {
// empty block, do nothing.
return;
}
console.log(is_pyodide_loaded);
var queue = new RSVP.Queue(); var queue = new RSVP.Queue();
if (!is_pyodide_loaded) { if (!is_pyodide_loaded) {
console.log("Loading pyodide"); console.log("Loading pyodide");
queue.push(function() { queue.push(function () {
console.log("Loading webassembly module"); console.log("Loading webassembly module");
return loadPyodide(); Module.instantiateWasm = loadPyodide;
}) window.Module = Module;
.push(function () {
console.log("Loading pyodide.asm.data.js");
return loadJSResource(`pyodide.asm.data.js`);
}) })
.push(function () { .push(function () {
console.log("Loading pyodide.asm.js"); console.log("Loading pyodide.asm.data.js");
return loadJSResource('pyodide.asm.js'); return loadJSResource('pyodide.asm.data.js');
}) })
.push(function () { .push(function () {
console.log("Prepare to set postRun and pyodide"); console.log("Loading pyodide.asm.js");
return pyodideSetting(); return loadJSResource('pyodide.asm.js');
}); })
.push(function () {
console.log("Delay the execution");
return RSVP.delay();
})
.push(function () {
console.log("Prepare to set postRun and pyodide");
return pyodideSetting();
});
is_pyodide_loaded = true; is_pyodide_loaded = true;
} }
console.log("Fuck!"); console.log("Fuck!");
......
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