Skip to content

Commit ec83f48

Browse files
committed
fix: Escape Pyodide output, improve error handling
Issue-87: #87
1 parent 70609c1 commit ec83f48

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/markdown_exec/pyodide.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ async function evaluatePython(pyodide, editor, output, session) {
2222
try {
2323
result = await pyodide.runPythonAsync(code, { globals: getSession(session, pyodide) });
2424
} catch (error) {
25-
writeOutput(output, error);
25+
writeOutput(output, new Option(error.toString()).innerHTML);
2626
}
27-
if (result) writeOutput(output, result);
27+
if (result) writeOutput(output, new Option(result).innerHTML);
2828
hljs.highlightElement(output);
2929
}
3030

@@ -91,11 +91,17 @@ async function setupPyodide(idPrefix, install = null, themeLight = 'tomorrow', t
9191
writeOutput(output, "Initializing...");
9292
let pyodide = await pyodidePromise;
9393
if (install && install.length) {
94-
micropip = pyodide.pyimport("micropip");
95-
for (const package of install)
96-
await micropip.install(package);
94+
try {
95+
micropip = pyodide.pyimport("micropip");
96+
for (const package of install)
97+
await micropip.install(package);
98+
clearOutput(output);
99+
} catch (error) {
100+
clearOutput(output);
101+
writeOutput(output, `Could not install one or more packages: ${install.join(", ")}\n`);
102+
writeOutput(output, new Option(error.toString()).innerHTML);
103+
}
97104
}
98-
clearOutput(output);
99105
run.onclick = () => evaluatePython(pyodide, editor, output, session);
100106
clear.onclick = () => clearOutput(output);
101107
output.parentElement.parentElement.addEventListener("keydown", (event) => {

0 commit comments

Comments
 (0)