Skip to content

trading_agent.terminal.screens.apps_screen

The Apps page: the packages' web apps, whether they answer, their systemd units, and start, stop, restart and open.

The web page shows an app inside the page; a terminal cannot, so o opens the app in the browser with xdg-open.

Typical usage example:

app.push_screen(AppsScreen('general'))

AppsScreen

AppsScreen(desk_name)

Bases: PageScreen

The Apps page.

Creates the page.

Parameters:

Name Type Description Default
desk_name str

The desk the chat shows.

required
Source code in src/trading_agent/terminal/screens/apps_screen.py
def __init__(self, desk_name: str):
    """Creates the page.

    Args:
        desk_name (str): The desk the chat shows.
    """
    super().__init__(desk_name)
    self._apps: list[dict[str, Any]] = []

compose_page

compose_page()

Builds the apps table.

Yields:

Type Description
Widget

The table.

Source code in src/trading_agent/terminal/screens/apps_screen.py
def compose_page(self):
    """Builds the apps table.

    Yields:
        Widget: The table.
    """
    yield widgets.DataTable(id='apps', cursor_type='row')

on_mount

on_mount()

Sets up the table, then reads the apps.

Source code in src/trading_agent/terminal/screens/apps_screen.py
def on_mount(self) -> None:
    """Sets up the table, then reads the apps."""
    self.table('apps').add_columns(
        'App', 'What it is', 'Answers', 'Unit', 'Address'
    )
    super().on_mount()

refresh_page async

refresh_page()

Reads the apps.

Source code in src/trading_agent/terminal/screens/apps_screen.py
@work(exclusive=True, group='page')
async def refresh_page(self) -> None:
    """Reads the apps."""
    try:
        self._apps = await self.app.gateway.apps()
    except gateway_client.GatewayError as error:
        self.problem(error.message)
        return
    table = self.table('apps')
    cursor = table.cursor_row
    table.clear()
    for item in self._apps:
        health = (
            'yes'
            if item.get('healthy')
            else f'no ({item.get("health_detail", "")})'
        )
        unit_state = item.get('unit_state') or {}
        unit = f'{item.get("unit", "")} {unit_state.get("active_state", "")}/{unit_state.get("sub_state", "")}'
        if item.get('unit_error'):
            unit += f' ({item["unit_error"]})'
        table.add_row(
            str(item.get('title') or item.get('package', '')),
            str(item.get('description', ''))[:50],
            health,
            unit,
            str(item.get('url', '')),
            key=str(item.get('package', '')),
        )
    if cursor is not None and cursor < table.row_count:
        table.move_cursor(row=cursor)

action_open_app

action_open_app()

Opens the highlighted app in the browser.

Source code in src/trading_agent/terminal/screens/apps_screen.py
def action_open_app(self) -> None:
    """Opens the highlighted app in the browser."""
    package = self.selected_key('apps')
    for item in self._apps:
        if item.get('package') == package and item.get('url'):
            program = shutil.which('xdg-open')
            if program is None:
                self.notify(f'Open {item["url"]} in your browser.')
                return
            subprocess.Popen(
                [
                    program,
                    str(item['url']),
                ],
                stdin=subprocess.DEVNULL,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
                start_new_session=True,
            )
            return

action_start

action_start()

Starts the highlighted app's unit.

Source code in src/trading_agent/terminal/screens/apps_screen.py
def action_start(self) -> None:
    """Starts the highlighted app's unit."""
    self._act('start')

action_stop

action_stop()

Stops the highlighted app's unit.

Source code in src/trading_agent/terminal/screens/apps_screen.py
def action_stop(self) -> None:
    """Stops the highlighted app's unit."""
    self._act('stop')

action_restart

action_restart()

Restarts the highlighted app's unit.

Source code in src/trading_agent/terminal/screens/apps_screen.py
def action_restart(self) -> None:
    """Restarts the highlighted app's unit."""
    self._act('restart')