Skip to content

Commit cea52b4

Browse files
Adding __terminal__ reference on terminal scripts (pyscript#1947)
1 parent 7ad7f0a commit cea52b4

File tree

8 files changed

+135
-76
lines changed

8 files changed

+135
-76
lines changed

pyscript.core/package-lock.json

Lines changed: 70 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyscript.core/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pyscript/core",
3-
"version": "0.3.17",
3+
"version": "0.3.18",
44
"type": "module",
55
"description": "PyScript",
66
"module": "./index.js",
@@ -53,7 +53,7 @@
5353
"@codemirror/language": "^6.10.0",
5454
"@codemirror/state": "^6.4.0",
5555
"@codemirror/view": "^6.23.0",
56-
"@playwright/test": "^1.41.0",
56+
"@playwright/test": "^1.41.1",
5757
"@rollup/plugin-commonjs": "^25.0.7",
5858
"@rollup/plugin-node-resolve": "^15.2.3",
5959
"@rollup/plugin-terser": "^0.4.4",
@@ -62,7 +62,7 @@
6262
"chokidar": "^3.5.3",
6363
"codemirror": "^6.0.1",
6464
"eslint": "^8.56.0",
65-
"rollup": "^4.9.5",
65+
"rollup": "^4.9.6",
6666
"rollup-plugin-postcss": "^4.0.2",
6767
"rollup-plugin-string": "^3.0.0",
6868
"static-handler": "^0.4.3",

pyscript.core/src/plugins/py-terminal.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,18 @@ const pyTerminal = async () => {
7878
fitAddon.fit();
7979
terminal.focus();
8080
defineProperty(element, "terminal", { value: terminal });
81+
return terminal;
8182
};
8283

8384
// branch logic for the worker
8485
if (element.hasAttribute("worker")) {
8586
// when the remote thread onReady triggers:
8687
// setup the interpreter stdout and stderr
87-
const workerReady = ({ interpreter, io }, { sync }) => {
88+
const workerReady = ({ interpreter, io, run }, { sync }) => {
89+
// in workers it's always safe to grab the polyscript currentScript
90+
run(
91+
"from polyscript.currentScript import terminal as __terminal__",
92+
);
8893
sync.pyterminal_drop_hooks();
8994

9095
// This part is inevitably duplicated as external scope
@@ -135,14 +140,18 @@ const pyTerminal = async () => {
135140
} else {
136141
// in the main case, just bootstrap XTerm without
137142
// allowing any input as that's not possible / awkward
138-
hooks.main.onReady.add(function main({ interpreter, io }) {
143+
hooks.main.onReady.add(function main({ interpreter, io, run }) {
139144
console.warn("py-terminal is read only on main thread");
140145
hooks.main.onReady.delete(main);
141-
init({
146+
147+
// on main, it's easy to trash and clean the current terminal
148+
globalThis.__py_terminal__ = init({
142149
disableStdin: true,
143150
cursorBlink: false,
144151
cursorStyle: "underline",
145152
});
153+
run("from js import __py_terminal__ as __terminal__");
154+
delete globalThis.__py_terminal__;
146155

147156
// This part is inevitably duplicated as external scope
148157
// can't be reached by workers out of the box.

pyscript.core/test/mpy.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,14 @@ test('MicroPython + configURL', async ({ page }) => {
6262
await page.goto('http://localhost:8080/test/config-url.html');
6363
await page.waitForSelector('html.main.worker');
6464
});
65+
66+
test('Pyodide + terminal on Main', async ({ page }) => {
67+
await page.goto('http://localhost:8080/test/py-terminal-main.html');
68+
await page.waitForSelector('html.ok');
69+
});
70+
71+
72+
test('Pyodide + terminal on Worker', async ({ page }) => {
73+
await page.goto('http://localhost:8080/test/py-terminal-worker.html');
74+
await page.waitForSelector('html.ok');
75+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>PyTerminal Main</title>
7+
<link rel="stylesheet" href="../dist/core.css">
8+
<script type="module" src="../dist/core.js"></script>
9+
<style>.xterm { padding: .5rem; }</style>
10+
</head>
11+
<body>
12+
<py-script src="terminal.py" terminal></py-script>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)