Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
140 changes: 70 additions & 70 deletions pyscript.core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyscript.core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pyscript/core",
"version": "0.3.17",
"version": "0.3.18",
"type": "module",
"description": "PyScript",
"module": "./index.js",
Expand Down Expand Up @@ -53,7 +53,7 @@
"@codemirror/language": "^6.10.0",
"@codemirror/state": "^6.4.0",
"@codemirror/view": "^6.23.0",
"@playwright/test": "^1.41.0",
"@playwright/test": "^1.41.1",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
Expand All @@ -62,7 +62,7 @@
"chokidar": "^3.5.3",
"codemirror": "^6.0.1",
"eslint": "^8.56.0",
"rollup": "^4.9.5",
"rollup": "^4.9.6",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-string": "^3.0.0",
"static-handler": "^0.4.3",
Expand Down
15 changes: 12 additions & 3 deletions pyscript.core/src/plugins/py-terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,18 @@ const pyTerminal = async () => {
fitAddon.fit();
terminal.focus();
defineProperty(element, "terminal", { value: terminal });
return terminal;
};

// branch logic for the worker
if (element.hasAttribute("worker")) {
// when the remote thread onReady triggers:
// setup the interpreter stdout and stderr
const workerReady = ({ interpreter, io }, { sync }) => {
const workerReady = ({ interpreter, io, run }, { sync }) => {
// in workers it's always safe to grab the polyscript currentScript
run(
"from polyscript.currentScript import terminal as __terminal__",
);
sync.pyterminal_drop_hooks();

// This part is inevitably duplicated as external scope
Expand Down Expand Up @@ -135,14 +140,18 @@ const pyTerminal = async () => {
} else {
// in the main case, just bootstrap XTerm without
// allowing any input as that's not possible / awkward
hooks.main.onReady.add(function main({ interpreter, io }) {
hooks.main.onReady.add(function main({ interpreter, io, run }) {
console.warn("py-terminal is read only on main thread");
hooks.main.onReady.delete(main);
init({

// on main, it's easy to trash and clean the current terminal
globalThis.__py_terminal__ = init({
disableStdin: true,
cursorBlink: false,
cursorStyle: "underline",
});
run("from js import __py_terminal__ as __terminal__");
delete globalThis.__py_terminal__;
Comment on lines +146 to +154
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still ought to be able to assign to the Python scope directly from JavaScript rather than doing this globalThis dance...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, I am not proud of that ugly dance but it worked well in both MicroPython and Pyodide ... can I do better for both "worlds"? 🤔


// This part is inevitably duplicated as external scope
// can't be reached by workers out of the box.
Expand Down
11 changes: 11 additions & 0 deletions pyscript.core/test/mpy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ test('MicroPython + configURL', async ({ page }) => {
await page.goto('http://localhost:8080/test/config-url.html');
await page.waitForSelector('html.main.worker');
});

test('Pyodide + terminal on Main', async ({ page }) => {
await page.goto('http://localhost:8080/test/py-terminal-main.html');
await page.waitForSelector('html.ok');
});


test('Pyodide + terminal on Worker', async ({ page }) => {
await page.goto('http://localhost:8080/test/py-terminal-worker.html');
await page.waitForSelector('html.ok');
});
14 changes: 14 additions & 0 deletions pyscript.core/test/py-terminal-main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PyTerminal Main</title>
<link rel="stylesheet" href="../dist/core.css">
<script type="module" src="../dist/core.js"></script>
<style>.xterm { padding: .5rem; }</style>
</head>
<body>
<py-script src="terminal.py" terminal></py-script>
</body>
</html>
Loading