( code: string, printLn: (message: string) => void )
| 30 | "import sys\r\nimport io\r\nsys.stdout = io.StringIO()\r\n"; |
| 31 | |
| 32 | export const runPython = async ( |
| 33 | code: string, |
| 34 | printLn: (message: string) => void |
| 35 | ): Promise<void> => { |
| 36 | await loadFiles(["/Program Files/Pyodide/pyodide.js"]); |
| 37 | |
| 38 | if (!window.pyodide && window.loadPyodide) { |
| 39 | window.pyodide = await window.loadPyodide(config); |
| 40 | } |
| 41 | |
| 42 | if (window.pyodide) { |
| 43 | const getVersion = code === "ver" || code === "version"; |
| 44 | |
| 45 | try { |
| 46 | if (code.includes("import micropip")) { |
| 47 | await window.pyodide.loadPackage("micropip", { checkIntegrity: false }); |
| 48 | } |
| 49 | |
| 50 | let result = await window.pyodide.runPythonAsync( |
| 51 | getVersion ? versionCommand : captureStdOut + code |
| 52 | ); |
| 53 | |
| 54 | if (!result) { |
| 55 | result = await window.pyodide.runPythonAsync("sys.stdout.getvalue()"); |
| 56 | } |
| 57 | |
| 58 | if (result) printLn(result); |
| 59 | } catch (error) { |
| 60 | const { message } = error as PyError; |
| 61 | |
| 62 | if (message) printLn(message); |
| 63 | } |
| 64 | } |
| 65 | }; |
no test coverage detected