File

class marimo.ui.file(filetypes: Sequence[str] | None = None, multiple: bool = False, kind: Literal['button', 'area'] = 'button', *, label: str = '', on_change: Callable[[Sequence[FileUploadResults]], None] | None = None)

A button or drag-and-drop area to upload a file.

Once a file is uploaded, the UI element’s value is a list of namedtuples (name, contents), where name is the filename and contents is the contents of the file. Alternatively, use the methods name(index: int = 0) and contents(index: int = 0) to retrieve the name or contents of the file at a specified index.

Use the kind argument to switch between a button and a drop-and-drop area.

The maximum file size is 100MB.

Examples.

Uploading a single file:

f = mo.ui.file()

# access the uploaded file's name
f.value[0].name
# or
f.name()

# access the uploaded file's contents
f.value[0].contents
# or
f.contents()

Uploading multiple files, accepting only .png and .jpg extensions:

f = mo.ui.file(filetypes=[".png", ".jpg"], multiple=True)

# access an uploaded file's name
f.value[index].name
# or
f.name(index)

# access the uploaded file's contents
f.value[index].contents
# or
f.contents(index)

Attributes.

  • value: a sequence of FileUploadResults, which have string name and bytes contents fields

Methods.

  • name(self, index: int = 0) -> Optional[str]: Get the name of the uploaded file at index.

  • contents(self, index: int = 0) -> Optional[bytes]: Get the contents of the uploaded file at index.

Initialization Args.

  • filetypes: the file types accepted; for example, filetypes=[".png", ".jpg"]. If None, all files are accepted. In addition to extensions, you may provide "audio/*", "video/*", or "image/*" to accept any audio, video, or image file.

  • multiple: if True, allow the user to upload multiple files

  • kind: "button" or "area"

  • label: text label for the element

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

Public methods

name([index])

Get file name at index.

contents([index])

Get file contents at index.

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.


name(index: int = 0) str | None

Get file name at index.

contents(index: int = 0) bytes | None

Get file contents at index.