The AppService class is responsible for creating and managing a single Textual app subprocess. It handles the communication between the running Textual app (via stdin/stdout) and the client browser (via provided websocket callbacks).
When a user connects to the websocket, an AppService instance is created to manage that specific session. It manages process startup, terminal resizing, focus/blur events, and file delivery.
Key Lifecycle Methods:
start(width, height): Launches the subprocess and begins the event loop. Do not call run() manually.stop(): Sends a quit meta packet to the process, cancels pending downloads, and waits for the process to exit.
Communication Flow:
- To the App: Use
send_bytes(), send_meta(), set_terminal_size(), focus(), or blur() to send commands to the subprocess via stdin. - From the App: The service listens to the subprocess
stdout and triggers on_data(), on_meta(), or on_packed() to relay information back to the browser via the callbacks provided during initialization.
# Conceptual initialization pattern
service = AppService(
command="python my_app.py",
write_bytes=websocket.send_bytes,
write_str=websocket.send_text,
close=websocket.close,
download_manager=my_download_manager
)
await service.start(width=80, height=24)