Skip to content
Feeding a clanker? Grab this page as raw .md

Matrix API#

Bases: AnyWidget

Spreadsheet-like numeric editor with bounds, naming, and symmetry helpers.

Note

This widget has graduated to marimo core. If you are using marimo, prefer marimo.ui.matrix. Matrix will continue to work in plain Jupyter and other anywidget hosts.

Examples:

from wigglystuff import Matrix

matrix = Matrix(rows=3, cols=3, min_value=0, max_value=10)
matrix

Create a Matrix editor.

Parameters:

Name Type Description Default
matrix Optional[List[List[float]]]

Optional 2D list of initial values.

None
rows int

Number of rows when matrix is omitted.

3
cols int

Number of columns when matrix is omitted.

3
min_value float

Lower bound for cell values.

-100
max_value float

Upper bound for cell values.

100
triangular bool

If True, enforce triangular editing constraints.

False
row_names Optional[List[str]]

Custom labels for rows.

None
col_names Optional[List[str]]

Custom labels for columns.

None
static bool

Disable editing when True.

False
flexible_cols bool

Allow column count changes interactively.

False
step float

Increment step size for cell value adjustments.

1.0
digits int

Number of decimal digits to display.

1
mirror bool

If True, mirror edits symmetrically across the diagonal.

False
**kwargs Any

Forwarded to anywidget.AnyWidget.

{}
Source code in wigglystuff/matrix.py
def __init__(
    self,
    matrix: Optional[List[List[float]]] = None,
    rows: int = 3,
    cols: int = 3,
    min_value: float = -100,
    max_value: float = 100,
    step: float = 1.0,
    digits: int = 1,
    mirror: bool = False,
    triangular: bool = False,
    row_names: Optional[List[str]] = None,
    col_names: Optional[List[str]] = None,
    static: bool = False,
    flexible_cols: bool = False,
    **kwargs: Any,
) -> None:
    """Create a Matrix editor.

    Args:
        matrix: Optional 2D list of initial values.
        rows: Number of rows when ``matrix`` is omitted.
        cols: Number of columns when ``matrix`` is omitted.
        min_value: Lower bound for cell values.
        max_value: Upper bound for cell values.
        triangular: If ``True``, enforce triangular editing constraints.
        row_names: Custom labels for rows.
        col_names: Custom labels for columns.
        static: Disable editing when ``True``.
        flexible_cols: Allow column count changes interactively.
        step: Increment step size for cell value adjustments.
        digits: Number of decimal digits to display.
        mirror: If ``True``, mirror edits symmetrically across the diagonal.
        **kwargs: Forwarded to ``anywidget.AnyWidget``.
    """
    warn_if_in_marimo(
        "Matrix",
        'Use <a href="https://docs.marimo.io/api/inputs/matrix/" '
        'style="font-weight: 600; text-decoration: underline;">'
        "<code>marimo.ui.matrix</code></a> instead.",
    )
    if matrix is not None:
        matrix_array = np.array(matrix)
        if matrix_array.min() < min_value:
            raise ValueError(
                f"The min value of input matrix is less than min_value={min_value}."
            )
        if matrix_array.max() > max_value:
            raise ValueError(
                f"The max value of input matrix is less than max_value={max_value}."
            )
        rows, cols = matrix_array.shape
        matrix = matrix_array.tolist()
    else:
        matrix = [
            [(min_value + max_value) / 2 for _ in range(cols)]
            for _ in range(rows)
        ]

    if row_names is not None and len(row_names) != rows:
        raise ValueError(
            f"Length of row_names ({len(row_names)}) must match number of rows ({rows})."
        )
    if col_names is not None and len(col_names) != cols:
        raise ValueError(
            f"Length of col_names ({len(col_names)}) must match number of columns ({cols})."
        )

    if row_names is None:
        row_names = []
    if col_names is None:
        col_names = []

    super().__init__(
        matrix=matrix,
        rows=rows,
        cols=cols,
        min_value=min_value,
        max_value=max_value,
        step=step,
        digits=digits,
        mirror=mirror,
        triangular=triangular,
        row_names=row_names,
        col_names=col_names,
        static=static,
        flexible_cols=flexible_cols,
        **kwargs,
    )

Synced traitlets#

Traitlet Type Notes
matrix list[list[float]] Cell values.
rows int Row count.
cols int Column count.
min_value float Minimum allowed value.
max_value float Maximum allowed value.
mirror bool Mirror edits across the diagonal when enabled.
step float Step size for edits.
digits int Decimal precision for display.
row_names list[str] Optional row labels.
col_names list[str] Optional column labels.
static bool Disable editing when true.
flexible_cols bool Allow column count changes interactively.