mirror of
https://github.com/pyscript/pyscript.git
synced 2022-05-01 19:47:48 +03:00
change OutputManager to actually have separate contexts for out and err to scripts can manage both in one place
This commit is contained in:
@@ -115,9 +115,11 @@ export class BaseEvalElement extends HTMLElement {
|
|||||||
await this._register_esm(pyodide);
|
await this._register_esm(pyodide);
|
||||||
|
|
||||||
if (source.includes("asyncio")){
|
if (source.includes("asyncio")){
|
||||||
|
await pyodide.runPythonAsync(`output_manager.change("`+this.outputElement.id+`")`);
|
||||||
output = await pyodide.runPythonAsync(source);
|
output = await pyodide.runPythonAsync(source);
|
||||||
await pyodide.runPythonAsync(`output_manager.revert()`)
|
await pyodide.runPythonAsync(`output_manager.revert()`)
|
||||||
}else{
|
}else{
|
||||||
|
output = pyodide.runPython(`output_manager.change("`+this.outputElement.id+`")`);
|
||||||
output = pyodide.runPython(source);
|
output = pyodide.runPython(source);
|
||||||
pyodide.runPython(`output_manager.revert()`)
|
pyodide.runPython(`output_manager.revert()`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,31 +95,57 @@ class Element:
|
|||||||
|
|
||||||
return Element(clone.id, clone)
|
return Element(clone.id, clone)
|
||||||
|
|
||||||
class OutputManager:
|
class OutputCtxManager:
|
||||||
def __init__(self, custom=None, output_to_console=True):
|
def __init__(self, out=None, output_to_console=True, append=True):
|
||||||
self._custom = custom
|
self._out = out
|
||||||
self._prev = custom
|
self._prev = out
|
||||||
self.output_to_console = output_to_console
|
self.output_to_console = output_to_console
|
||||||
self.prev = None
|
self._append = append
|
||||||
|
|
||||||
def change(self, custom):
|
def change(self, out=None, err=None, output_to_console=True, append=True):
|
||||||
self._prev = self._custom
|
self._prevt = self._out
|
||||||
self._custom = custom
|
self._out = out
|
||||||
console.log("----> changed to", self._custom)
|
self.output_to_console = output_to_console
|
||||||
|
self._append = append
|
||||||
|
console.log("----> changed out to", self._out, self._append)
|
||||||
|
|
||||||
def revert(self):
|
def revert(self):
|
||||||
console.log("----> reverted")
|
console.log("----> reverted")
|
||||||
self._custom = self._prev
|
self._out = self._prev
|
||||||
|
|
||||||
def write(self, txt):
|
def write(self, txt):
|
||||||
if self._custom:
|
console.log('writing to', self._out, txt, self._append)
|
||||||
pyscript.write(self._custom, txt, append=True)
|
if self._out:
|
||||||
|
pyscript.write(self._out, txt, append=self._append)
|
||||||
if self.output_to_console:
|
if self.output_to_console:
|
||||||
console.log(self._custom, txt)
|
console.log(self._out, txt)
|
||||||
|
|
||||||
|
class OutputManager:
|
||||||
|
def __init__(self, out=None, err=None, output_to_console=True, append=True):
|
||||||
|
sys.stdout = self._out_manager = OutputCtxManager(out, output_to_console, append)
|
||||||
|
sys.strerr = self._err_manager = OutputCtxManager(err, output_to_console, append)
|
||||||
|
self.output_to_console = output_to_console
|
||||||
|
self._append = append
|
||||||
|
|
||||||
|
def change(self, out=None, err=None, output_to_console=True, append=True):
|
||||||
|
self._out_manager.change(out, output_to_console, append)
|
||||||
|
sys.stdout = self._out_manager
|
||||||
|
self._err_manager.change(err, output_to_console, append)
|
||||||
|
sys.stderr = self._err_manager
|
||||||
|
self.output_to_console = output_to_console
|
||||||
|
self.append = append
|
||||||
|
|
||||||
|
def revert(self):
|
||||||
|
self._out_manager.revert()
|
||||||
|
self._err_manager.revert()
|
||||||
|
sys.stdout = self._out_manager
|
||||||
|
sys.stdout = self._err_manager
|
||||||
|
console.log("----> reverted")
|
||||||
|
|
||||||
|
|
||||||
pyscript = PyScript()
|
pyscript = PyScript()
|
||||||
output_manager = OutputManager()
|
output_manager = OutputManager()
|
||||||
sys.stdout = output_manager
|
|
||||||
`
|
`
|
||||||
|
|
||||||
let loadInterpreter = async function(): Promise<any> {
|
let loadInterpreter = async function(): Promise<any> {
|
||||||
|
|||||||
Reference in New Issue
Block a user