Skip to content

API turboframe.web

rafageist edited this page Dec 4, 2026 · 1 revision

turboframe.web

Source: turboframe/web/init.py. Generated from the code of TurboFrame 0.3.0 by tools/wiki_api.py.

The same application in a web browser: app --web [HOST:PORT] [--res COLSxROWS|auto].

An application calls launch() first thing in its main:

def main() -> None:
    if web.launch():  # --web given: the browser runs the app until the server stops
        return
    MyApp().run()

Without --web nothing changes: launch() returns False and the application runs in its terminal. With it, launch() serves the application to a browser and returns True when the server stops. Each browser tab is a session of its own: the application started again, as a child process, with the same arguments less the web ones, speaking Textual's web protocol (textual.drivers.web_driver). Nothing of the application changes: it is the same TUI, drawn by a terminal in the page.

  • --web alone serves on 127.0.0.1:8000; --web 127.0.0.1:9090, --web :9090, --web localhost and --web [::1]:9090 (IPv6 in brackets) choose the address. --web=ADDRESS is never ambiguous; after a bare --web the next argument is taken as the address only when it looks like one.
  • --res 120x60 fixes the terminal at 120 columns by 60 rows of cells, whatever the browser's size (the page scales it); --res auto (the default) follows the browser window.
  • Only loopback addresses are served (127.0.0.0/8, ::1, localhost). The page is opened with a URL that holds a secret token, printed once on the terminal; see turboframe.web.server for what protects the server.

The server needs the web extra (aiohttp); this module does not, so an application can import it always.

Contents

DEFAULT_HOST, DEFAULT_PORT, MAX_COLS, MAX_ROWS, MIN_COLS, MIN_ROWS, WEB_CHILD_ENV, WebOptions, WebUsageError, child_argv, is_loopback, launch, parse_address, parse_res, split_web_args

DEFAULT_HOST

'127.0.0.1'

DEFAULT_PORT

8000

MAX_COLS

500

MAX_ROWS

300

MIN_COLS

20

MIN_ROWS

10

WEB_CHILD_ENV

'TURBOFRAME_WEB_CHILD'

class WebOptions

class WebOptions
WebOptions(host: str = '127.0.0.1', port: int = 8000, res: tuple[int, int] | None = None)

Where to serve and the terminal's size: res (columns, rows) fixed, or None to follow the browser.

  • url_host (property): The host as a URL writes it (an IPv6 address in brackets).

class WebUsageError

class WebUsageError(ValueError)

A --web or --res the launcher cannot use; the message says why, for the user.

child_argv

def child_argv(rest: list[str]) -> list[str]

The command that starts the application again with rest as its arguments: the same interpreter and script (or -m module) as now, or the frozen executable itself. Never a shell.

is_loopback

def is_loopback(host: str) -> bool

host is this machine only: localhost or a loopback address (127.0.0.0/8, ::1).

launch

def launch(argv: list[str] | None = None, *, title: str | None = None, max_sessions: int = 4, open_browser: bool = False) -> bool

Serve the application to a browser when argv (by default sys.argv) has --web; True once the server stopped (Ctrl+C), False without --web: the application then runs in its terminal as always.

A usage error, a missing web extra or a port in use ends the program with a message and exit status 2. In a session's own process (WEB_CHILD_ENV) it returns False: the web arguments were removed, and a nested --web is refused rather than served again.

parse_address

def parse_address(text: str | None) -> tuple[str, int]

HOST:PORT, HOST, :PORT or [IPV6]:PORT as (host, port); empty or None is the default. Only loopback hosts are accepted: WebUsageError says why another one is not.

parse_res

def parse_res(text: str | None) -> tuple[int, int] | None

COLSxROWS (cells, not pixels) as (cols, rows); auto, empty or None is None (follow the browser). The size must be from MIN_COLSxMIN_ROWS to MAX_COLSxMAX_ROWS.

split_web_args

def split_web_args(args: list[str]) -> tuple[WebOptions | None, list[str]]

args (without the program) as (options, the rest): options is None without --web. --res without --web is a WebUsageError, as is a value given twice. The rest keeps its order.

Clone this wiki locally