8000 change OutputManager to actually have separate contexts for out and e… · testnest/pyscript@00b571d · GitHub
[go: up one dir, main page]

Skip to content

Commit 00b571d

Browse files
committed
change OutputManager to actually have separate contexts for out and err to scripts can manage both in one place
1 parent 4d89060 commit 00b571d

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

pyscriptjs/src/components/base.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ export class BaseEvalElement extends HTMLElement {
115115
await this._register_esm(pyodide);
116116

117117
if (source.includes("asyncio")){
118+
await pyodide.runPythonAsync(`output_manager.change("`+this.outputElement.id+`")`);
118119
output = await pyodide.runPythonAsync(source);
119120
await pyodide.runPythonAsync(`output_manager.revert()`)
120121
}else{
122+
output = pyodide.runPython(`output_manager.change("`+this.outputElement.id+`")`);
121123
output = pyodide.runPython(source);
122124
pyodide.runPython(`output_manager.revert()`)
123125
}

pyscriptjs/src/interpreter.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,57 @@ class Element:
9595
9696
return Element(clone.id, clone)
9797
98-
class OutputManager:
99-
def __init__(self, custom=None, output_to_console=True):
100-
self._custom = custom
101-
self._prev = custom
98+
class OutputCtxManager:
99+
def __init__(self, out=None, output_to_console=True, append=True):
100+
self._out = out
101+
self._prev = out
102102
self.output_to_console = output_to_console
103-
self.prev = None
103+
self._append = append
104104
105-
def change(self, custom):
106-
self._prev = self._custom
107-
self._custom = custom
108-
console.log("----> changed to", self._custom)
105+
def change(self, out=None, err=None, output_to_console=True, append=True):
106+
self._prevt = self._out
107+
self._out = out
108+
self.output_to_console = output_to_console
109+
self._append = append
110+
console.log("----> changed out to", self._out, self._append)
109111
110112
def revert(self):
111113
console.log("----> reverted")
112-
self._custom = self._prev
114+
self._out = self._prev
113115
114116
def write(self, txt):
115-
if self._custom:
116-
pyscript.write(self._custom, txt, append=True)
117+
console.log('writing to', self._out, txt, self._append)
118+
if self._out:
119+
pyscript.write(self._out, txt, append=self._append)
117120
if self.output_to_console:
118-
console.log(self._custom, txt)
121+
console.log(self._out, txt)
122+
123+
class OutputManager:
124+
def __init__(self, out=None, err=None, output_to_console=True, append=True):
125+
sys.stdout = self._out_manager = OutputCtxManager(out, output_to_console, append)
126+
sys.strerr = self._err_manager = OutputCtxManager(err, output_to_console, append)
127+
self.output_to_console = output_to_console
128+
self._append = append
129+
130+
def change(self, out=None, err=None, output_to_console=True, append=True):
131+
self._out_manager.change(out, output_to_console, append)
132+
sys.stdout = self._out_manager
133+
self._err_manager.change(err, output_to_console, append)
134+
sys.stderr = self._err_manager
135+
self.output_to_console = output_to_console
136+
self.append = append
137+
138+
def revert(self):
139+
self._out_manager.revert()
140+
self._err_manager.revert()
141+
sys.stdout = self._out_manager
142+
sys.stdout = self._err_manager
143+
console.log("----> reverted")
144+
119145
120146
pyscript = PyScript()
121147
output_manager = OutputManager()
122-
sys.stdout = output_manager
148+
123149
`
124150

125151
let loadInterpreter = async function(): Promise<any> {

0 commit comments

Comments
 (0)
0