How To Create A Simple Python Code Runner With Brython
I'm creating simple code editor which can run python code in & show the output & error messages. Currently I'm able to run a python code but the problem is the output is sh
Solution 1:
Use brython-runner. You can run Python code in a string and handle standard output and error with custom callback functions. It runs the code with a Brython instance in the web worker.
<scriptsrc="https://cdn.jsdelivr.net/gh/pythonpad/brython-runner/lib/brython-runner.bundle.js"></script><script>const runner = newBrythonRunner({
stdout: {
write(content) {
// Show output messages here.console.log('StdOut: ' + content);
},
flush() {},
},
stderr: {
write(content) {
// Show error messages here.console.error('StdErr: ' + content);
},
flush() {},
},
stdin: {
asyncreadline() {
var userInput = prompt();
console.log('Received StdIn: ' + userInput);
return userInput;
},
}
});
runner.runCode('print("hello world")');
</script>
Disclaimer: I'm the author of this module. ;)
Solution 2:
try it:
<bodyonload="brython()"><scripttype="text/python">from browser importdocument, windowimport traceback
def run(event):
try:
exec(document['code'].value)
except Exception:
error_message=traceback.format_exc()
document['error_message_textarea'].value=error_message
document['run'].bind('click',run)
</script><inputid='code'value='print(123+"OK")'></input><buttonid='run'>
RUN
</button><br><textareaid='error_message_textarea'style='color:red;width: 300px; height: 300px;'></textarea></body>
Post a Comment for "How To Create A Simple Python Code Runner With Brython"