Bind console

This commit is contained in:
Romain de Laage 2021-04-03 12:02:40 +02:00
parent d53cf997a9
commit c6804949c5
Signed by: rdelaage
GPG Key ID: 534845FADDF0C329
1 changed files with 28 additions and 0 deletions

View File

@ -20,13 +20,41 @@
<footer>
Romain de Laage - CC0
</footer>
<script type="text/python" id="pyinit">
import sys
from browser import document
# Define new class to bind html element with std files
class Stdfile() :
def __init__(self, elmtId, error = False):
self.elmt = document[elmtId]
self.error = error
def write(self, text):
if self.error == True:
self.elmt.html = self.elmt.html + '<div class="error">' + text.replace('\n', '<br />') + '</div>'
else:
self.elmt.html = self.elmt.html + '<div class="message">' + text.replace('\n', '<br />') + '</div>'
# Bind stderr and stdout with console element of page
sys.stderr = Stdfile('console', True)
sys.stdout = Stdfile('console')
</script>
<script type="text/python" id="pyclear">
from browser import document
# Clear console
document['console'].html = ''
</script>
<script type="text/python" id="pycode"></script>
<script src="assets/js/editor.js"></script>
<script>
const flask = getEditor("#code", "py")
brython({ids:["pyinit"]})
function run() {
document.getElementById("pycode").innerHTML = flask.getCode()
brython({ids:["pyclear"]})
brython({
debug:1,
ids:["pycode"]