Skip to content

Config

elva.widgets.config

Configuration panel widgets for Textual apps.

Classes:

ConfigPanel(config, applied=False, label=None)

Bases: Container

Container for ConfigView widgets.

Parameters:

  • config (dict) –

    configuration parameter mapping to apply to write to the panel.

  • applied (bool, default: False ) –

    if False, treat all config view values as changed.

  • label (str, default: None ) –

    label string for the panel itself to be displayed at the top.

Classes:

  • Applied

    Message object holding configuration change information.

Methods:

Attributes:

  • state

    Current configuration parameters.

  • last

    Previous configuration parameters.

  • changed

    Changed configuration parameters.

Source code in src/elva/widgets/config.py
def __init__(self, config: dict, applied: bool = False, label: str = None):
    """
    Arguments:
        config: configuration parameter mapping to apply to write to the panel.
        applied: if `False`, treat all config view values as changed.
        label: label string for the panel itself to be displayed at the top.
    """
    super().__init__()
    self.config = config
    self.applied = applied
    self.label = label

state property

Current configuration parameters.

last property

Previous configuration parameters.

changed property

Changed configuration parameters.

Applied(last, config, changed)

Bases: Message

Message object holding configuration change information.

Parameters:

  • last (dict) –

    previous configuration mapping.

  • config (dict) –

    current configuration mapping.

  • changed (dict) –

    mapping of changed configuration parameters.

Attributes:

  • last (dict) –

    Previous configuration mapping.

  • config (dict) –

    Current configuration mapping.

  • changed (dict) –

    Mapping of changed configuration parameters.

Source code in src/elva/widgets/config.py
def __init__(self, last: dict, config: dict, changed: dict):
    """
    Arguments:
        last: previous configuration mapping.
        config: current configuration mapping.
        changed: mapping of changed configuration parameters.
    """
    super().__init__()
    self.last = last
    self.config = config
    self.changed = changed
last = last instance-attribute

Previous configuration mapping.

config = config instance-attribute

Current configuration mapping.

changed = changed instance-attribute

Mapping of changed configuration parameters.

compose()

Hook arranging config view widgets.

Source code in src/elva/widgets/config.py
def compose(self):
    """
    Hook arranging config view widgets.
    """
    if self.label:
        yield Label(self.label)
    with Grid():
        with VerticalScroll():
            for c in self.config:
                yield c
        with Grid():
            yield Button("Apply", id="apply")
            yield Button("Reset", id="reset")

apply()

Store the current value also as last value.

Source code in src/elva/widgets/config.py
def apply(self):
    """
    Store the current value also as last value.
    """
    for c in self.config:
        c.apply()
    self.applied = True

reset()

Reset the current value to the last value.

Source code in src/elva/widgets/config.py
def reset(self):
    """
    Reset the current value to the last value.
    """
    for c in self.config:
        c.reset()

post_applied_config()

Send an Applied message.

Source code in src/elva/widgets/config.py
def post_applied_config(self):
    """Send an [`Applied`][elva.widgets.config.ConfigPanel.Applied] message."""
    self.post_message(self.Applied(self.last, self.state, self.changed))
    self.apply()

on_button_pressed(message)

Hook called on a pressed button.

Either posts an Applied message or resets the config views.

Parameters:

  • message (Message) –

    message object from the pressed button event.

Source code in src/elva/widgets/config.py
def on_button_pressed(self, message: Message):
    """
    Hook called on a pressed button.

    Either posts an [`Applied`][elva.widgets.config.ConfigPanel.Applied] message or resets the config views.

    Arguments:
        message: message object from the pressed button event.
    """
    match message.button.id:
        case "apply":
            self.post_applied_config()
        case "reset":
            self.reset()

decode_content(content)

Try to decode content according to TOML syntax.

Parameters:

  • content (str) –

    TOML data string to be parsed.

Returns:

  • dict

    parsed configuration mapping.

Source code in src/elva/widgets/config.py
def decode_content(self, content: str) -> dict:
    """
    Try to decode content according to TOML syntax.

    Arguments:
        content: TOML data string to be parsed.

    Returns:
        parsed configuration mapping.
    """
    try:
        config = tomllib.loads(content)
    # tomli exceptions may change in the future according to its docs
    except Exception:
        config = None
    return config

on_paste(message)

Hook called on a paste event.

The pasted content is assumed to be TOML syntax and tried to be parsed. On success, the config views get updated if the corresponding keys have been pasted.

Parameters:

  • message (Message) –

    message object from the paste event.

Source code in src/elva/widgets/config.py
def on_paste(self, message: Message):
    """
    Hook called on a paste event.

    The pasted content is assumed to be TOML syntax and tried to be parsed.
    On success, the config views get updated if the corresponding keys have been pasted.

    Arguments:
        message: message object from the paste event.
    """
    config = self.decode_content(message.text)
    if config:
        for c in self.config:
            value = config.get(c.name)
            if value is not None:
                c.value = value

ConfigView(widget)

Bases: Container

Wrapper Container around user-facing input widgets.

It allows consistent styling via TCSS classes.

Parameters:

  • widget (Widget) –

    internal user-facing input widget.

Classes:

  • Changed

    Message object posted on change events.

  • Saved

    Message object posted on save events.

Methods:

Attributes:

  • last (Any) –

    Previous value of the configuration parameter.

  • hover (bool) –

    Flag whether the mouse pointer is hovering over this container.

  • focus_within (bool) –

    Flag whether the child widgets have focus.

  • changed (bool) –

    Flag whether the configuration value has changed.

  • name (str) –

    Key of the configuration parameter.

  • value (Any) –

    Value of the configuration parameter.

Source code in src/elva/widgets/config.py
def __init__(self, widget: Widget):
    """
    Arguments:
        widget: internal user-facing input widget.
    """
    super().__init__()
    self.widget = widget

last instance-attribute

Previous value of the configuration parameter.

hover = reactive(False) class-attribute instance-attribute

Flag whether the mouse pointer is hovering over this container.

focus_within = reactive(False) class-attribute instance-attribute

Flag whether the child widgets have focus.

changed property

Flag whether the configuration value has changed.

name property

Key of the configuration parameter.

value property writable

Value of the configuration parameter.

Changed(name, value)

Bases: Message

Message object posted on change events.

Parameters:

  • name (str) –

    key of the configuration parameter.

  • value (Any) –

    value of the configuration parameter.

Attributes:

  • name (str) –

    Key of the configuration parameter.

  • value (Any) –

    Value of the configuration parameter.

Source code in src/elva/widgets/config.py
def __init__(self, name: str, value: Any):
    """
    Arguments:
        name: key of the configuration parameter.
        value: value of the configuration parameter.
    """
    super().__init__()
    self.name = name
    self.value = value
name = name instance-attribute

Key of the configuration parameter.

value = value instance-attribute

Value of the configuration parameter.

Saved(name, value)

Bases: Message

Message object posted on save events.

Parameters:

  • name (str) –

    key of the configuration parameter.

  • value (Any) –

    value of the configuration parameter.

Attributes:

  • name (str) –

    Key of the configuration parameter.

  • value (Any) –

    Value of the configuration parameter.

Source code in src/elva/widgets/config.py
def __init__(self, name: str, value: Any):
    """
    Arguments:
        name: key of the configuration parameter.
        value: value of the configuration parameter.
    """
    super().__init__()
    self.name = name
    self.value = value
name = name instance-attribute

Key of the configuration parameter.

value = value instance-attribute

Value of the configuration parameter.

compose()

Hook arranging child widgets.

Source code in src/elva/widgets/config.py
def compose(self):
    """
    Hook arranging child widgets.
    """
    yield Label(self.name or "")
    yield self.widget

on_mount()

Hook applying the configuration parameters when mounted.

Source code in src/elva/widgets/config.py
def on_mount(self):
    """
    Hook applying the configuration parameters when mounted.
    """
    self.apply()

apply()

Set the last value to the current value.

Source code in src/elva/widgets/config.py
def apply(self):
    """
    Set the last value to the current value.
    """
    self.last = self.value

reset()

Set the current value to the last value.

Source code in src/elva/widgets/config.py
def reset(self):
    """
    Set the current value to the last value.
    """
    self.value = self.last

toggle_button_visibility(state)

Manage the invisible TCSS class on Button widgets.

Parameters:

  • state (bool) –

    if True, remove the invisible TCSS class from button widgets, else add it to them.

Source code in src/elva/widgets/config.py
def toggle_button_visibility(self, state: bool):
    """
    Manage the `invisible` TCSS class on [`Button`][textual.widgets.Button] widgets.

    Arguments:
        state: if `True`, remove the `invisible` TCSS class from button widgets, else add it to them.
    """
    if state:
        self.query(Button).remove_class("invisible")
    else:
        self.query(Button).add_class("invisible")

on_enter(message)

Hook called on pressed Enter key.

This sets the hover flag to True.

Parameters:

  • message (Message) –

    message object posted on the pressed Enter key event.

Source code in src/elva/widgets/config.py
def on_enter(self, message: Message):
    """
    Hook called on pressed `Enter` key.

    This sets the [`hover`][elva.widgets.config.ConfigView.hover] flag to `True`.

    Arguments:
        message: message object posted on the pressed `Enter` key event.
    """
    self.hover = True

on_leave(message)

Hook called on the mouse pointer leaving the widget's bounds.

This sets the hover flag to False only if the mouse has really left the own boundaries and the inner input widget is not focused.

Parameters:

  • message (Message) –

    message object posted on the pressed Enter key event.

Source code in src/elva/widgets/config.py
def on_leave(self, message: Message):
    """
    Hook called on the mouse pointer leaving the widget's bounds.

    This sets the [`hover`][elva.widgets.config.ConfigView.hover] flag to `False` only if the mouse has really left the own boundaries and the inner input widget is not focused.

    Arguments:
        message: message object posted on the pressed `Enter` key event.
    """
    if not self.is_mouse_over and not self.focus_within:
        self.hover = False

watch_hover(hover)

Hook called on hover changed.

This toggles the button visibility accordingly.

Parameters:

Source code in src/elva/widgets/config.py
def watch_hover(self, hover: bool):
    """
    Hook called on hover changed.

    This toggles the button visibility accordingly.

    Arguments:
        hover: current value of [`hover`][elva.widgets.config.ConfigView.hover].
    """
    self.toggle_button_visibility(hover)

on_descendant_focus(message)

Hook called on child widgets gaining focus.

This sets the focus_within flag to True.

Parameters:

  • message (Message) –

    message object posted on focus gain event in child widgets.

Source code in src/elva/widgets/config.py
def on_descendant_focus(self, message: Message):
    """
    Hook called on child widgets gaining focus.

    This sets the [`focus_within`][elva.widgets.config.ConfigView.focus_within] flag to `True`.

    Arguments:
       message: message object posted on focus gain event in child widgets.
    """
    self.focus_within = True

on_descendant_blur(message)

Hook called on child widgets loosing focus.

This sets the focus_within flag to False.

Parameters:

  • message (Message) –

    message object posted on focus loss event in child widgets.

Source code in src/elva/widgets/config.py
def on_descendant_blur(self, message: Message):
    """
    Hook called on child widgets loosing focus.

    This sets the [`focus_within`][elva.widgets.config.ConfigView.focus_within] flag to `False`.

    Arguments:
       message: message object posted on focus loss event in child widgets.
    """
    if not any(node.has_focus for node in self.query()):
        self.focus_within = False

watch_focus_within(focus)

Hook called on focus changed in child widgets.

This toggles the button visibility accordingly.

Parameters:

Source code in src/elva/widgets/config.py
def watch_focus_within(self, focus: bool):
    """
    Hook called on focus changed in child widgets.

    This toggles the button visibility accordingly.

    Arguments:
        focus: current value of [`focus_within`][elva.widgets.config.ConfigView.focus_within].
    """
    self.toggle_button_visibility(focus)

ConfigInput

Bases: Input

Input widget being able to let paste event messages bubble.

_on_paste(message)

Hook called on a paste event.

This allows the message object to bubble up if the pasted content is valid TOML syntax, but does nothing else with it.

Parameters:

  • message (Message) –

    message object posted in a paste event.

Source code in src/elva/widgets/config.py
def _on_paste(self, message: Message):
    """
    Hook called on a paste event.

    This allows the `message` object to bubble up if the pasted content is valid TOML syntax, but does nothing else with it.

    Arguments:
        message: message object posted in a paste event.
    """
    try:
        tomllib.loads(message.text)
    except Exception:
        pass
    else:
        # prevent Input._on_paste() being called,
        # so the Paste message can bubble up to ConfigPanel.on_paste()
        message.prevent_default()

RadioSelect(options, *args, value=None, **kwargs)

Bases: Container

List of options with radio buttons.

Parameters:

  • options (list[tuple[str, Any]]) –

    name-value-tuples holding values alongside their displayable names.

  • value (None | Any, default: None ) –

    current value to select upfront.

  • args (tuple, default: () ) –

    positional arguments passed to the Container class.

  • kwargs (dict, default: {} ) –

    keyword arguments passed to the Container class.

Methods:

  • from_values

    Create a new instance from a list of options.

  • compose

    Hook arranging child widgets.

  • on_click

    Hook called on mouse click event.

Attributes:

Source code in src/elva/widgets/config.py
def __init__(
    self,
    options: list[tuple[str, Any]],
    *args: tuple,
    value: None | Any = None,
    **kwargs: dict,
):
    """
    Arguments:
        options: name-value-tuples holding values alongside their displayable names.
        value: current value to select upfront.
        args: positional arguments passed to the [`Container`][textual.containers.Container] class.
        kwargs: keyword arguments passed to the [`Container`][textual.containers.Container] class.
    """
    super().__init__(*args, **kwargs)
    self.names, self.values = list(zip(*options))
    if value is None:
        value = self.values[0]
    elif value not in self.values:
        raise AttributeError(f"value '{value}' is not in values {self.values}")

    self.buttons = dict(
        (n, RadioButton(n, value=(v == value), name=n)) for n, v in options
    )
    self.options = dict(options)

    self.radio_set = RadioSet()

names instance-attribute

List of names representing the values.

values instance-attribute

List of values at choice.

buttons = dict((n, RadioButton(n, value=v == value, name=n)) for (n, v) in options) instance-attribute

Mapping of RadioButton instances to the values' names.

options = dict(options) instance-attribute

Mapping of values to their corresponding name.

radio_set = RadioSet() instance-attribute

Instance of the inner RadioSet widget.

value property writable

Value of currently active, i.e. selected, radio button.

from_values(options, *args, value=None, **kwargs) classmethod

Create a new instance from a list of options.

Parameters:

  • options (list[Any]) –

    list of values at choice with their string representation as displayed names.

  • value (None | Any, default: None ) –

    current value to select upfront.

  • args (tuple, default: () ) –

    positional arguments passed to the Container class.

  • kwargs (dict, default: {} ) –

    keyword arguments passed to the Container class.

Source code in src/elva/widgets/config.py
@classmethod
def from_values(
    cls, options: list[Any], *args: tuple, value: None | Any = None, **kwargs: dict
):
    """
    Create a new instance from a list of options.

    Arguments:
        options: list of values at choice with their string representation as displayed names.
        value: current value to select upfront.
        args: positional arguments passed to the [`Container`][textual.containers.Container] class.
        kwargs: keyword arguments passed to the [`Container`][textual.containers.Container] class.
    """
    options = [(str(option), option) for option in options]

    return cls(options, *args, value=value, **kwargs)

compose()

Hook arranging child widgets.

Source code in src/elva/widgets/config.py
def compose(self):
    """
    Hook arranging child widgets.
    """
    with self.radio_set:
        for button in self.buttons.values():
            yield button

on_click(message)

Hook called on mouse click event.

This sets the focus to the currently active radio button.

Parameters:

  • message (Message) –

    message object posted in a mouse click event.

Source code in src/elva/widgets/config.py
def on_click(self, message: Message):
    """
    Hook called on mouse click event.

    This sets the focus to the currently active radio button.

    Arguments:
        message: message object posted in a mouse click event.
    """
    self.radio_set.pressed_button.focus()

RadioSelectView(*args, **kwargs)

Bases: ConfigView

Configuration view wrapper around a RadioSelect widget.

Parameters:

Methods:

Source code in src/elva/widgets/config.py
def __init__(self, *args: tuple, **kwargs: dict):
    """
    Arguments:
        args: positional arguments passed to [`RadioSelect`][elva.widgets.config.RadioSelect].
        kwargs: keyword arguments passed to [`RadioSelect`][elva.widgets.config.RadioSelect].

    """
    widget = RadioSelect(*args, **kwargs)
    super().__init__(widget)

compose()

Hook arranging child widgets.

Source code in src/elva/widgets/config.py
def compose(self):
    """
    Hook arranging child widgets.
    """
    with Grid():
        yield Label(self.name or "")
        yield Button("S", id=f"save-{self.name}")
        yield self.widget

on_button_pressed(message)

Hook called on a button pressed event from the child widgets.

This posts then a Saved message.

Parameters:

  • message (Message) –

    message object posted on a button pressed event.

Source code in src/elva/widgets/config.py
def on_button_pressed(self, message: Message):
    """
    Hook called on a button pressed event from the child widgets.

    This posts then a [`Saved`][elva.widgets.config.ConfigView.Saved] message.

    Arguments:
        message: message object posted on a button pressed event.
    """
    self.post_message(self.Saved(self.name, self.value))

on_click(message)

Hook called on a mouse click event.

This sets the focus to the inner radio set widget.

Parameters:

  • message (Message) –

    message object posted on a mouse click event.

Source code in src/elva/widgets/config.py
def on_click(self, message: Message):
    """
    Hook called on a mouse click event.

    This sets the focus to the inner radio set widget.

    Arguments:
        message: message object posted on a mouse click event.
    """
    self.widget.radio_set.focus()

on_radio_set_changed(message)

Hook called when the radio set changes.

This posts then a Changed message itself.

Parameters:

  • message (Message) –

    message object posted on a radio set changed event.

Source code in src/elva/widgets/config.py
def on_radio_set_changed(self, message: Message):
    """
    Hook called when the radio set changes.

    This posts then a [`Changed`][elva.widgets.config.ConfigView.Changed] message itself.

    Arguments:
        message: message object posted on a radio set changed event.
    """
    self.post_message(self.Changed(self.name, self.value))

TextInputView(*args, **kwargs)

Bases: ConfigView

Configuration view wrapper around a ConfigInput widget for generic text input.

Parameters:

Methods:

Source code in src/elva/widgets/config.py
def __init__(self, *args: tuple, **kwargs: dict):
    """
    Arguments:
        args: positional arguments passed to [`ConfigInput`][elva.widgets.config.ConfigInput].
        kwargs: keyword arguments passed to [`ConfigInput`][elva.widgets.config.ConfigInput].
    """
    widget = ConfigInput(*args, **kwargs)
    super().__init__(widget)

compose()

Hook arranging child widgets.

Source code in src/elva/widgets/config.py
def compose(self):
    """
    Hook arranging child widgets.
    """
    with Grid():
        yield Label(self.name or "")
        with Grid():
            yield Button("X", id=f"cut-{self.name}")
            yield Button("C", id=f"copy-{self.name}")
            yield Button("S", id=f"save-{self.name}")
        yield self.widget

on_button_pressed(message)

Hook called on a button pressed event from the child widgets.

This either copies the current value to clipboard or posts a Changed message.

Parameters:

  • message (Message) –

    message object posted on a button pressed event.

Source code in src/elva/widgets/config.py
def on_button_pressed(self, message: Message):
    """
    Hook called on a button pressed event from the child widgets.

    This either copies the current value to clipboard or posts a [`Changed`][elva.widgets.config.ConfigView.Saved] message.

    Arguments:
        message: message object posted on a button pressed event.
    """
    button_id = message.button.id
    cut_id = f"cut-{self.name}"
    copy_id = f"copy-{self.name}"
    save_id = f"save-{self.name}"

    if button_id == cut_id:
        copy_to_clipboard(self.value)
        self.widget.clear()
    elif button_id == copy_id:
        copy_to_clipboard(self.value)
    elif button_id == save_id:
        self.post_message(self.Saved(self.name, self.value))

on_input_changed(message)

Hook called on an input change event.

This posts then a Changed message.

Parameters:

  • message (Message) –

    message object posted on an input change event.

Source code in src/elva/widgets/config.py
def on_input_changed(self, message: Message):
    """
    Hook called on an input change event.

    This posts then a [`Changed`][elva.widgets.config.ConfigView.Saved] message.

    Arguments:
        message: message object posted on an input change event.
    """
    self.post_message(self.Changed(self.name, self.value))

URLInputView(*args, **kwargs)

Bases: TextInputView

Configuration view wrapper around a ConfigInput widget for URLs.

Parameters:

Methods:

Attributes:

  • value

    Value of the input field if being a valid URL.

Source code in src/elva/widgets/config.py
def __init__(self, *args: tuple, **kwargs: dict):
    """
    Arguments:
        args: positional arguments passed to [`ConfigInput`][elva.widgets.config.ConfigInput].
        kwargs: keyword arguments passed to [`ConfigInput`][elva.widgets.config.ConfigInput].
    """
    super().__init__(*args, **kwargs)
    self.is_valid = True

value property writable

Value of the input field if being a valid URL.

on_input_changed(message)

Hook called on an input change event.

This posts then a Changed message if no validation result is present, i.e. no Validator has been passed to the constructor, or the validation succeeded. Additionally, the TCSS class invalid is added or removed to the input widget depending on the validation result.

Parameters:

  • message (Message) –

    message object posted on an input change event.

Source code in src/elva/widgets/config.py
def on_input_changed(self, message: Message):
    """
    Hook called on an input change event.

    This posts then a [`Changed`][elva.widgets.config.ConfigView.Saved] message if no validation result is present, i.e. no [`Validator`][textual.validation.Validator] has been passed to the constructor, or the validation succeeded.
    Additionally, the TCSS class `invalid` is added or removed to the input widget depending on the validation result.

    Arguments:
        message: message object posted on an input change event.
    """
    validation_result = message.validation_result
    if validation_result is not None:
        if validation_result.is_valid:
            self.is_valid = True
            self.widget.remove_class("invalid")
            self.post_message(self.Changed(self.name, self.value))
        else:
            self.is_valid = False
            self.widget.add_class("invalid")
    else:
        self.post_message(self.Changed(self.name, self.value))

PathInputView(value, *args, **kwargs)

Bases: TextInputView

Configuration view wrapper around a ConfigInput widget for paths.

Parameters:

  • value (Any) –

    currently set value in the input field.

  • args (tuple, default: () ) –

    positional arguments passed to ConfigInput.

  • kwargs (dict, default: {} ) –

    keyword arguments passed to ConfigInput.

Attributes:

  • value

    Path object of the current value in the input field.

Source code in src/elva/widgets/config.py
def __init__(self, value: Any, *args: tuple, **kwargs: dict):
    """
    Arguments:
        value: currently set value in the input field.
        args: positional arguments passed to [`ConfigInput`][elva.widgets.config.ConfigInput].
        kwargs: keyword arguments passed to [`ConfigInput`][elva.widgets.config.ConfigInput].
    """
    super().__init__()
    value = str(value) if value is not None else None
    self.widget = ConfigInput(value, *args, **kwargs)

value property writable

Path object of the current value in the input field.

SwitchView(*args, **kwargs)

Bases: ConfigView

Configuration view wrapper around a Switch widget.

Parameters:

  • args (tuple, default: () ) –

    positional arguments passed to Switch.

  • kwargs (dict, default: {} ) –

    keyword arguments passed to Switch.

Methods:

  • compose

    Hook arranging child widgets.

  • on_button_pressed

    Hook called on a button pressed event from the child widgets.

Source code in src/elva/widgets/config.py
def __init__(self, *args: tuple, **kwargs: dict):
    """
    Arguments:
        args: positional arguments passed to [`Switch`][textual.widgets.Switch].
        kwargs: keyword arguments passed to [`Switch`][textual.widgets.Switch].
    """
    widget = Switch(*args, **kwargs)
    super().__init__(widget)

compose()

Hook arranging child widgets.

Source code in src/elva/widgets/config.py
def compose(self):
    """
    Hook arranging child widgets.
    """
    with Grid():
        yield Label(self.name or "")
        yield Button("S", id=f"save-{self.name}")
        with Container():
            yield self.widget

on_button_pressed(message)

Hook called on a button pressed event from the child widgets.

This posts then a Saved message.

Parameters:

  • message (Message) –

    message object posted on a button pressed event.

Source code in src/elva/widgets/config.py
def on_button_pressed(self, message: Message):
    """
    Hook called on a button pressed event from the child widgets.

    This posts then a [`Saved`][elva.widgets.config.ConfigView.Saved] message.

    Arguments:
        message: message object posted on a button pressed event.
    """
    self.post_message(self.Saved(self.name, self.value))

QRCode(content, *args, collapsed=True, **kwargs)

Bases: Widget

Collapsible QR code displaying widget.

Parameters:

  • content (str) –

    the content to encode in the QR code.

  • collapsed (bool, default: True ) –

    flag whether the view is collapsed on mount.

  • args (tuple, default: () ) –

    positional arguments passed to the Widget class.

  • kwargs (dict, default: {} ) –

    keyword arguments passed to the Widget class.

Methods:

Attributes:

  • qr (QRCode) –

    Instance of the QR code encoding object.

  • code (Static) –

    Widget instance holding the string representation of the QR code.

  • value

    QR code encoded data.

Source code in src/elva/widgets/config.py
def __init__(
    self, content: str, *args: tuple, collapsed: bool = True, **kwargs: dict
):
    """
    Arguments:
        content: the content to encode in the QR code.
        collapsed: flag whether the view is collapsed on mount.
        args: positional arguments passed to the [`Widget`][textual.widget.Widget] class.
        kwargs: keyword arguments passed to the [`Widget`][textual.widget.Widget] class.
    """
    super().__init__(*args, **kwargs)

    self.version = 1
    self.qr = qrcode.QRCode(
        version=self.version,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        border=0,
    )
    self.code = Static()
    self.collapsible = Collapsible(title="QR", collapsed=collapsed)
    self.value = content

qr = qrcode.QRCode(version=self.version, error_correction=qrcode.constants.ERROR_CORRECT_L, border=0) instance-attribute

Instance of the QR code encoding object.

code = Static() instance-attribute

Widget instance holding the string representation of the QR code.

value = content class-attribute instance-attribute

QR code encoded data.

compose()

Hook arrange the child widgets.

Source code in src/elva/widgets/config.py
def compose(self):
    """
    Hook arrange the child widgets.
    """

    with self.collapsible:
        yield self.code

update_qrcode()

Update the displayed QR code.

Source code in src/elva/widgets/config.py
def update_qrcode(self):
    """
    Update the displayed QR code.
    """
    qr = self.qr
    qr.clear()

    f = io.StringIO()

    qr.version = self.version
    qr.add_data(self.value)
    qr.print_ascii(out=f)
    self.code.update(f.getvalue())

watch_value()

Hook called on changed value.

This updates the displayed QR code from the new value.

Source code in src/elva/widgets/config.py
def watch_value(self):
    """
    Hook called on changed value.

    This updates the displayed QR code from the new value.
    """
    self.update_qrcode()

QRCodeView(*args, **kwargs)

Bases: ConfigView

Configuration view wrapper around a QRCode widget.

Parameters:

  • args (tuple, default: () ) –

    positional arguments passed to QRCode.

  • kwargs (dict, default: {} ) –

    keyword arguments passed to QRCode.

Methods:

  • compose

    Hook arranging child widgets.

  • on_button_pressed

    Hook called on a button pressed event from the child widgets.

  • on_click

    Hook called on a mouse click event.

Source code in src/elva/widgets/config.py
def __init__(self, *args: tuple, **kwargs: dict):
    """
    Arguments:
        args: positional arguments passed to [`QRCode`][elva.widgets.config.QRCode].
        kwargs: keyword arguments passed to [`QRCode`][elva.widgets.config.QRCode].
    """
    widget = QRCode(*args, **kwargs)
    super().__init__(widget)

compose()

Hook arranging child widgets.

Source code in src/elva/widgets/config.py
def compose(self):
    """
    Hook arranging child widgets.
    """
    with Grid():
        yield Label(self.name or "")
        yield Button("C", id=f"copy-{self.name or 'qrcode'}")
        yield self.widget

on_button_pressed(message)

Hook called on a button pressed event from the child widgets.

This copies the current QR encoded value to clipboard.

Parameters:

  • message (Message) –

    message object posted on a button pressed event.

Source code in src/elva/widgets/config.py
def on_button_pressed(self, message: Message):
    """
    Hook called on a button pressed event from the child widgets.

    This copies the current QR encoded value to clipboard.

    Arguments:
        message: message object posted on a button pressed event.
    """
    copy_to_clipboard(self.value)

on_click(message)

Hook called on a mouse click event.

This toggles the collapsed state.

Parameters:

  • message (Message) –

    message object posted on a mouse click event.

Source code in src/elva/widgets/config.py
def on_click(self, message: Message):
    """
    Hook called on a mouse click event.

    This toggles the collapsed state.

    Arguments:
        message: message object posted on a mouse click event.
    """
    collapsible = self.query_one(Collapsible)
    collapsible.collapsed = not collapsible.collapsed

WebsocketsURLValidator

Bases: Validator

Websocket URL validator.

This class is designed for use in the URLInputView.

Methods:

  • validate

    Hook called when validation is requested.

validate(value)

Hook called when validation is requested.

Parameters:

  • value (str) –

    current input field value to be validated.

Returns:

  • ValidationResult

    a result object holding information about the validation outcome.

Source code in src/elva/widgets/config.py
def validate(self, value: str) -> ValidationResult:
    """
    Hook called when validation is requested.

    Arguments:
        value: current input field value to be validated.

    Returns:
        a result object holding information about the validation outcome.
    """
    if value:
        try:
            parse_uri(value)
        except Exception as exc:
            return self.failure(description=str(exc))
        else:
            return self.success()
    else:
        return self.success()

PathSuggester

Bases: Suggester

Suggester for paths.

This class is designed for use in the PathInputView widget.

Methods:

get_suggestion(value) async

Hook called when a suggestion is requested.

Parameters:

  • value (str) –

    current input widget value on which to base suggestions on.

Returns:

  • str

    suggested extended or completed path.

Source code in src/elva/widgets/config.py
async def get_suggestion(self, value: str) -> str:
    """
    Hook called when a suggestion is requested.

    Arguments:
        value: current input widget value on which to base suggestions on.

    Returns:
        suggested extended or completed path.
    """
    path = Path(value)

    if path.is_dir():
        dir = path
    else:
        dir = path.parent

    try:
        _, dirs, files = next(dir.walk())
    except StopIteration:
        return value

    names = sorted(dirs) + sorted(files)
    try:
        name = next(filter(lambda n: n.startswith(path.name), names))
    except StopIteration:
        if path.is_dir():
            name = names[0] if names else ""
        else:
            name = path.name

    if value.startswith("."):
        prefix = "./"
    else:
        prefix = ""

    return prefix + str(dir / name)