Skip to content

file_chooser

anomaly_match_ui.widgets.file_chooser

Styled file/folder chooser wrapper for AnomalyMatch UI.

AMFileChooser(path='', *, filename='', select_default=False, show_only_dirs=False, filter_pattern='', title='', purpose='')

Bases: VBox

Dark-themed file/folder chooser for AnomalyMatch UI.

Wraps ipyfilechooser.FileChooser with ESA-branded dark styling scoped via the .am-file-chooser CSS class.

Parameters:

Name Type Description Default
path str

Initial directory to display.

''
select_default bool

Whether to pre-select the default path.

False
show_only_dirs bool

If True, only directories are shown.

False
filter_pattern str

Glob pattern to filter files (e.g. "*.pth").

''
title str

Optional label displayed above the chooser.

''
purpose str

Stable key identifying this chooser site (e.g. "model_chooser", "source_chooser"). When set, the chooser writes through the parent directory of every confirmed selection to :mod:anomaly_match_ui.utils.ui_state, so that the next time the user opens a chooser with the same purpose it can default to where they were last browsing instead of the original cfg value's directory (#429).

''
Source code in anomaly_match_ui/widgets/file_chooser.py
def __init__(
    self,
    path: str = "",
    *,
    filename: str = "",
    select_default: bool = False,
    show_only_dirs: bool = False,
    filter_pattern: str = "",
    title: str = "",
    purpose: str = "",
) -> None:
    if not path:
        path = os.getcwd()

    fc_kwargs: dict = {
        "path": path,
        "select_default": select_default,
        "show_only_dirs": show_only_dirs,
    }
    if filename:
        fc_kwargs["filename"] = filename
    if filter_pattern:
        fc_kwargs["filter_pattern"] = filter_pattern

    self._file_chooser = FileChooser(**fc_kwargs)
    self._purpose = purpose
    # ipyfilechooser.FileChooser.register_callback **replaces** the
    # callback rather than appending — so we own a list and dispatch
    # to all registered handlers from a single bound shim.  Without
    # this, every screen-side ``register_callback`` overwrites the
    # persistence hook and the last-browsed dir never gets recorded
    # (#429 follow-up).
    self._callbacks: list[Callable] = []
    self._file_chooser.register_callback(self._dispatch_callbacks)
    if purpose:
        self._callbacks.append(self._persist_browsed_dir)

    # Inject CSS only once across all instances
    if AMFileChooser._style_widget is None:
        AMFileChooser._style_widget = widgets.HTML(value=_CHOOSER_STYLE)

    children: list[widgets.Widget] = [AMFileChooser._style_widget]

    if title:
        children.append(
            widgets.HTML(
                value=(
                    f'<div style="color:{TEXT_COLOR}; font-size:13px;'
                    f' font-weight:bold; padding:4px 0;">{title}</div>'
                ),
            )
        )

    children.append(self._file_chooser)

    super().__init__(
        children=children,
        layout=widgets.Layout(
            min_height="120px",
            max_height="280px",
            width="100%",
            max_width="100%",
        ),
    )
    self.add_class("am-file-chooser")

selected property

Return the currently selected path, or None.

selected_filename property

Return the selected filename component.

register_callback(callback)

Observe selection changes.

Multiple callers can register; each is invoked in registration order on every selection. An exception in one does not stop the rest from firing.

Parameters:

Name Type Description Default
callback Callable

Called with the underlying FileChooser instance whenever the user clicks Select.

required
Source code in anomaly_match_ui/widgets/file_chooser.py
def register_callback(self, callback: Callable) -> None:
    """Observe selection changes.

    Multiple callers can register; each is invoked in registration
    order on every selection.  An exception in one does not stop
    the rest from firing.

    Args:
        callback: Called with the underlying ``FileChooser``
            instance whenever the user clicks Select.
    """
    self._callbacks.append(callback)

reset(path=None)

Reset the chooser, optionally to a new path.

Parameters:

Name Type Description Default
path str | None

New starting directory. Uses the current directory if None.

None
Source code in anomaly_match_ui/widgets/file_chooser.py
def reset(self, path: str | None = None) -> None:
    """Reset the chooser, optionally to a new *path*.

    Args:
        path: New starting directory. Uses the current directory if ``None``.
    """
    if path:
        self._file_chooser.reset(path)
    else:
        self._file_chooser.reset()