Ordering - faw

Skip to content

# Ordering

Ordering Widget for Marimo

## `OrderingWidget`

Bases: `AnyWidget`

An ordering question widget where students arrange items in sequence using drag-and-drop.

Attributes:

Name | Type | Description
--- | --- | ---
`question` | `str` | The question text to display
`items` | `list` | Items in the correct order
`shuffle` | `bool` | Whether to shuffle items initially
`value` | `dict` | Current state with 'order' and 'correct' keys

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

     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
    47
    48
    49
    50
    class OrderingWidget(anywidget.AnyWidget):
        """
        An ordering question widget where students arrange items in sequence using drag-and-drop.

        Attributes:
            question (str): The question text to display
            items (list): Items in the correct order
            shuffle (bool): Whether to shuffle items initially
            value (dict): Current state with 'order' and 'correct' keys
        """

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

        # Traitlets
        question = traitlets.Unicode("").tag(sync=True)
        items = traitlets.List(trait=traitlets.Unicode()).tag(sync=True)
        current_order = traitlets.List(trait=traitlets.Unicode()).tag(sync=True)
        shuffle = traitlets.Bool(True).tag(sync=True)
        value = traitlets.Dict(default_value=None, allow_none=True).tag(sync=True)

        def __init__(self, question: str, items: list[str], shuffle: bool = True, **kwargs):
            """
            Initialize an ordering widget.

            Args:
                question: The question text
                items: Items in the correct order
                shuffle: Whether to shuffle items initially (default: True)
            """
            super().__init__(**kwargs)
            self.question = question
            self.items = items
            self.shuffle = shuffle

            # Create shuffled initial order if requested
            if shuffle:
                current = items.copy()
                random.shuffle(current)
                self.current_order = current
            else:
                self.current_order = items.copy()

### `__init__(question, items, shuffle=True, **kwargs)`

Initialize an ordering widget.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`question` | `str` | The question text | required
`items` | `list[str]` | Items in the correct order | required
`shuffle` | `bool` | Whether to shuffle items initially (default: True) | `True`

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

    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    def __init__(self, question: str, items: list[str], shuffle: bool = True, **kwargs):
        """
        Initialize an ordering widget.

        Args:
            question: The question text
            items: Items in the correct order
            shuffle: Whether to shuffle items initially (default: True)
        """
        super().__init__(**kwargs)
        self.question = question
        self.items = items
        self.shuffle = shuffle

        # Create shuffled initial order if requested
        if shuffle:
            current = items.copy()
            random.shuffle(current)
            self.current_order = current
        else:
            self.current_order = items.copy()

## Example