from browser import window from io import StringIO import sys output = None # Global variable for the html output element src = None # Global variable for python src to be executed # Redirects stdout to output element. class cOutput: def write(self, data): output.innerHTML += "
"+str(data)+"
" output.style.color = 'black' def flush(self): pass # Redirects stderr to output element. class cError: def write(self, data): output.innerHTML += "
" + str(data) + "
" def flush(self): pass def runPythonCode(code): output.innerHTML = "" exec(src) def redirectOutput(outputElement): global output # Use global instead of local variable "output" output = outputElement sys.stdout = cOutput() sys.stderr = cError() def bindRunButton(button): button.bind("click", runPythonCode) def setSrc(srcModel): global src src = srcModel # Makes functions available in the global Javascript scope. window.redirectOutput = redirectOutput window.bindRunButton = bindRunButton window.setSrc = setSrc