-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec-python.js
More file actions
210 lines (186 loc) · 9.67 KB
/
exec-python.js
File metadata and controls
210 lines (186 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// ============================================
// exec-python.js — Python Executable Blocks (Pyodide WASM)
// Extracted from executable-blocks.js
// ============================================
(function (M) {
'use strict';
var escapeHtml = M._exec.escapeHtml;
var _pyodideInstance = null;
var _pyodideLoading = false;
var _pyodideCallbacks = [];
function getPyodide(onReady, onProgress) {
if (_pyodideInstance) { onReady(_pyodideInstance); return; }
_pyodideCallbacks.push(onReady);
if (_pyodideLoading) return;
_pyodideLoading = true;
if (onProgress) onProgress('Loading Python runtime (~11 MB)...');
var script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.js';
script.onload = function () {
if (onProgress) onProgress('Initializing Python...');
window.loadPyodide({
indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.26.4/full/'
}).then(function (pyodide) {
_pyodideInstance = pyodide;
_pyodideLoading = false;
pyodide.runPython([
'import sys',
'from io import StringIO',
''
].join('\n'));
var cbs = _pyodideCallbacks.splice(0);
cbs.forEach(function (cb) { cb(pyodide); });
}).catch(function (err) {
_pyodideLoading = false;
console.error('Pyodide load failed:', err);
var cbs = _pyodideCallbacks.splice(0);
cbs.forEach(function (cb) { cb(null, err); });
});
};
script.onerror = function () {
_pyodideLoading = false;
var cbs = _pyodideCallbacks.splice(0);
cbs.forEach(function (cb) { cb(null, new Error('Failed to load Pyodide CDN script')); });
};
document.head.appendChild(script);
}
M.addPythonBlockToolbars = function () {
M.markdownPreview.querySelectorAll('.executable-python-container').forEach(function (container) {
if (container.querySelector('.code-block-toolbar')) return;
var toolbar = document.createElement('div');
toolbar.className = 'code-block-toolbar';
toolbar.setAttribute('aria-label', 'Python sandbox actions');
var btnRun = document.createElement('button');
btnRun.className = 'code-toolbar-btn python-run-btn';
btnRun.title = 'Run in Python sandbox (Pyodide)';
btnRun.setAttribute('aria-label', 'Run Python');
btnRun.innerHTML = '<i class="bi bi-play-fill"></i> Run';
btnRun.addEventListener('click', function () { executePythonBlock(container, btnRun); });
var btnCopy = document.createElement('button');
btnCopy.className = 'code-toolbar-btn code-copy-btn';
btnCopy.title = 'Copy code';
btnCopy.setAttribute('aria-label', 'Copy code');
btnCopy.innerHTML = '<i class="bi bi-clipboard"></i>';
btnCopy.addEventListener('click', function () {
var codeEl = container.querySelector('code');
if (!codeEl) return;
navigator.clipboard.writeText(codeEl.textContent).then(function () {
btnCopy.innerHTML = '<i class="bi bi-check-lg"></i>';
setTimeout(function () { btnCopy.innerHTML = '<i class="bi bi-clipboard"></i>'; }, 1500);
}).catch(function () {
btnCopy.innerHTML = '<i class="bi bi-x-lg"></i>';
setTimeout(function () { btnCopy.innerHTML = '<i class="bi bi-clipboard"></i>'; }, 1500);
});
});
toolbar.appendChild(btnRun);
toolbar.appendChild(btnCopy);
container.insertBefore(toolbar, container.firstChild);
});
};
function executePythonBlock(container, btnRun) {
var codeEl = container.querySelector('code');
if (!codeEl) return;
var code = codeEl.textContent;
var outputEl = container.querySelector('.code-output');
if (!outputEl) {
outputEl = document.createElement('div');
outputEl.className = 'code-output python-output';
container.appendChild(outputEl);
}
btnRun.disabled = true;
btnRun.innerHTML = '<i class="bi bi-hourglass-split"></i> Loading...';
outputEl.style.display = 'block';
outputEl.innerHTML = '<span class="code-output-loading"><i class="bi bi-arrow-repeat"></i> Loading Python runtime...</span>';
getPyodide(function (pyodide, err) {
if (!pyodide || err) {
outputEl.innerHTML = '<span class="code-output-error">Failed to load Python: ' + escapeHtml((err && err.message) || 'Unknown error') + '</span>';
btnRun.disabled = false;
btnRun.innerHTML = '<i class="bi bi-play-fill"></i> Run';
return;
}
btnRun.innerHTML = '<i class="bi bi-hourglass-split"></i> Running...';
outputEl.innerHTML = '<span class="code-output-loading"><i class="bi bi-arrow-repeat"></i> Executing...</span>';
setTimeout(function () {
try {
pyodide.runPython('sys.stdout = StringIO()\nsys.stderr = StringIO()');
var usesMpl = /\bimport\s+matplotlib\b|\bfrom\s+matplotlib\b|\bplt\.\b/.test(code);
if (usesMpl) {
try {
pyodide.runPython("import micropip");
pyodide.runPython("import matplotlib\nmatplotlib.use('AGG')");
} catch (e) { /* ignore */ }
}
pyodide.runPython(code);
var stdout = pyodide.runPython('sys.stdout.getvalue()');
var stderr = pyodide.runPython('sys.stderr.getvalue()');
var outputHtml = '';
if (usesMpl) {
try {
pyodide.runPython([
'import matplotlib.pyplot as _plt',
'import base64 as _b64',
'from io import BytesIO as _BytesIO',
'_mdv_figs = []',
'for _fig_num in _plt.get_fignums():',
' _buf = _BytesIO()',
' _plt.figure(_fig_n
7333
um).savefig(_buf, format="png", dpi=100, bbox_inches="tight")',
' _buf.seek(0)',
' _mdv_figs.append(_b64.b64encode(_buf.read()).decode())',
' _buf.close()',
'_plt.close("all")'
].join('\n'));
var figs = pyodide.runPython('_mdv_figs').toJs();
if (figs && figs.length > 0) {
figs.forEach(function (b64) {
outputHtml += '<div class="python-plot-output"><img src="data:image/png;base64,' + b64 + '" alt="matplotlib plot" style="max-width:100%;border-radius:6px;margin:4px 0" /></div>';
});
}
} catch (e) { /* ignore plot errors */ }
}
if (stdout) outputHtml += '<span class="code-output-stdout">' + escapeHtml(stdout) + '</span>';
if (stderr) outputHtml += '<span class="code-output-stderr">' + escapeHtml(stderr) + '</span>';
if (!outputHtml) outputHtml = '<span class="code-output-muted">(no output)</span>';
outputEl.innerHTML = outputHtml;
} catch (runErr) {
outputEl.innerHTML = '<span class="code-output-error">Error: ' + escapeHtml(runErr.message) + '</span>';
} finally {
btnRun.disabled = false;
btnRun.innerHTML = '<i class="bi bi-play-fill"></i> Run';
}
}, 50);
}, function (msg) {
outputEl.innerHTML = '<span class="code-output-loading"><i class="bi bi-arrow-repeat"></i> ' + escapeHtml(msg) + '</span>';
});
}
// --- Register runtime adapter for exec-controller ---
var pythonAdapter = {
execute: function (source) {
return new Promise(function (resolve, reject) {
getPyodide(function (pyodide, err) {
if (!pyodide || err) {
reject(err || new Error('Failed to load Pyodide'));
return;
}
try {
pyodide.runPython('sys.stdout = StringIO()\nsys.stderr = StringIO()');
pyodide.runPython(source);
var stdout = pyodide.runPython('sys.stdout.getvalue()');
var stderr = pyodide.runPython('sys.stderr.getvalue()');
var output = '';
if (stdout) output += stdout;
if (stderr) output += (output ? '\n' : '') + stderr;
resolve(output || '(no output)');
} catch (e) {
reject(e);
}
});
});
}
};
if (M._execRegistry) {
M._execRegistry.registerRuntime('python', pythonAdapter);
} else {
if (!M._pendingRuntimeAdapters) M._pendingRuntimeAdapters = [];
M._pendingRuntimeAdapters.push({ key: 'python', adapter: pythonAdapter });
}
})(window.MDView);