-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (68 loc) · 2.7 KB
/
script.js
File metadata and controls
77 lines (68 loc) · 2.7 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
window.onload = async () => {
const runCodeBtn = document.getElementById('runCode');
const saveCodeBtn = document.getElementById('saveCode');
const fetchCodeBtn = document.getElementById('fetchCode');
const output = document.getElementById('output');
const fetchCodeIdInput = document.getElementById('fetchCodeId');
// Initialize Pyodide
let pyodide = await loadPyodide({
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.18.1/full/"
});
// Initialize Ace Editor
const editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/python");
// Run Python code in the browser
// Run Python code in the browser
// Run Python code in the browser
runCodeBtn.addEventListener('click', async () => {
output.textContent = ''; // Clear previous output
try {
let code = editor.getValue();
// Redirect the output
pyodide.stdout = (text) => { output.textContent += text; };
pyodide.stderr = (text) => { output.textContent += text; };
await pyodide.runPythonAsync(code);
output.textContent = pyodide.stdout
// If there was no output, say 'No output'
// if (output.textContent.trim() === '') {
// output.textContent = 'No output';
// }
} catch (error) {
output.textContent = `Error: ${error.message}`;
} finally {
// Reset stdout and stderr
pyodide.stdout = console.log;
pyodide.stderr = console.error;
}
});
// Save code to the backend
saveCodeBtn.addEventListener('click', async () => {
let code = editor.getValue();
let response = await fetch('http://localhost:3000/save', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ code: code })
});
let data = await response.json();
if (data.id) {
let shareableLink = `http://localhost:3000/snippet/${data.id}`;
output.innerHTML = `Code saved. Shareable link: <a href="${shareableLink}" target="_blank">${shareableLink}</a>`;
} else {
output.textContent = `Error: ${data.message}`;
}
});
// Fetch code by ID from the backend
fetchCodeBtn.addEventListener('click', async () => {
let id = fetchCodeIdInput.value;
let response = await fetch(`http://localhost:3000/snippet/${id}`);
let data = await response.json();
if (data.code) {
editor.setValue(data.code, -1); // -1 prevents selecting all text after setting new value
} else {
output.textContent = `Error: ${data.message}`;
}
});
};