Skip to content

Commit e6470a2

Browse files
committed
POC Monaco with Pyodide; but dependency bug that oddly appears at times?
1 parent 81ea3e3 commit e6470a2

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
</head>
5+
<body>
6+
<script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.46.0/min/vs/loader.js"></script>
7+
<script type="module" id="monaco-editor-init">
8+
// Configure the Monaco Editor's loader
9+
require.config({
10+
paths: {
11+
'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.46.0/min/vs'
12+
}
13+
});
14+
</script>
15+
16+
<!--
17+
Example pyodide load code from:
18+
https://pyodide.org/en/stable/usage/quickstart.html#alternative-example
19+
-->
20+
<p>
21+
You can execute any Python code. Just enter something in the box below and
22+
click the button.
23+
</p>
24+
<input id="code" value="sum([1, 2, 3, 4, 5])" />
25+
<button onclick="evaluatePython()">Run</button>
26+
<br />
27+
<br />
28+
<div>Output:</div>
29+
<textarea id="output" style="width: 100%;" rows="6" disabled></textarea>
30+
31+
32+
<script type="module">
33+
const output = document.getElementById("output");
34+
const code = document.getElementById("code");
35+
36+
function addToOutput(s) {
37+
output.value += ">>>" + code.value + "\n" + s + "\n";
38+
}
39+
40+
output.value = "Initializing...\n";
41+
42+
// init Pyodide
43+
async function main() {
44+
const dpyodide = import("https://cdn.jsdelivr.net/npm/pyodide@0.25.0/pyodide.mjs").then(
45+
async ({loadPyodide}) => {
46+
const pyodideProcess = await loadPyodide(
47+
{indexURL: "https://cdn.jsdelivr.net/npm/pyodide@0.25.0/"}
48+
);
49+
return pyodideProcess;
50+
}
51+
);
52+
output.value += "Ready!\n";
53+
return dpyodide;
54+
}
55+
56+
let pyodideReadyPromise = main();
57+
58+
globalThis.evaluatePython = async function() {
59+
let dpyodide = await pyodideReadyPromise;
60+
try {
61+
let output = dpyodide.runPython(code.value);
62+
addToOutput(output);
63+
} catch (err) {
64+
addToOutput(err);
65+
}
66+
}
67+
</script>
68+
69+
<!--
70+
Attempt to place a monaco editor
71+
-->
72+
<div id="m-container" style="height:500px">
73+
74+
</div>
75+
76+
<script type="module">
77+
// Load the Monaco Editor and create an instance
78+
let editor;
79+
require(['vs/editor/editor.main'], function() {
80+
editor = monaco.editor.create(document.getElementById('m-container'), {
81+
value: [
82+
'def x():',
83+
'\t print("Hello world!")',
84+
].join('\n'),
85+
language: 'python'
86+
});
87+
});
88+
</script>
89+
90+
91+
</body>
92+
</html>

0 commit comments

Comments
 (0)