AnnotatedText[source]
The AnnotatedText is a rich text viewer that supports span-level annotations, nested token highlights, and various user event callbacks.
Examples
from pret import (
    component,
    create_store,
    use_store_snapshot,
    use_event_callback,
    use_state,
)
from pret.ui.metanno import AnnotatedText
from pret.ui.joy import Button, Box
txt = (
    "Metanno brings annotation components to Pret\n"
    "to build tailored annotation tools."
)
# One span covering the word “Metanno”
spans = create_store(
    [
        {
            "id": f"span-0-7",
            "begin": 0,
            "end": 7,
            "label": "OBJ",
            "highlighted": False,
        }
    ]
)
txt_annotation_styles = create_store(
    {
        "OBJ": {
            "color": "red",
            "shape": "underline",
        }
    }
)
@component
def MyText():
    tracked_spans = use_store_snapshot(spans)
    tracked_styles = use_store_snapshot(txt_annotation_styles)
    @use_event_callback
    def handle_select(ranges, modkeys):
        for sp in ranges:
            spans.extend(
                [
                    {
                        "id": f"span-{sp['begin']}-{sp['end']}",
                        "begin": sp["begin"],
                        "end": sp["end"],
                        "label": "OBJ",
                    }
                ]
            )
    def on_mouse_enter_span(span_id, modkeys):
        for i, sp in enumerate(spans):
            if sp["id"] == span_id:
                spans[i]["highlighted"] = True
    def on_mouse_leave_span(span_id, modkeys):
        for i, sp in enumerate(spans):
            if sp["id"] == span_id:
                spans[i]["highlighted"] = False
    def on_span_style_change():
        old_style = txt_annotation_styles["OBJ"]["shape"]
        new_style = "box" if old_style == "underline" else "underline"
        txt_annotation_styles["OBJ"]["shape"] = new_style
    return Box(
        Button("Change style", on_click=on_span_style_change),
        Button("Remove annotations", on_click=lambda: spans.clear()),
        AnnotatedText(
            text=txt,
            spans=tracked_spans,
            annotation_styles=tracked_styles,
            on_mouse_select=handle_select,
            on_mouse_enter_span=on_mouse_enter_span,
            on_mouse_leave_span=on_mouse_leave_span,
            style={"gridColumn": "1 / -1"},
        ),
        sx={
            "p": 1,
            "display": "grid",
            "gridTemplateColumns": "repeat(2, auto)",
            "gap": 1,
        },
    )
MyText()
Parameters
| PARAMETER | DESCRIPTION | 
|---|---|
| text | Raw text content shown in the viewer.   TYPE:  | 
| spans | Span-level annotations over  
   TYPE:  | 
| annotation_styles | Named style presets that control span background color, border, label placement, etc. Each style may define properties such as: 
   TYPE:  | 
| mouse_selection | Current mouse drag selection expressed as character‐offset ranges. Passed to    TYPE:  | 
| style | CSS style overrides for the outer element.   TYPE:  | 
| actions | Optional imperative helpers (   TYPE:  | 
| key | React reconciliation key.   TYPE:  | 
| on_click_span | Called when the user clicks on a span. 
   TYPE:  | 
| on_key_press | Called when a key is pressed with focus inside the component. 
   TYPE:  | 
| on_mouse_enter_span | Called when the mouse pointer enters a span. 
   TYPE:  | 
| on_mouse_leave_span | Called when the mouse pointer leaves a span. 
   TYPE:  | 
| on_mouse_select | Triggered when the user finishes selecting text with the mouse. 
   TYPE:  |