From 685e6028f1cb490b52dad0b4b2cc857f4d3df674 Mon Sep 17 00:00:00 2001 From: sanchezcarlosjr <24639141+sanchezcarlosjr@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:28:39 -0800 Subject: [PATCH] feat: load packages --- .../notebook/cellTypes/languages/Python.ts | 66 ++++++++++++++----- src/app/notebook/cellTypes/stringToHTML.ts | 2 +- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/app/notebook/cellTypes/languages/Python.ts b/src/app/notebook/cellTypes/languages/Python.ts index d77300a..8acd4c4 100644 --- a/src/app/notebook/cellTypes/languages/Python.ts +++ b/src/app/notebook/cellTypes/languages/Python.ts @@ -3,15 +3,52 @@ import {Observable, shareReplay} from "rxjs"; import {Extension} from "@codemirror/state"; import {python} from "@codemirror/lang-python"; +/** + * @param {string} key + * @param {any} value + */ +function jsonStringifyToObjectReplacer(key: string, value: any) { + if (value && value.toObject) { + return value.toObject(); + } + if (value && value.toJs) { + console.log(value.toString()); + return value.toString(); + } + if (value && value.toJSON) { + return value.toJSON(); + } + return value; +} + const pyodide = new Observable<{ runPython: (code: string) => any setStdout(options: { batched: (input: string) => void }): void; runPythonAsync(s: string): Promise; + loadPackagesFromImports(code: string): Promise; }>(subscriber => { // @ts-ignore loadPyodide().then(async (instance) => { await instance.loadPackage("micropip"); + await instance.loadPackage('pyodide-http'); + await instance.runPythonAsync(` +from js import document + +def create_root_element(self): + return document.getElementById(BLOCK_ID).children[1] + +def display(f): + f.canvas.create_root_element = create_root_element.__get__(create_root_element, f.canvas.__class__) + f.canvas.show() + return "
" +import micropip + +await micropip.install('requests') + +import pyodide_http +pyodide_http.patch_all() +`) // @ts-ignore globalThis.pyodide = instance; subscriber.next(instance); @@ -37,23 +74,20 @@ export class Python extends Language { } }); const code = `BLOCK_ID = "${this.editorJsTool?.block?.id}" -from js import document -def create_root_element(self): - return document.getElementById(BLOCK_ID).children[1] -def display(f): - f.canvas.create_root_element = create_root_element.__get__(create_root_element, f.canvas.__class__) - f.canvas.show() - return "
" ${this.mostRecentCode}`; - instance.runPythonAsync(code).then((output: any) => { - if (output !== undefined) { - this.write(output); - } - this.stop(); - }).catch((e: any) => { - this.rewrite(`
${e.message}
`); - this.stop(); - }); + (async () => { + await instance.loadPackagesFromImports(code); + await instance.runPythonAsync(code) + .then((output: any) => { + if (output !== undefined) { + this.write(JSON.stringify(output, jsonStringifyToObjectReplacer)); + } + this.stop(); + }).catch((e: any) => { + this.rewrite(`
${e.message}
`); + this.stop(); + }); + })().then() }); return true; } diff --git a/src/app/notebook/cellTypes/stringToHTML.ts b/src/app/notebook/cellTypes/stringToHTML.ts index 30a2804..c072e25 100644 --- a/src/app/notebook/cellTypes/stringToHTML.ts +++ b/src/app/notebook/cellTypes/stringToHTML.ts @@ -6,6 +6,6 @@ */ export function stringToHTML(str: string): ChildNode { const parser = new DOMParser(); - const doc = parser.parseFromString(str, 'text/html'); + const doc = parser.parseFromString(str.replace(/[\u00A0-\u9999<>&]/g, i => '&#'+i.charCodeAt(0)+';'), 'text/html'); return doc.body.firstChild as ChildNode; }