Routes¶
marimo.routes
¶
routes(
routes: dict[
str,
Union[
Callable[[], object],
Callable[[], Coroutine[None, None, object]],
object,
],
]
)
Bases: UIElement[str, str]
Renders a list of routes that are switched based on the URL path.
Routes currently don't support nested routes, or dynamic routes (e.g. #/user/:id
).
If you'd like to see these features, please let us know on GitHub:
https://github.com/marimo-team/marimo/issues
For a simple-page-application (SPA) experience, you should use hash-based routing.
For example, prefix your routes with #/
.
If you are using a multi-page-application (MPA) with marimo.create_asgi_app
,
you should use path-based routing. For example, prefix your routes with /
.
Examples:
mo.routes(
{
"#/": render_home,
"#/about": render_about,
"#/contact": render_contact,
mo.routes.CATCH_ALL: render_home,
}
)
PARAMETER | DESCRIPTION |
---|---|
routes
|
A dictionary of routes, where the key is the URL path and the value is a function that returns the content to display.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Html
|
An Html object.
TYPE:
|
form
¶
form(
label: str = "",
*,
bordered: bool = True,
loading: bool = False,
submit_button_label: str = "Submit",
submit_button_tooltip: Optional[str] = None,
submit_button_disabled: bool = False,
clear_on_submit: bool = False,
show_clear_button: bool = False,
clear_button_label: str = "Clear",
clear_button_tooltip: Optional[str] = None,
validate: Optional[
Callable[[Optional[JSONType]], Optional[str]]
] = None,
on_change: Optional[
Callable[[Optional[T]], None]
] = None
) -> form[S, T]
Create a submittable form out of this UIElement
.
Creates a form that gates submission of a UIElement
's value until a submit button is clicked.
The form's value is the value of the underlying element from the last submission.
Examples:
Convert any UIElement
into a form:
Combine with HTML.batch
to create a form made out of multiple UIElements
:
form = (
mo.ui.md(
'''
**Enter your prompt.**
{prompt}
**Choose a random seed.**
{seed}
'''
)
.batch(
prompt=mo.ui.text_area(),
seed=mo.ui.number(),
)
.form()
)
PARAMETER | DESCRIPTION |
---|---|
label
|
A text label for the form.
TYPE:
|
bordered
|
Whether the form should have a border.
TYPE:
|
loading
|
Whether the form should be in a loading state.
TYPE:
|
submit_button_label
|
The label of the submit button.
TYPE:
|
submit_button_tooltip
|
The tooltip of the submit button.
TYPE:
|
submit_button_disabled
|
Whether the submit button should be disabled.
TYPE:
|
clear_on_submit
|
Whether the form should clear its contents after submitting.
TYPE:
|
show_clear_button
|
Whether the form should show a clear button.
TYPE:
|
clear_button_label
|
The label of the clear button.
TYPE:
|
clear_button_tooltip
|
The tooltip of the clear button.
TYPE:
|
validate
|
A function that takes the form's value and returns an error message if invalid,
or
TYPE:
|
on_change
|
A callback that takes the form's value and returns an error message if invalid,
or
TYPE:
|
send_message
¶
Send a message to the element rendered on the frontend from the backend.
python import marimo
app = marimo.App()
@app.cell def __(): import marimo as mo return
@app.cell def __(): mo.sidebar( [ mo.md("# marimo"), mo.nav_menu( { "#/": f"{mo.icon('lucide:home')} Home", "#/about": f"{mo.icon('lucide:user')} About", "#/contact": f"{mo.icon('lucide:phone')} Contact", "Links": { "https://twitter.com/marimo_io": "Twitter", "https://github.com/marimo-team/marimo": "GitHub", }, }, orientation="vertical", ), ] ) return
@app.cell def __(): mo.routes({ "#/": mo.md("# Home"), "#/about": mo.md("# About"), "#/contact": mo.md("# Contact"), mo.routes.CATCH_ALL: mo.md("# Home"), }) return