Skip to content

Add REPL plugin hooks; Add output, output-mode, stderr attributes #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
54d10d3
Add before, after REPL hooks
JeffersGlass Jan 11, 2023
62c1595
Basic stdout/stderr handling
JeffersGlass Jan 11, 2023
52d3c5d
Re-introduce 'output-mode' attribute for py-repl
JeffersGlass Jan 11, 2023
7692b56
Adjust to use options/kwargs
JeffersGlass Jan 25, 2023
be949d4
runtime -> interpreter
JeffersGlass Jan 26, 2023
b2d2d9c
Add plugin execution tests
JeffersGlass Jan 26, 2023
dd2cc99
Add test for output attribute on repl
JeffersGlass Jan 26, 2023
0a84e07
Add async test
JeffersGlass Jan 26, 2023
859cb4a
Stub remaining integration tests
JeffersGlass Jan 26, 2023
91c25e0
Remove dedent
JeffersGlass Jan 27, 2023
813bb48
Add dynamic-tag test
JeffersGlass Jan 27, 2023
cc4bbad
More tests
JeffersGlass Jan 27, 2023
92f720a
'Output=' doesn't affect display()
JeffersGlass Jan 29, 2023
48043e5
Adjust behavior of repl results
JeffersGlass Jan 29, 2023
ae0bdcf
Documentation
JeffersGlass Jan 29, 2023
47896ab
Changelog
JeffersGlass Jan 29, 2023
c4381f7
Cleanup
JeffersGlass Feb 6, 2023
eee2fe1
Merge from main
JeffersGlass Feb 6, 2023
2168944
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 6, 2023
56b7770
Address comments
JeffersGlass Mar 20, 2023
856d61b
Merge from main
JeffersGlass Mar 20, 2023
a8deeea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2023
2e904a8
Remove unused imports
JeffersGlass Mar 22, 2023
982f477
Formats changelog
marimeireles Mar 22, 2023
d775649
Linting
marimeireles Mar 22, 2023
5bf2da8
Linting correctly
marimeireles Mar 22, 2023
9d84d86
Adjust changelog formatting
JeffersGlass Mar 22, 2023
49faecf
Merge branch 'main' into pyrepl-hooks
JeffersGlass Mar 22, 2023
41884c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Basic stdout/stderr handling
  • Loading branch information
JeffersGlass committed Mar 22, 2023
commit 62c15959830925a79421b731c268470c021c0320
30 changes: 14 additions & 16 deletions pyscriptjs/src/components/pyrepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { pyExec, pyDisplay } from '../pyexec';
import { getLogger } from '../logger';
import { InterpreterClient } from '../interpreter_client';
import type { PyScriptApp } from '../main';
import { Stdio } from '../stdio';

const logger = getLogger('py-repl');
const RUNBUTTON = `<svg style="height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green" viewBox="0 0 384 512" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg"><g transform="translate(192 256)" transform-origin="96 0"><g transform="translate(0,0) scale(1,1)"><path d="M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z" fill="currentColor" transform="translate(-192 -256)"></path></g></g></svg>`;
Expand All @@ -32,6 +33,8 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {
shadow: ShadowRoot;
outDiv: HTMLElement;
editor: EditorView;
stdout_manager: Stdio | null;
stderr_manager: Stdio | null;

constructor() {
super();
Expand Down Expand Up @@ -167,8 +170,9 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {
outEl.innerHTML = '';

// execute the python code
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
app.plugins.beforePyReplExec(interpreter, pySrc, outEl, this);
const pyResult = (await pyExec(interpreter, pySrc, outEl)).result;
app.plugins.afterPyReplExec(interpreter, pySrc, outEl, this, pyResult);

// display the value of the last evaluated expression (REPL-style)
if (pyResult !== undefined) {
Expand Down Expand Up @@ -207,27 +211,21 @@ export function make_PyRepl(interpreter: InterpreterClient, app: PyScriptApp) {
const nextExecId = parseInt(lastExecId) + 1;

const newPyRepl = document.createElement('py-repl');
newPyRepl.setAttribute('root', this.getAttribute('root'));
newPyRepl.id = this.getAttribute('root') + '-' + nextExecId.toString();

if (this.hasAttribute('auto-generate')) {
newPyRepl.setAttribute('auto-generate', '');
this.removeAttribute('auto-generate');
}

const outputMode = getAttribute(this, 'output-mode');
if (outputMode) {
newPyRepl.setAttribute('output-mode', outputMode);
}

const addReplAttribute = (attribute: string) => {
//Attributes to be copied from old REPL to auto-generated REPL
for (const attribute of ['root', 'output-mode', 'output', 'stderr']){
const attr = getAttribute(this, attribute);
if (attr) {
newPyRepl.setAttribute(attribute, attr);
}
};
}

newPyRepl.id = this.getAttribute('root') + '-' + nextExecId.toString();

addReplAttribute('output');
if (this.hasAttribute('auto-generate')) {
newPyRepl.setAttribute('auto-generate', '');
this.removeAttribute('auto-generate');
}

newPyRepl.setAttribute('exec-id', nextExecId.toString());
if (this.parentElement) {
Expand Down
32 changes: 32 additions & 0 deletions pyscriptjs/src/plugins/stdiodirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,36 @@ export class StdioDirector extends Plugin {
options.pyScriptTag.stderr_manager = null;
}
}

beforePyReplExec(runtime: any, src: any, outEl: any, pyReplTag: any): void {
let output_targeted_io;
if (pyReplTag.hasAttribute("output")){
output_targeted_io = new TargetedStdio(pyReplTag, "output", true, true);
}
else {
output_targeted_io = new TargetedStdio(pyReplTag.outDiv, "id", true, true);
}

pyReplTag.stdout_manager = output_targeted_io;
this._stdioMultiplexer.addListener(output_targeted_io);


if (pyReplTag.hasAttribute("stderr")){
const stderr_targeted_io = new TargetedStdio(pyReplTag, "stderr", false, true);
pyReplTag.stderr_manager = stderr_targeted_io;
this._stdioMultiplexer.addListener(stderr_targeted_io);
}

}

afterPyReplExec(runtime: any, src: any, outEl: any, pyReplTag: any, result: any): void {
if (pyReplTag.stdout_manager != null){
this._stdioMultiplexer.removeListener(pyReplTag.stdout_manager)
pyReplTag.stdout_manager = null
}
if (pyReplTag.stderr_manager != null){
this._stdioMultiplexer.removeListener(pyReplTag.stderr_manager)
pyReplTag.stderr_manager = null
}
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy