Python Console¶
Embedded interactive Python REPL in a terminal widget.
▶ Run in browserTags: devtools
Pairs the TerminalEmulator widget with PyConsoleNode – an in-process Python interpreter. Unlike the bash terminal demo (which needs a real PTY and only runs on desktop), this console runs inside the app’s own interpreter, so it works identically on desktop and in the browser via Pyodide. It also shares the live program state: the namespace below exposes the scene tree so you can poke at it.
Try:
2 ** 100
import math; math.tau
[n.name for n in tree.root.children]
help(str) (prints the full help text; the embedded console has no pager)
Controls:
Click the terminal to focus, type to interact
Left/Right/Home/End edit the line; Up/Down recall history
Ctrl+C cancels the current line; Ctrl+D (empty line) or exit() closes it
Run: uv run python examples/features/devtools/python_console.py
Source¶
1"""Python Console: Embedded interactive Python REPL in a terminal widget.
2
3Pairs the TerminalEmulator widget with PyConsoleNode -- an in-process Python
4interpreter. Unlike the bash terminal demo (which needs a real PTY and only runs
5on desktop), this console runs *inside* the app's own interpreter, so it works
6identically on desktop and in the browser via Pyodide. It also shares the live
7program state: the namespace below exposes the scene tree so you can poke at it.
8
9Try:
10 - 2 ** 100
11 - import math; math.tau
12 - [n.name for n in tree.root.children]
13 - help(str) (prints the full help text; the embedded console has no pager)
14
15Controls:
16 - Click the terminal to focus, type to interact
17 - Left/Right/Home/End edit the line; Up/Down recall history
18 - Ctrl+C cancels the current line; Ctrl+D (empty line) or exit() closes it
19
20Run: uv run python examples/features/devtools/python_console.py
21
22# /// simvx
23# web = { responsive = true, root = "PythonConsoleDemo" }
24# ///
25"""
26
27from simvx.core import AnchorPreset, Node, PyConsoleNode, Vec2
28from simvx.core.ui import Label, TerminalEmulator
29from simvx.graphics import App
30
31COLS, ROWS = 100, 30
32
33
34class PythonConsoleDemo(Node):
35 def on_ready(self):
36 title = Label("SimVX Python Console: runs in-process (desktop & web)")
37 title.text_colour = (0.6, 0.8, 1.0, 1.0)
38 title.font_size = 14.0
39 title.set_anchor_preset(AnchorPreset.TOP_LEFT)
40 title.margin_left = 10
41 title.margin_top = 5
42 title.size = Vec2(800, 20)
43 self.add_child(title)
44
45 self.term = TerminalEmulator(cols=COLS, rows=ROWS)
46 self.term.set_anchor_preset(AnchorPreset.TOP_LEFT)
47 self.term.margin_left = 10
48 self.term.margin_top = 30
49 self.add_child(self.term)
50
51 # In-process REPL. Expose live objects so the console can inspect the app.
52 self.console = PyConsoleNode(namespace={"tree": self.tree, "app": self.app, "node": self})
53 self.add_child(self.console)
54
55 self.term.attach(self.console)
56 self.console.start()
57 self.console.resize(COLS, ROWS)
58 self.term.set_focus()
59
60 # Close the app when the console exits (Ctrl-D / exit()).
61 self.console.process_exited.connect(self._on_exit)
62
63 def _on_exit(self, _code):
64 if self.app is not None:
65 self.app.quit()
66
67
68if __name__ == "__main__":
69 app = App(title="SimVX Python Console", width=880, height=650)
70 app.run(PythonConsoleDemo())