Skip to content

ScientificNumber widget: enter and display numbers in scientific notation, scale them automatically, show units #327

Description

@m4xii-ii

Issue spun out of closed PR #326.

Problem the widget would solve: you want to enter a number on the order of 1e-20, but don't want to type a run of leading zeros in decimal notation, so with mo.ui.number you make the user enter a scaled value and then manually rescale it back every time you read the number.

The solution: TangleSlider solves part of this problem by accepting scientific notation on input, but you still have to apply the scale by hand everywhere. This widget lets you enter the number in scientific notation and define a scale that is applied automatically when you read the value back from Python. It also lets you display the number in decimal or scientific notation, show a scale label and a unit label, and define a step size and bounds.

The code would look like this:

from wigglystuff import ScientificNumber
distance = mo.ui.anywidget(
    ScientificNumber(
        label="$\\text{Distance}$",
        unit_label="$\\text{km}$",
        scale_label="$\\times 10^{8}$",
        scale=1e11, # Decompose applied scale into unit and scale factor
        value=1.496e11,
        notation="scientific", # Also display number in scientific notation
    )
)

See demo.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions