[PROPOSAL] REPL output replacement (#439)

* fix OutputManager _append setter

* fix OutputManager change parameters

* fix OutputCtxManager __init__ and change methods

* replacing OutputManager pyscript.write with write function

* add optional output-append attribute to py-repl

* add appendOutput(default: true) to base component

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update pyscriptjs/src/components/pyrepl.ts

Co-authored-by: woxtu <woxtup@gmail.com>

* change from output-append flag to output-mode attribute

* removed type annotation

* repositioned setOutputMode call for auto-generated REPLs to work

* fixed indentation error for indented input

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added preEvaluate method

* moved output-mode logic to preEvaluate

* remove static write method from PyScript, add write method to Element

* removed err parameter from OutputCtxManager

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add PyScript.write back with a deprecation warning

* fix wrong input name

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: woxtu <woxtup@gmail.com>
Co-authored-by: Fabio Pliger <fabio.pliger@gmail.com>
This commit is contained in:
marianoweber
2022-05-25 00:58:48 +02:00
committed by GitHub
parent 2f03b18619
commit f712b1369a
3 changed files with 93 additions and 46 deletions

View File

@@ -32,6 +32,7 @@ export class BaseEvalElement extends HTMLElement {
outputElement: HTMLElement;
errorElement: HTMLElement;
theme: string;
appendOutput: boolean;
constructor() {
super();
@@ -40,6 +41,7 @@ export class BaseEvalElement extends HTMLElement {
this.shadow = this.attachShadow({ mode: 'open' });
this.wrapper = document.createElement('slot');
this.shadow.appendChild(this.wrapper);
this.setOutputMode("append");
}
addToOutput(s: string) {
@@ -47,9 +49,30 @@ export class BaseEvalElement extends HTMLElement {
this.outputElement.hidden = false;
}
setOutputMode(defaultMode = "append") {
const mode = this.hasAttribute('output-mode') ? this.getAttribute('output-mode') : defaultMode;
switch (mode) {
case "append":
this.appendOutput = true;
break;
case "replace":
this.appendOutput = false;
break;
default:
console.log(`${this.id}: custom output-modes are currently not implemented`);
}
}
// subclasses should overwrite this method to define custom logic
// before code gets evaluated
preEvaluate(): void {
return null;
}
// subclasses should overwrite this method to define custom logic
// after code has been evaluated
postEvaluate() {
postEvaluate(): void {
return null;
}
@@ -99,6 +122,8 @@ export class BaseEvalElement extends HTMLElement {
async evaluate(): Promise<void> {
console.log('evaluate');
this.preEvaluate();
const pyodide = runtime;
let source: string;
let output;
@@ -110,13 +135,13 @@ export class BaseEvalElement extends HTMLElement {
if (source.includes('asyncio')) {
await pyodide.runPythonAsync(
`output_manager.change("` + this.outputElement.id + `", "` + this.errorElement.id + `")`,
`output_manager.change(out="${this.outputElement.id}", err="${this.errorElement.id}", append=${this.appendOutput ? 'True' : 'False'})`,
);
output = await pyodide.runPythonAsync(source);
await pyodide.runPythonAsync(`output_manager.revert()`);
} else {
output = pyodide.runPython(
`output_manager.change("` + this.outputElement.id + `", "` + this.errorElement.id + `")`,
`output_manager.change(out="${this.outputElement.id}", err="${this.errorElement.id}", append=${this.appendOutput ? 'True' : 'False'})`,
);
output = pyodide.runPython(source);
pyodide.runPython(`output_manager.revert()`);
@@ -143,8 +168,8 @@ export class BaseEvalElement extends HTMLElement {
this.errorElement.style.removeProperty('display');
}
}
removeClasses(this.errorElement, ['bg-red-200', 'p-2']);
}
removeClasses(this.errorElement, ['bg-red-200', 'p-2']);
this.postEvaluate();
} catch (err) {