Python Console

Embedded interactive Python REPL in a terminal widget.

▶ Run in browser

Tags: ui

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) (then type q to leave the pager-free help)

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/ui/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)        (then type q to leave the pager-free help)
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/ui/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
30from simvx.graphics.draw2d import Draw2D
31
32COLS, ROWS = 100, 30
33
34
35class PythonConsoleDemo(Node):
36    def on_ready(self):
37        Draw2D.set_font()
38
39        title = Label("SimVX Python Console: runs in-process (desktop & web)")
40        title.text_colour = (0.6, 0.8, 1.0, 1.0)
41        title.font_size = 14.0
42        title.set_anchor_preset(AnchorPreset.TOP_LEFT)
43        title.margin_left = 10
44        title.margin_top = 5
45        title.size = Vec2(800, 20)
46        self.add_child(title)
47
48        self.term = TerminalEmulator(cols=COLS, rows=ROWS)
49        self.term.set_anchor_preset(AnchorPreset.TOP_LEFT)
50        self.term.margin_left = 10
51        self.term.margin_top = 30
52        self.add_child(self.term)
53
54        # In-process REPL. Expose live objects so the console can inspect the app.
55        self.console = PyConsoleNode(namespace={"tree": self.tree, "app": self.app, "node": self})
56        self.add_child(self.console)
57
58        self.term.attach(self.console)
59        self.console.start()
60        self.console.resize(COLS, ROWS)
61        self.term.set_focus()
62
63        # Close the app when the console exits (Ctrl-D / exit()).
64        self.console.process_exited.connect(self._on_exit)
65
66    def _on_exit(self, _code):
67        if self.app is not None:
68            self.app.quit()
69
70
71if __name__ == "__main__":
72    app = App(title="SimVX Python Console", width=860, height=650)
73    app.run(PythonConsoleDemo())