Python Console¶
an interactive Python prompt inside the running game.
▶ Run in browserTags: devtools
PyConsoleNode is a Python interpreter that runs in the game’s own process, and
TerminalEmulator is the widget that shows it and sends it keystrokes. Because
the interpreter is the game’s own, the console works the same on the desktop and
in the browser, and it sees the live program: whatever you pass as namespace
becomes a global at the prompt. Here that is tree (the SceneTree) and app.
Wiring the two takes three calls: add both nodes, terminal.attach(console) so
the widget prints what the console writes and forwards what you type, then
console.start() to print the banner and the first prompt. The console emits
process_exited when it closes, which this example uses to quit.
Commands run on the frame thread, so a long loop or time.sleep freezes the game
while it runs, and input() raises instead of waiting.
Try:
2 ** 100import math; math.tau[n.name for n in tree.root.children]help(str)prints the whole help text; the console has no pager.
Click the terminal to focus it. Left, Right, Home and End move along the line,
Up and Down recall history, Ctrl+C abandons the line, Ctrl+L clears the screen,
and Ctrl+D on an empty line or exit() closes the console.
Source¶
1# /// simvx
2# web = { responsive = true }
3# ///
4"""Python Console: an interactive Python prompt inside the running game.
5
6`PyConsoleNode` is a Python interpreter that runs in the game's own process, and
7`TerminalEmulator` is the widget that shows it and sends it keystrokes. Because
8the interpreter is the game's own, the console works the same on the desktop and
9in the browser, and it sees the live program: whatever you pass as `namespace`
10becomes a global at the prompt. Here that is `tree` (the `SceneTree`) and `app`.
11
12Wiring the two takes three calls: add both nodes, `terminal.attach(console)` so
13the widget prints what the console writes and forwards what you type, then
14`console.start()` to print the banner and the first prompt. The console emits
15`process_exited` when it closes, which this example uses to quit.
16
17Commands run on the frame thread, so a long loop or `time.sleep` freezes the game
18while it runs, and `input()` raises instead of waiting.
19
20Try:
21
22- `2 ** 100`
23- `import math; math.tau`
24- `[n.name for n in tree.root.children]`
25- `help(str)` prints the whole help text; the console has no pager.
26
27Click the terminal to focus it. Left, Right, Home and End move along the line,
28Up and Down recall history, Ctrl+C abandons the line, Ctrl+L clears the screen,
29and Ctrl+D on an empty line or `exit()` closes the console.
30"""
31
32from simvx.core import Anchor, Node, PyConsoleNode
33from simvx.core.ui import Label, TerminalEmulator
34from simvx.graphics import App
35
36
37class PythonConsoleDemo(Node):
38 def on_ready(self):
39 title = self.add_child(Label("Python console: runs in-process", anchor=Anchor.TOP_LEFT, inset=(10, 5)))
40 title.text_colour = (0.6, 0.8, 1.0, 1.0)
41
42 terminal = self.add_child(TerminalEmulator(cols=100, rows=30, anchor=Anchor.TOP_LEFT, inset=(10, 30)))
43 console = self.add_child(PyConsoleNode(namespace={"tree": self.tree, "app": self.app}))
44 terminal.attach(console)
45 console.start()
46 terminal.grab_focus()
47 console.process_exited.connect(lambda _code: self.app.quit())
48
49
50if __name__ == "__main__":
51 App(title="Python Console", width=880, height=650).run(PythonConsoleDemo())