Repository navigation
API 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.
-
--webalone serves on127.0.0.1:8000;--web 127.0.0.1:9090,--web :9090,--web localhostand--web [::1]:9090(IPv6 in brackets) choose the address.--web=ADDRESSis never ambiguous; after a bare--webthe next argument is taken as the address only when it looks like one. -
--res 120x60fixes 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; seeturboframe.web.serverfor what protects the server.
The server needs the web extra (aiohttp); this module does not, so an application can import it always.
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
'127.0.0.1'
8000
500
300
20
10
'TURBOFRAME_WEB_CHILD'
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(ValueError)A --web or --res the launcher cannot use; the message says why, for the user.
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.
def is_loopback(host: str) -> boolhost is this machine only: localhost or a loopback address (127.0.0.0/8, ::1).
def launch(argv: list[str] | None = None, *, title: str | None = None, max_sessions: int = 4, open_browser: bool = False) -> boolServe 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.
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.
def parse_res(text: str | None) -> tuple[int, int] | NoneCOLSxROWS (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.
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.
TurboFrame · part of Divengine · GPL-3.0-or-later · report an issue
Tutorial
Examples
The desktop
- The Desktop
- Application Mixins
- Windows and Lists
- Dialogs
- Histories
- Progress
- Themes and Colors
- Glyphs
- Settings and Recent Files
- Command Palette
Editing
Tasks
Languages
Running
Reference
Links