DataWidgetFactory
DataWidgetFactory builds the widgets of a view from a Store. Its widgets read the rows of the Store, save the edits to it, and share the selected rows: selecting a note in a table shows its text in a text widget and its fields in a form. Review entities builds a first view with it.
from metanno import DataRelation, DataWidgetFactory
factory = DataWidgetFactory(
store, relations=[DataRelation("notes", "entities", on="note_id")]
)
relations links the tables of the Store, here the entities to their note, see Annotate across levels.
Widgets
| Method | Shows | Used in |
|---|---|---|
create_table_widget | the rows of a table, to filter, sort and edit | Annotate a table |
create_text_widget | the text of the selected row with its entities highlighted, and a toolbar of labels | Review entities |
create_form_widget | the fields of the selected row, with buttons to go to the previous or next row | Design your view |
create_selected_field_view | one field of the selected row, such as the ID of a note above its text | Design your view |
create_pdf_widget | the PDF of the selected row, in the PDF viewer of the browser | see below |
create_status_bar | the last edit Python refused, and whether the view is connected to Python | Design your view |
Tables, texts and forms take a fields list, which picks the columns to show and how to edit them, see Choose how each field is shown.
The PDF widget asks a Python function for the document of the selected row, for instance to show the original report next to its extracted text. The function receives the row and returns the PDF as bytes, after converting other formats, such as scans, if needed:
from pathlib import Path
def load_report(note):
"""
Read the PDF of a note from the folder of the original reports
"""
return Path(f"reports/{note['note_id']}.pdf").read_bytes()
pdf = factory.create_pdf_widget(
store_key="notes", primary_key="note_id", loader=load_report
)
Behavior in the interface
The widgets behave the same way in every view:
- a checkbox or a choice is saved as soon as it is clicked, a text field when you press Enter or click elsewhere, while Esc, or selecting another row, cancels it
- when several rows are selected, changing a field changes it for all of them, as one edit
- Ctrl+Z undoes the last change made in the view and Ctrl+Y or Ctrl+Shift+Z redoes it, with Cmd on a Mac, while inside a text field these keys undo the typing
- when Python refuses an edit, for instance because someone else changed the same cell, the field shows why. A Metanno tab shows the last refusal under the view, add
create_status_barto views shown in a notebook or a web app - the text widget shows the document of the selected row, or with
docs_mode="selected"every selected document, and withdocs_mode="all"all the documents of the table, or those under the selected parent row of a related table - a table related to other tables by
DataRelationshows the rows under the selected row of one of them, chosen above it among those with a selected row, e.g. , and for notes. While no stay is selected, the notes of the selected patient show. Clicking a note selects and scrolls to its stay and its patient in their tables, which then show the stays of that patient, while the notes stay shown until is clicked
Reference
Build tables, forms and annotation views that share a PRET Store.
Widgets created by this factory edit the same data and share row selection. The factory keeps selection separately from the data so navigation does not enter the data undo history.
Parameters
| PARAMETER | DESCRIPTION |
|---|---|
data | Existing PRET Store, or initial mapping of row lists and DataFrames TYPE: |
sync | Used when creating a Store from initial data. False or None keeps edits in the browser. True synchronizes with Python without saving to disk. A file path also saves edits and lets other Python processes open the same Store TYPE: |
relations | Parent and child columns used to filter linked tables TYPE: |
read_only | Disable mutations from these widgets without changing notebook access to the Store TYPE: |
create_table_widget [source]
Create a Table widget bound to a store entry. Field metadata is inferred from the underlying data, with optional callbacks for selection changes, hover, and cell content clicks. An imperative handle can be exposed for scrolling and filtering.
Parameters
| PARAMETER | DESCRIPTION |
|---|---|
store_key | Key pointing to the list-like store to render. TYPE: |
primary_key | Column identifying rows, shared selections store its values. Required for lists of rows, with None a DataFrame table clears its selection when its rows change. TYPE: |
fields | Field metadata used for rendering and editing. Expects:
TYPE: |
style | Inline style of the table container, which fills its parent unless these keys override it, e.g. a fixed height in a notebook output. TYPE: |
handle | Imperative handle exposing helpers :
TYPE: |
accept_related_filter_keys | Controls whether this table accepts filters broadcast by other views when the factory declares no relation for them, tables linked by a DataRelation follow the selection of their parent instead. TYPE: |
on_selection_change | Called when focus moves inside the table.
TYPE: |
on_mouse_hover_row | Called when the mouse hovers over a row.
TYPE: |
on_click_cell_content | Called when hyperlink-like content inside a cell is clicked.
TYPE: |
source_requery | Show an action for fetching the DataFrame source again. TYPE: |
include_unconfigured_columns | Show columns missing from fields without allowing edits to them. TYPE: |
follow_parent_selection | For the child of a TYPE: |
parent_row_name | Name of one row of the parent table, e.g. TYPE: |
default_sort | Columns ordering the rows whenever the user has not sorted the table by a column header, when it opens and after Clear sort, e.g. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Renderable | A Pret component instance rendering the configured table. |
create_text_widget [source]
Build a text annotation widget that pairs a text store with a spans store. Provides toolbar buttons for creating spans, keyboard navigation between documents, and callbacks for hover and click events.
Parameters
| PARAMETER | DESCRIPTION |
|---|---|
store_text_key | Key pointing to the store containing documents. TYPE: |
store_spans_key | Key pointing to the store containing span annotations. If TYPE: |
text_key | Field name holding the raw text to annotate. TYPE: |
text_primary_key | Field name that uniquely identifies each text document. TYPE: |
spans_primary_key | Field name that uniquely identifies each span, required with TYPE: |
fields | Field metadata for span fields, used in the inline editor. TYPE: |
begin_key | Field name for span start offsets. TYPE: |
end_key | Field name for span end offsets. TYPE: |
style_key | Field name for span styling configuration key. Spans without it use the style of their TYPE: |
label_key | Field name for span display label. TYPE: |
label_formatter | Optional function used to compute the rendered label text for each span. It receives the full span dictionary and should return a string. If left as None, the uppercased TYPE: |
button_key | Field name that annotation buttons will update. TYPE: |
labels | Mapping of label name to styling config (e.g. color, shortcut). TYPE: |
style | Inline style overrides for the text widget wrapper and content panes. TYPE: |
handle | Imperative handle exposing helpers:
TYPE: |
on_change_text_id | Called when navigation changes the active document.
TYPE: |
on_add_span | Called after new spans are created from the current selection.
TYPE: |
on_hover_spans | Called whenever spans are hovered.
TYPE: |
on_click_span | Called when a span is clicked.
TYPE: |
on_select_text | Called when the text selected in a document changes, including when it is cleared.
TYPE: |
docs_mode | Controls which documents are displayed:
TYPE: |
container_renderer | Optional callback used to wrap each rendered document. It receives TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Tuple[Renderable, Renderable] | A pair of Pret components: the toolbar widget and the text widget. |
create_form_widget [source]
Render a single record from a store as a classic form with text, select, and boolean inputs. The current row can be changed via the imperative handle to keep multiple widgets in sync. When multiple rows are selected in a linked table, fields show mixed values and edits are applied to every selected row.
Parameters
| PARAMETER | DESCRIPTION |
|---|---|
store_key | Key pointing to the collection to edit TYPE: |
primary_key | Field name that uniquely identifies each row (primary key) TYPE: |
fields | Field metadata used for rendering and editing:
An empty list shows every column of a DataFrame, editable when the DataFrame is TYPE: |
style | Inline style overrides for the wrapping container, which has an 8px padding by default TYPE: |
min_input_width | Minimum width applied to each input control TYPE: |
add_navigation_buttons | Whether to display previous/next navigation buttons TYPE: |
allow_delete | Show a button that deletes the selected rows in one Store action TYPE: |
handle | Imperative handle exposing helpers:
TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Renderable | A Pret component instance rendering the editable form |
create_selected_field_view [source]
Render a single field from the currently selected row in a store.
Parameters
| PARAMETER | DESCRIPTION |
|---|---|
store_key | Key pointing to the collection to display TYPE: |
shown_key | Field key to show from the current row TYPE: |
fallback | Value returned when no row is available TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Renderable | Pret component instance displaying the selected field value |
create_pdf_widget [source]
Show the PDF of the lead selected row of a collection in the browser PDF viewer, e.g. beside a documents table.
Parameters
| PARAMETER | DESCRIPTION |
|---|---|
store_key | Key of the collection or DataFrame whose selected row is shown TYPE: |
primary_key | Field identifying the rows, as in the table showing them TYPE: |
loader | Called in Python with the selected row, returns the PDF bytes, e.g. read from a folder of downloaded files. Other formats, such as scans or DOCX files, can be converted to PDF there. TYPE: |
style | Inline style of the container, which fills its parent unless these keys override it TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
Renderable | A Pret component showing the PDF, or a loading, empty or error message |
create_status_bar [source]
Return a bar showing the last rejected edit or undo of the Store and its connection to Python, place it below the widgets of a notebook output or an app. Factory widgets do not show Store errors themselves
Parameters
| PARAMETER | DESCRIPTION |
|---|---|
show_connection | Also show whether the Store is connected, disconnected or offline TYPE: |