Multiple choice - faw

Skip to content

# Multiple choice

Multiple Choice Widget for Marimo

## `MultipleChoiceWidget`

Bases: `AnyWidget`

A multiple choice question widget.

Attributes:

Name | Type | Description
--- | --- | ---
`question` | `str` | The question text to display
`options` | `list` | List of answer options
`correct_answer` | `int` | Index of the correct answer (0-based)
`explanation` | `str` | Optional explanation text shown after answering
`value` | `dict` | Current state with 'selected', 'correct', and 'answered' keys

Source code in `src/faw/multiple_choice.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
    class MultipleChoiceWidget(anywidget.AnyWidget):
        """
        A multiple choice question widget.

        Attributes:
            question (str): The question text to display
            options (list): List of answer options
            correct_answer (int): Index of the correct answer (0-based)
            explanation (str): Optional explanation text shown after answering
            value (dict): Current state with 'selected', 'correct', and 'answered' keys
        """

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

        # Traitlets
        question = traitlets.Unicode("").tag(sync=True)
        options = traitlets.List(trait=traitlets.Unicode()).tag(sync=True)
        correct_answer = traitlets.Int(0).tag(sync=True)
        explanation = traitlets.Unicode("").tag(sync=True)
        value = traitlets.Dict(default_value=None, allow_none=True).tag(sync=True)

        def __init__(self, question: str, options: list[str], correct_answer: int, explanation: str = "", **kwargs):
            """
            Initialize a multiple choice widget.

            Args:
                question: The question text
                options: List of answer options
                correct_answer: Index of the correct answer (0-based)
                explanation: Optional explanation text
            """
            super().__init__(**kwargs)
            self.question = question
            self.options = options
            self.correct_answer = correct_answer
            self.explanation = explanation

### `__init__(question, options, correct_answer, explanation='', **kwargs)`

Initialize a multiple choice widget.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`question` | `str` | The question text | required
`options` | `list[str]` | List of answer options | required
`correct_answer` | `int` | Index of the correct answer (0-based) | required
`explanation` | `str` | Optional explanation text | `''`

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

    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    def __init__(self, question: str, options: list[str], correct_answer: int, explanation: str = "", **kwargs):
        """
        Initialize a multiple choice widget.

        Args:
            question: The question text
            options: List of answer options
            correct_answer: Index of the correct answer (0-based)
            explanation: Optional explanation text
        """
        super().__init__(**kwargs)
        self.question = question
        self.options = options
        self.correct_answer = correct_answer
        self.explanation = explanation

## Example