Add a custom Python output handler

This commit is contained in:
Michael Droettboom 2018-04-25 13:00:25 -04:00
parent 8ee2dec166
commit 6701273754
1 changed files with 22 additions and 0 deletions

View File

@ -27,5 +27,27 @@ var languagePluginLoader = new Promise((resolve, reject) => {
document.body.appendChild(script);
};
wasmXHR.send(null);
if (window.iodide !== undefined) {
const py_output_handler = {
shouldHandle: (val) => {
return (typeof val === 'object' &&
val['$$'] !== undefined &&
val['$$']['ptrType']['name'] === 'Py*');
},
render: (val) => {
if (val.hasattr('to_html')) {
return new DOMParser().parseFromString(
val.getattr('to_html').call([], {}), 'text/html').body.firstChild;
} else {
let pre = document.createElement('pre');
pre.textContent = window.pyodide.repr(val);
return pre;
}
}
};
window.iodide.addOutputHandler(py_output_handler);
}
});
languagePluginLoader