capsule.pty() opens a real pseudo-terminal (PTY) inside the capsule over a WebSocket connection. You can run any interactive program — a shell, a REPL, a TUI — send keystrokes, resize the terminal, and read raw output bytes in real time. Sessions survive WebSocket disconnects: the process keeps running on the capsule, and you can reconnect to it later using its tag.
Opening a PTY session
capsule.pty() is a context manager. On entry it opens a WebSocket and starts the specified command inside a PTY. On exit it sends SIGKILL to the process and closes the connection.
Parameters
Iterating events
Iterating over the session yieldsPtyEvent objects:
term.tag and term.pid are None until the started event is received. If you need them before iterating, consume the first event explicitly.Sending input
Resizing the terminal
Callresize() whenever the host terminal dimensions change:
Killing the process
Session tag and PID
After thestarted event, term.tag and term.pid are populated:
Reconnecting to an existing session
The PTY process keeps running on the capsule after you disconnect. Usecapsule.pty_connect() with the saved tag to reattach:
Sessions persist until the process exits or the capsule is destroyed. You can disconnect and reconnect as many times as you need without losing the process state.
Accessing services running inside the capsule
If a process inside the capsule listens on a port, usecapsule.get_url() to get a proxy URL you can connect to from outside: