Labeling - faw

Skip to content

# Labeling

Labeling Widget for Marimo

## `LabelingWidget`

Bases: `AnyWidget`

A text labeling widget where students drag numbered labels to text lines.

Attributes:

Name | Type | Description
--- | --- | ---
`question` | `str` | The question text to display
`labels` | `list` | List of label texts (shown on left)
`text_lines` | `list` | List of text lines to be labeled (shown on right)
`correct_labels` | `dict` | Mapping of line indices to lists of correct label indices
`value` | `dict` | Current state with 'placed_labels', 'score', 'total', and 'correct' keys

Source code in `src/faw/labeling.py`

     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    class LabelingWidget(anywidget.AnyWidget):
        """
        A text labeling widget where students drag numbered labels to text lines.

        Attributes:
            question (str): The question text to display
            labels (list): List of label texts (shown on left)
            text_lines (list): List of text lines to be labeled (shown on right)
            correct_labels (dict): Mapping of line indices to lists of correct label indices
            value (dict): Current state with 'placed_labels', 'score', 'total', and 'correct' keys
        """

        # Load JavaScript from external file
        _esm = Path(__file__).parent / "static" / "labeling.js"

        # Traitlets
        question = traitlets.Unicode("").tag(sync=True)
        labels = traitlets.List(trait=traitlets.Unicode()).tag(sync=True)
        text_lines = traitlets.List(trait=traitlets.Unicode()).tag(sync=True)
        correct_labels = traitlets.Dict().tag(sync=True)
        value = traitlets.Dict(default_value=None, allow_none=True).tag(sync=True)

        def __init__(self, question: str, labels: list[str], text_lines: list[str], correct_labels: dict, **kwargs):
            """
            Initialize a labeling widget.

            Args:
                question: The question text
                labels: List of label texts (e.g., ["Variable declaration", "Function call", "Loop"])
                text_lines: List of text lines to be labeled (e.g., code lines, sentences)
                correct_labels: Dict mapping line index to list of correct label indices
                               Example: {0: [0, 1], 2: [2]} means line 0 should have labels 0 and 1,
                               line 2 should have label 2
            """
            super().__init__(**kwargs)
            self.question = question
            self.labels = labels
            self.text_lines = text_lines
            self.correct_labels = correct_labels

### `__init__(question, labels, text_lines, correct_labels, **kwargs)`

Initialize a labeling widget.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`question` | `str` | The question text | required
`labels` | `list[str]` | List of label texts (e.g., ["Variable declaration", "Function call", "Loop"]) | required
`text_lines` | `list[str]` | List of text lines to be labeled (e.g., code lines, sentences) | required
`correct_labels` | `dict` | Dict mapping line index to list of correct label indices Example: {0: [0, 1], 2: [2]} means line 0 should have labels 0 and 1, line 2 should have label 2 | required

Source code in `src/faw/labeling.py`

    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    def __init__(self, question: str, labels: list[str], text_lines: list[str], correct_labels: dict, **kwargs):
        """
        Initialize a labeling widget.

        Args:
            question: The question text
            labels: List of label texts (e.g., ["Variable declaration", "Function call", "Loop"])
            text_lines: List of text lines to be labeled (e.g., code lines, sentences)
            correct_labels: Dict mapping line index to list of correct label indices
                           Example: {0: [0, 1], 2: [2]} means line 0 should have labels 0 and 1,
                           line 2 should have label 2
        """
        super().__init__(**kwargs)
        self.question = question
        self.labels = labels
        self.text_lines = text_lines
        self.correct_labels = correct_labels

## Example