-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PyTerminal Plugin #1801
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
Closed
Closed
PyTerminal Plugin #1801
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Di 10000 ff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// PyScript py-terminal plugin | ||
import { hooks } from "../core.js"; | ||
|
||
const XTERM = "5.3.0"; | ||
const XTERM_READLINE = "1.1.1"; | ||
|
||
const { assign } = Object; | ||
|
||
// Avoid conflicts with py-terminal re-definition | ||
if (!customElements.get("py-terminal")) { | ||
document.head.append( | ||
assign(document.createElement("link"), { | ||
rel: "stylesheet", | ||
href: `https://cdn.jsdelivr.net/npm/xterm@${XTERM}/css/xterm.min.css`, | ||
}), | ||
); | ||
|
||
const [{ Terminal }, { Readline }] = await Promise.all([ | ||
import( | ||
/* webpackIgnore: true */ `https://cdn.jsdelivr.net/npm/xterm@${XTERM}/+esm` | ||
), | ||
import( | ||
/* webpackIgnore: true */ `https://cdn.jsdelivr.net/npm/xterm-readline@${XTERM_READLINE}/+esm` | ||
), | ||
]); | ||
|
||
customElements.define( | ||
"py-terminal", | ||
class extends HTMLElement { | ||
#terminal; | ||
#readline; | ||
init(options) { | ||
this.#readline = new Readline(); | ||
this.#terminal = new Terminal({ | ||
theme: { | ||
background: "#191A19", | ||
foreground: "#F5F2E7", | ||
}, | ||
...options, | ||
}); | ||
this.#terminal.loadAddon(this.#readline); | ||
this.#terminal.open(this); | ||
this.#terminal.focus(); | ||
} | ||
readline(prompt) { | ||
return this.#readline.read(prompt); | ||
} | ||
write(line) { | ||
this.#readline.write(line); | ||
} | ||
}, | ||
); | ||
|
||
const codeBefore = ` | ||
from pyscript import pyterminal as _pyterminal | ||
_pyterminal.init() | ||
`; | ||
|
||
const codeAfter = ` | ||
from pyscript import pyterminal as _pyterminal | ||
|
||
# avoid bootstrapping interact() if no terminal exists | ||
if _pyterminal.PY_TERMINAL: | ||
import code as _code | ||
_code.interact() | ||
`; | ||
|
||
const main = ({ io }) => { | ||
const pt = document.querySelector("py-terminal"); | ||
if (pt) { | ||
cleanUp(true); | ||
pt.init({ | ||
disableStdin: true, | ||
cursorBlink: false, | ||
cursorStyle: "underline", | ||
}); | ||
io.stdout = (value) => { | ||
pt.write(`${value}\n`); | ||
}; | ||
io.stderr = (error) => { | ||
pt.write(`${error.message || error}\n`); | ||
}; | ||
} | ||
}; | ||
|
||
const thread = (_, xworker) => { | ||
const pt = document.querySelector("py-terminal"); | ||
if (pt) { | ||
cleanUp(false); | ||
pt.init({ | ||
disableStdin: false, | ||
cursorBlink: true, | ||
cursorStyle: "block", | ||
}); | ||
xworker.sync.pyterminal_readline = pt.readline.bind(pt); | ||
xworker.sync.pyterminal_write = pt.write.bind(pt); | ||
} | ||
}; | ||
|
||
// we currently support only one <py-terminal> | ||
const cleanUp = (fromMain) => { | ||
hooks.onInterpreterReady.delete(main); | ||
hooks.onWorkerReady.delete(thread); | ||
if (fromMain) { | ||
hooks.codeBeforeRunWorker.delete(codeBefore); | ||
hooks.codeAfterRunWorker.delete(codeBefore); | ||
} | ||
}; | ||
|
||
hooks.onInterpreterReady.add(main); | ||
hooks.onWorkerReady.add(thread); | ||
hooks.codeBeforeRunWorker.add(codeBefore); | ||
hooks.codeAfterRunWorker.add(codeAfter); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import builtins | ||
import sys | ||
|
||
import js | ||
from pyscript import RUNNING_IN_WORKER, document, sync | ||
|
||
|
||
class _PyTerminal: | ||
def write(self, line): | ||
sync.pyterminal_write(line) | ||
|
||
def input(self, prompt): | ||
return sync.pyterminal_readline(prompt) | ||
|
||
|
||
PY_TERMINAL = None | ||
|
||
|
||
def init(): | ||
global PY_TERMINAL | ||
# we currently support only one terminal | ||
# and it's interactive only within a worker tag | ||
if not PY_TERMINAL and RUNNING_IN_WORKER and document.querySelector("py-terminal"): | ||
PY_TERMINAL = _PyTerminal() | ||
sys.stdout = sys.stderr = PY_TERMINAL | ||
builtins.input = PY_TERMINAL.input |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn
D7AE
more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>PyTerminal</title> | ||
<link rel="stylesheet" href="../dist/core.css"> | ||
<script type="module" src="../dist/core.js"></script> | ||
<style>.xterm { padding: .5rem; }</style> | ||
</head> | ||
<body> | ||
<script type="py" worker> | ||
import sys | ||
from pyscript import display | ||
display("Hello", "PyScript Next - PyTerminal", append=False) | ||
print("this should go to the terminal") | ||
print("another line") | ||
|
||
# this works as expected | ||
print("this goes to stderr", file=sys.stderr) | ||
</script> | ||
<py-terminal></py-terminal> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
declare namespace _default { | ||
function error(): Promise<typeof import("./plugins/error.js")>; | ||
} | ||
declare const _default: { | ||
error: () => Promise<typeof import("./plugins/error.js")>; | ||
"py-terminal": () => Promise<typeof import("./plugins/py-terminal.js")>; | ||
}; | ||
export default _default; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This
2D4E
suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a shame not to support top level await...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hoodmane not intentional ... and I am not sure what you are referring to. Mind to expand a bit? We do support top level await if the tag has an
async
attribute though, but I am trying to figure out what would be the benefit here ... or maybe you are referring to some missing field wheninteract(...)
is called that would allow top level await within the repl?Thanks in advance for any sort of clarification, please remember I know nothing about Python 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, this has been discussed during yesterday community call, I am waiting for @hoodmane link/hint on how to workaround and enable the top level await if possible, thanks!