Skip to content

trading_agent.terminal.widgets.confirm_dialog

A dialog that asks for a reason, and sometimes a typed word, before a serious action such as flattening a desk or firing the kill switch.

It copies the web page's ConfirmDialog.tsx: the action cannot be confirmed until a reason is written, and, when a word is required, until that exact word is typed. It returns the reason, or None when cancelled.

Typical usage example:

app.push_screen(ConfirmDialog('Fire the kill switch', explanation, 'Fire', word='FLATTEN'), on_reason)

ConfirmDialog

ConfirmDialog(title, explanation, action_label, word=None)

Bases: ModalScreen[str | None]

The confirmation dialog.

Creates the dialog.

Parameters:

Name Type Description Default
title str

What is about to happen.

required
explanation str

What it does, in a sentence or two.

required
action_label str

The word for confirming, such as "Flatten".

required
word str | None

A word the user must type to confirm, or None.

None
Source code in src/trading_agent/terminal/widgets/confirm_dialog.py
def __init__(
    self,
    title: str,
    explanation: str,
    action_label: str,
    word: str | None = None,
):
    """Creates the dialog.

    Args:
        title (str): What is about to happen.
        explanation (str): What it does, in a sentence or two.
        action_label (str): The word for confirming, such as "Flatten".
        word (str | None): A word the user must type to confirm, or None.
    """
    super().__init__(classes='picker-screen')
    self._title_text = title
    self._explanation = explanation
    self._action_label = action_label
    self._word = word

compose

compose()

Builds the dialog.

Yields:

Type Description
Widget

The title, the explanation, the reason box, the word box when needed, the error line and the hint.

Source code in src/trading_agent/terminal/widgets/confirm_dialog.py
def compose(self):
    """Builds the dialog.

    Yields:
        Widget: The title, the explanation, the reason box, the word box when needed, the error line and the hint.
    """
    with containers.Vertical(classes='picker confirm'):
        yield widgets.Static(
            Content.from_markup(
                '[b $error]$title[/]', title=self._title_text
            ),
            classes='picker-title',
        )
        yield widgets.Static(self._explanation, markup=False)
        yield widgets.Input(
            placeholder='Reason (required, kept in the journal)',
            id='confirm-reason',
        )
        if self._word is not None:
            yield widgets.Input(
                placeholder=f'Type {self._word} to confirm',
                id='confirm-word',
            )
        yield widgets.Static('', id='confirm-error')
        yield widgets.Static(
            Content.from_markup(
                '[dim]enter to $action · esc to cancel[/dim]',
                action=self._action_label.lower(),
            ),
            classes='picker-hint',
        )

on_mount

on_mount()

Puts the cursor in the reason box.

Source code in src/trading_agent/terminal/widgets/confirm_dialog.py
def on_mount(self) -> None:
    """Puts the cursor in the reason box."""
    self.query_one('#confirm-reason', widgets.Input).focus()

on_input_submitted

on_input_submitted(event)

Confirms when the reason, and the word if needed, are given.

Parameters:

Name Type Description Default
event Submitted

The submission.

required
Source code in src/trading_agent/terminal/widgets/confirm_dialog.py
def on_input_submitted(self, event: widgets.Input.Submitted) -> None:
    """Confirms when the reason, and the word if needed, are given.

    Args:
        event (widgets.Input.Submitted): The submission.
    """
    event.stop()
    reason = self.query_one('#confirm-reason', widgets.Input).value.strip()
    if not reason:
        self._error('Write a reason first.')
        self.query_one('#confirm-reason', widgets.Input).focus()
        return
    if self._word is not None:
        box = self.query_one('#confirm-word', widgets.Input)
        if box.value.strip() != self._word:
            self._error(f'Type {self._word} exactly to confirm.')
            box.focus()
            return
    self.dismiss(reason)

action_cancel

action_cancel()

Closes the dialog without confirming.

Source code in src/trading_agent/terminal/widgets/confirm_dialog.py
def action_cancel(self) -> None:
    """Closes the dialog without confirming."""
    self.dismiss(None)