Skip to content

tables

controller.tables

Defines the FSMTable for displaying the FSM in a structured table format.

Classes

AppTreeTable

Bases: Table

Defines a table for the data from the app tree data.

Classes
Meta

Table meta options for rendering behavior and styling.

FSMTable

Bases: Table

Defines a table for the data from the FSM.

Classes
Meta

Table meta options for rendering behavior and styling.

Functions
from_dict(states, current_state) classmethod

Create the FSM table from the states dictionary.

Parameters:

Name Type Description Default
states dict[str, list[dict[str, str]]]

The FSM states and events.

required
current_state str

The current state of the FSM.

required

Returns:

Name Type Description
str str

The rendered FSM table.

Source code in controller/tables.py
@classmethod
def from_dict(cls, states: dict[str, dict[str, str]], current_state: str) -> str:
    """Create the FSM table from the states dictionary.

    Args:
        states (dict[str, list[dict[str, str]]]): The FSM states and events.
        current_state (str): The current state of the FSM.

    Returns:
        str: The rendered FSM table.
    """
    table_data: list[dict[str, SafeString]] = []
    for state, events in states.items():
        current = state == current_state
        table_data.append(
            {
                "state": toggle_text(state, current),
            }
        )
        for event, target in events.items():
            table_data.append(
                {
                    "transition": toggle_button(event, current),
                    "arrow": mark_safe("→"),
                    "target": toggle_text(target, current),
                }
            )
    return cls(table_data)

Functions

toggle_button(event, current)

Render a button that is disabled if the event is not the current state.

Parameters:

Name Type Description Default
event str

The text to display.

required
current bool

Whether the event is the current state.

required

Returns:

Name Type Description
str SafeString

The button as a safe string.

Source code in controller/tables.py
def toggle_button(event: str, current: bool) -> SafeString:
    """Render a button that is disabled if the event is not the current state.

    Args:
        event (str): The text to display.
        current (bool): Whether the event is the current state.

    Returns:
        str: The button as a safe string.
    """
    if current:
        action = f"hx-post={reverse('controller:dialog')} hx-target='#arguments-dialog'"
        return mark_safe(
            f"<input type='submit' value='{event}' name='event' {action} "
            + "class='btn btn-success w-100 mx-2 small-text'>"
        )
    return mark_safe(
        f"<input value='{event}' disabled "
        + "class='btn btn-secondary w-100 mx-2 small-text'>"
    )

toggle_text(text, current)

Format the text to be displayed differently if it is the current state.

Parameters:

Name Type Description Default
text str

The text to display.

required
current bool

Whether the text is the current state.

required

Returns:

Name Type Description
SafeString SafeString

The text as a safe string.

Source code in controller/tables.py
def toggle_text(text: str, current: bool) -> SafeString:
    """Format the text to be displayed differently if it is the current state.

    Args:
        text (str): The text to display.
        current (bool): Whether the text is the current state.

    Returns:
        SafeString: The text as a safe string.
    """
    if not current:
        return mark_safe(text.upper())
    return mark_safe(f'<span class="fw-bold text-primary">{text.upper()}</span>')