Skip to content

Commit

Permalink
Merge pull request #16 from meet244/main
Browse files Browse the repository at this point in the history
Displays correct output
  • Loading branch information
codingkatty authored Oct 12, 2024
2 parents e5a524f + 15a4445 commit f6766d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion learn.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h2>Python Lessons</h2>
<div id="editor-container">
<div id="editor"></div>
<div id="terminal-container">
<div id="terminal"></div>
<textarea id="terminal" readonly></textarea>
</div>
</div>
<div id="button-container">
Expand Down
32 changes: 29 additions & 3 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,43 @@ print(greet("Alice"))`, 1);
}

async function runCode() {
// Clear the terminal
clearTerminal();

// Get the code from the editor
const code = editor.getValue();
const terminal = document.getElementById('terminal');
terminal.innerHTML += 'Running code...\n';

// Run the code using Pyodide
try {
let output = await pyodide.runPythonAsync(code);
terminal.innerHTML += `Output:\n${output}\n`;
await pyodide.runPythonAsync(`
import sys
from io import StringIO
# Create a buffer to capture stdout
output_buffer = StringIO()
sys.stdout = output_buffer
try:
# Run the user code
${code.split('\n').map(line => ' ' + line).join('\n')}
except Exception as e:
print(f"Error: {e}")
# Reset stdout and get the output
sys.stdout = sys.__stdout__
output = output_buffer.getvalue()
output
`).then(output => {
// console.log(output);
terminal.innerHTML += output;
});
} catch (err) {
terminal.innerHTML += `Error:\n${err}\n`;
}
}


function clearTerminal() {
document.getElementById('terminal').innerHTML = '';
}
Expand Down

0 comments on commit f6766d6

Please sign in to comment.