|
@@ -111,88 +111,7 @@
|
|
|
};
|
|
|
|
|
|
const executePython = async (code) => {
|
|
|
- if (!code.includes('input')) {
|
|
|
- executePythonAsWorker(code);
|
|
|
- } else {
|
|
|
- result = null;
|
|
|
- stdout = null;
|
|
|
- stderr = null;
|
|
|
-
|
|
|
- executing = true;
|
|
|
-
|
|
|
- document.pyodideMplTarget = document.getElementById(`plt-canvas-${id}`);
|
|
|
-
|
|
|
- let pyodide = await loadPyodide({
|
|
|
- indexURL: '/pyodide/',
|
|
|
- stdout: (text) => {
|
|
|
- console.log('Python output:', text);
|
|
|
-
|
|
|
- if (stdout) {
|
|
|
- stdout += `${text}\n`;
|
|
|
- } else {
|
|
|
- stdout = `${text}\n`;
|
|
|
- }
|
|
|
- },
|
|
|
- stderr: (text) => {
|
|
|
- console.log('An error occurred:', text);
|
|
|
- if (stderr) {
|
|
|
- stderr += `${text}\n`;
|
|
|
- } else {
|
|
|
- stderr = `${text}\n`;
|
|
|
- }
|
|
|
- },
|
|
|
- packages: ['micropip']
|
|
|
- });
|
|
|
-
|
|
|
- try {
|
|
|
- const micropip = pyodide.pyimport('micropip');
|
|
|
-
|
|
|
- // await micropip.set_index_urls('https://pypi.org/pypi/{package_name}/json');
|
|
|
-
|
|
|
- let packages = [
|
|
|
- code.includes('requests') ? 'requests' : null,
|
|
|
- code.includes('bs4') ? 'beautifulsoup4' : null,
|
|
|
- code.includes('numpy') ? 'numpy' : null,
|
|
|
- code.includes('pandas') ? 'pandas' : null,
|
|
|
- code.includes('matplotlib') ? 'matplotlib' : null,
|
|
|
- code.includes('sklearn') ? 'scikit-learn' : null,
|
|
|
- code.includes('scipy') ? 'scipy' : null,
|
|
|
- code.includes('re') ? 'regex' : null,
|
|
|
- code.includes('seaborn') ? 'seaborn' : null,
|
|
|
- code.includes('sympy') ? 'sympy' : null,
|
|
|
- code.includes('tiktoken') ? 'tiktoken' : null
|
|
|
- ].filter(Boolean);
|
|
|
-
|
|
|
- console.log(packages);
|
|
|
- await micropip.install(packages);
|
|
|
-
|
|
|
- result = await pyodide.runPythonAsync(`from js import prompt
|
|
|
-def input(p):
|
|
|
- return prompt(p)
|
|
|
-__builtins__.input = input`);
|
|
|
-
|
|
|
- result = await pyodide.runPython(code);
|
|
|
-
|
|
|
- if (!result) {
|
|
|
- result = '[NO OUTPUT]';
|
|
|
- }
|
|
|
-
|
|
|
- console.log(result);
|
|
|
- console.log(stdout);
|
|
|
- console.log(stderr);
|
|
|
-
|
|
|
- const pltCanvasElement = document.getElementById(`plt-canvas-${id}`);
|
|
|
-
|
|
|
- if (pltCanvasElement?.innerHTML !== '') {
|
|
|
- pltCanvasElement.classList.add('pt-4');
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
- console.error('Error:', error);
|
|
|
- stderr = error;
|
|
|
- }
|
|
|
-
|
|
|
- executing = false;
|
|
|
- }
|
|
|
+ executePythonAsWorker(code);
|
|
|
};
|
|
|
|
|
|
const executePythonAsWorker = async (code) => {
|