Form

class marimo.ui.form(element: UIElement[S, T], *, bordered: bool = True, loading: bool = False, submit_button_label: str = 'Submit', submit_button_tooltip: str | None = None, submit_button_disabled: bool = False, clear_on_submit: bool = False, show_clear_button: bool = False, clear_button_label: str = 'Clear', clear_button_tooltip: str | None = None, validate: Callable[[Mapping[str, Mapping[str, JSONType] | Sequence[JSONType] | str | int | float | bool | MIME | None] | Sequence[Mapping[str, JSONType] | Sequence[JSONType] | str | int | float | bool | MIME | None] | str | int | float | bool | MIME | None], str | None] | None = None, label: str = '', on_change: Callable[[T | None], None] | None = None)

A submittable form linked to a UIElement.

Use a form to prevent sending UI element values to Python until a button is clicked.

The value of a form is the value of the underlying element the last time the form was submitted.

Example.

# Create a form with chaining
form = mo.ui.slider(1, 100).form()
# Create a form with multiple elements
form = (
    mo.md('''
    **Your form.**

    {name}

    {date}
''')
    .batch(
        name=mo.ui.text(label="name"),
        date=mo.ui.date(label="date"),
    )
    .form(show_clear_button=True, bordered=False)
)
# Instantiate a form directly
form = mo.ui.form(element=mo.ui.slider(1, 100))

Attributes.

  • value: the value of the wrapped element when the form’s submit button was last clicked

  • element: a copy of the wrapped element

Initialization Args.

  • element: the element to wrap

  • bordered: whether the form should have a border

  • loading: whether the form should be in a loading state

  • submit_button_label: the label of the submit button

  • submit_button_tooltip: the tooltip of the submit button

  • submit_button_disabled: whether the submit button should be disabled

  • clear_on_submit: whether the form should clear its contents after submitting

  • show_clear_button: whether the form should show a clear button

  • clear_button_label: the label of the clear button

  • clear_button_tooltip: the tooltip of the clear button

  • validate: a function that takes the form’s value and returns an error message if the value is invalid, or None if the value is valid

  • label: text label for the form

  • on_change: optional callback to run when this element’s value changes

Public methods

Inherited from UIElement

form([label, bordered, loading, ...])

Create a submittable form out of this UIElement.

send_message(message, buffers)

Send a message to the element rendered on the frontend from the backend.

Inherited from Html

batch(**elements)

Convert an HTML object with templated text into a UI element.

center()

Center an item.

right()

Right-justify.

left()

Left-justify.

callout([kind])

Create a callout containing this HTML element.

style([style])

Wrap an object in a styled container.

Public Data Attributes:

Inherited from UIElement

value

The element’s current value.

Inherited from Html

text

A string of HTML representing this element.