I have a problem.
I need to redirect all Brython output to a <textarea>
element.
Here's my HTML-file.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython_stdlib.js"></script>
</head>
<body onload="brython(1)">
<textarea id="console"></textarea>
<script type="text/python3">
print('test')
</script>
</body>
</html>
I know that by default, all output (sys.stdout and sys.stderr) in Brython goes to the console.
I tried this:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython_stdlib.js"></script>
</head>
<body onload="brython(1)">
<textarea id="console"></textarea>
<script type="text/python3">
from browser import document
import sys
sys.stdout = document['console']
</script>
<script type="text/python3">
print('test')
</script>
</body>
</html>
But it gives me an AttributeError.
Traceback (most recent call last):
module __main__2 line 1
print('test')
AttributeError: 'TEXTAREA' object has no attribute 'write'
I need to use print()
, because the user will enter the code, and the code's result will be printed to <textarea>
.
I tried to search for examples, but I did not found any.