Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions docs/build-publish-packages.rst

This file was deleted.

25 changes: 25 additions & 0 deletions docs/build-publish.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Build & Publish
===============

Conda packages
--------------

Conda packages are supported; all build and publishing steps are defined as
Pixi tasks:

.. code-block:: bash

pixi run conda-build
pixi run conda-publish

Conda packages are published to the `anaconda.org/neutrons <https://anaconda.org/neutrons>`_
organization.

Documentation
-------------

Build the documentation with:

.. code-block:: bash

pixi run build-docs
6 changes: 0 additions & 6 deletions docs/documentation.rst

This file was deleted.

3 changes: 1 addition & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ experiment's runs, and (soon) assign runs to roles and export a setup file.
usage
getting-started
testing
documentation
build-publish-packages
build-publish
license
indices-and-tables
9 changes: 9 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ pyoncatng's `OncatLogin` and `IPTSTable`:
1. Click **Connect** and approve the ONCat sign-in in your browser.
2. Type an IPTS number (e.g. `24703`) and click **Load** to fetch that
experiment's USANS runs.
3. Select runs in the table: click selects one run, Ctrl/Cmd+click toggles a run,
Shift+click selects a range, and double-click selects every run with the same
title. Ctrl/Cmd+double-click adds that title group to the selection.
4. Click **Fetch Runs** to capture the highlighted runs. The button stays
disabled until the table has a selection, and the captured runs are ordered
by increasing run number regardless of the order they were selected in.
Loading another IPTS clears the selection and disables the button again;
the runs fetched before it are kept. Each click replaces the previous
capture, so the widget holds exactly the runs highlighted at the last click.

Assigning runs to roles (sample / background / empty cell / empty beam) and
exporting the `usansred` JSON configuration are planned for subsequent
Expand Down
5 changes: 3 additions & 2 deletions src/usansemble/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def index() -> None:
ui.label("usansemble").classes("text-h5")
ui.markdown(
"Sign in to ONCat, then enter an IPTS number (e.g. **24703**) and click "
"**Load** to fetch its USANS runs. Selecting runs and exporting a "
"`usansred` configuration will come next."
"**Load** to fetch its USANS runs. Use the table's selection gestures "
"to choose runs, then click **Fetch Runs** to capture them; assigning "
"them to roles and exporting a `usansred` configuration will come next."
)
RunsSelector(facility="SNS", instrument="USANS")
123 changes: 121 additions & 2 deletions src/usansemble/widgets/runs_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@
(click/Ctrl+click/Shift+click) this widget adds a **double-click** that selects
every run sharing the double-clicked run's title, since the runs of one USANS
measurement share a title -- see :data:`_SELECT_BY_TITLE_JS`.

A **Fetch Runs** button below the table captures the current selection. AG Grid
returns selected rows in the order the selection was built, so the captured rows
are sorted by increasing run number before being stored. Consumers read them
through :attr:`RunsSelector.fetched_runs` or subscribe with
:meth:`RunsSelector.on_runs_fetched`. The button is enabled only while the table
holds a selection: loading another IPTS rebuilds the grid and drops the
selection, which disables it again.
"""

import copy
import json
from typing import Callable, Optional
from typing import Any, Callable, List, Optional

from nicegui import ui
from pyoncatng.configuration import get_data
from pyoncatng.widgets.iptstable import IPTSTable

# ``IPTSTable`` always prepends its key column (the run number) to the caller's
# processing variables, so the ID column name is not in PROCESSING_VARIABLES
# below; alias the widget's own constant to stay in step with it.
from pyoncatng.widgets.iptstable import KEY_COLUMN as ID_COLUMN
from pyoncatng.widgets.iptstable import IPTSTable, Row
from pyoncatng.widgets.login import OncatLogin

PROCESSING_VARIABLES = (
Expand Down Expand Up @@ -65,6 +79,23 @@
"Ctrl/Cmd+double-click adds them to the selection"
)

# The button capturing the selection, and the two outcomes it reports.
FETCH_BUTTON_LABEL = "Fetch Runs"
FETCHED_MESSAGE = "Fetched {n} run(s)."
NO_SELECTION_MESSAGE = "Select at least one run first."


def _copy_rows(rows: List[Row]) -> List[Row]:
"""Deep-copy a row list, so nothing in it is shared with the caller.

Row values are scalars today (run number, title, timestamp, counts), but a
row holds whatever the ONCat projection returned (``IPTSTable.rows_from_runs``
copies ``run.get(path)`` verbatim), and a metadata path may yield a list or a
nested dict. Copying in depth keeps the boundary intact whatever the columns
are configured to fetch.
"""
return copy.deepcopy(list(rows))


class RunsSelector(ui.column):
"""An ``OncatLogin`` card above an ``IPTSTable``, packaged as one widget.
Expand Down Expand Up @@ -111,6 +142,8 @@ def __init__(
self._instrument = instrument
self._table_height = table_height
self._login_orientation = login_orientation
self._fetched_runs: List[Row] = []
self._fetch_callbacks: List[Callable[[List[Row]], None]] = []
self._build_ui()

# -- public integration surface ----------------------------------------
Expand All @@ -130,6 +163,21 @@ def agent(self):
"""The authenticated ONCat agent, for consumers to query."""
return self._login.agent

@property
def fetched_runs(self) -> List[Row]:
"""The runs captured by the last **Fetch Runs**, by increasing run number.

Each **Fetch Runs** click replaces this with whatever is highlighted at
the time; nothing else does. Loading another IPTS or signing out leaves
the last capture in place, so it stays available to the later steps of
the config-assembly flow until the user fetches again.

Deep copies of the stored rows, so a consumer cannot mutate the captured
selection -- the same boundary ``RunTable`` draws around its own rows.
Empty until the button is first clicked with a selection.
Comment thread
jmborr marked this conversation as resolved.
Resolved
"""
return _copy_rows(self._fetched_runs)

def on_connection_change(self, callback: Callable[[bool], None]) -> None:
"""Register a callback fired with the connected bool on every change.

Expand All @@ -138,6 +186,14 @@ def on_connection_change(self, callback: Callable[[bool], None]) -> None:
"""
self._login.on_connection_change(callback)

def on_runs_fetched(self, callback: Callable[[List[Row]], None]) -> None:
"""Register a callback fired with the fetched runs on every fetch.

The callback receives the same sorted list :attr:`fetched_runs` returns.
Callbacks are synchronous, matching ``on_connection_change``.
"""
self._fetch_callbacks.append(callback)

# -- UI construction ----------------------------------------------------

def _build_ui(self) -> None:
Expand All @@ -163,6 +219,7 @@ def _build_ui(self) -> None:
)
self._enable_title_double_click()
self._add_selection_help()
self._add_fetch_button()

def _enable_title_double_click(self) -> None:
"""Make a double-click select every row sharing the clicked row's title.
Expand All @@ -186,3 +243,65 @@ def _add_selection_help(self) -> None:
"""
with self._table:
self._selection_help = ui.label(SELECTION_HELP).classes("text-xs text-gray-500")

def _add_fetch_button(self) -> None:
"""Add the **Fetch Runs** button and its status line below the table.

Built in the ``RunsSelector`` column rather than inside the table card,
so it sits under the card. The button stays disabled until the table has
a selection; the enabling is driven by the table's selection-change hook.
"""
self._fetch_button = ui.button(FETCH_BUTTON_LABEL, on_click=self._on_fetch)
self._fetch_button.set_enabled(False)
self._fetch_status = ui.label("").classes("text-xs text-gray-500")
self._fetch_status.set_visibility(False)
self._table.on_selection_change(self._on_selection_change)
# A Load replaces the rows through NiceGUI's aggrid update method, which
# destroys and recreates the grid rather than patching it; the selection
# is dropped without a selectionChanged event, so the button has to be
# reset on the rebuild instead. ``gridReady`` fires on every rebuild.
# ``IPTSTable`` keeps its grid private, hence the reach into ``_table``.
self._table._table.on("gridReady", self._on_table_rebuilt)

# -- fetching -----------------------------------------------------------

def _set_fetch_status(self, text: str) -> None:
"""Show (or clear, when ``text`` is empty) the fetch status line."""
self._fetch_status.set_text(text)
self._fetch_status.set_visibility(bool(text))

async def _on_selection_change(self, _event: Any = None) -> None:
"""Enable the fetch button only while the table has a selection."""
rows = await self._table.selected_rows()
self._fetch_button.set_enabled(bool(rows))

def _on_table_rebuilt(self, _event: Any = None) -> None:
"""Disable the fetch button whenever the grid is rebuilt.

Loading another IPTS replaces the rows, so any highlighted row numbers
now address different runs and the selection must not be fetchable.
Already-fetched runs are kept: they were captured deliberately, and the
status line still describes them.
"""
self._fetch_button.set_enabled(False)

async def _on_fetch(self, _event: Any = None) -> None:
"""Capture the selected runs, ordered by increasing run number.

``selected_rows()`` reaches AG Grid's ``getSelectedRows``, which returns
the rows in the order the selection was built (Ctrl+click order, or the
order the title double-click handler selected them), so the rows are
sorted here. ``int`` keeps the ordering numeric should ONCat ever report
the run number as a string.
"""
rows = await self._table.selected_rows()
if not rows:
# Reaching here means the enabled state was stale, so correct it and
# report the reason. Any previously fetched runs are left intact.
self._fetch_button.set_enabled(False)
self._set_fetch_status(NO_SELECTION_MESSAGE)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return
Comment thread
jmborr marked this conversation as resolved.
Resolved
self._fetched_runs = sorted(_copy_rows(rows), key=lambda row: int(row[ID_COLUMN]))
self._set_fetch_status(FETCHED_MESSAGE.format(n=len(self._fetched_runs)))
for callback in self._fetch_callbacks:
callback(self.fetched_runs)
Loading
Loading