mirror of https://github.com/pyodide/pyodide.git
Add a custom Python output handler
This commit is contained in:
parent
8ee2dec166
commit
6701273754
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue