Skip to main content

Creating Custom UI Components

marimo provides a rich library of built-in UI components, but you can also create custom components using anywidget - a framework for building custom Jupyter widgets with modern web technologies.

Why Custom Components?

Create custom components when you need:
  • Specialized visualizations not covered by built-in components
  • Integration with third-party JavaScript libraries
  • Custom interactive controls for domain-specific workflows
  • Shareable, reusable components across projects

Using anywidget with marimo

marimo provides seamless integration with anywidget through mo.ui.anywidget():

Quick Start: Basic Widget

Create a simple color picker widget:

Component Architecture

Anatomy of an anywidget

From marimo/_plugins/ui/_impl/from_anywidget.py:
Key components:
  1. Python traits: Define the widget’s state (synchronized between Python and JavaScript)
  2. JavaScript module: Renders the UI and handles interactions
  3. marimo wrapper: Integrates with marimo’s reactive execution

State Synchronization

marimo automatically synchronizes widget state:

Building a Complete Widget

Example: Interactive Drawing Widget

From examples/third_party/anywidget/tldraw_colorpicker.py:

Widget with Multiple Traits

Advanced Features

Using External Libraries

Integrate JavaScript libraries like D3, Plotly, or custom frameworks:

CSS Styling

Add custom styles to your widget:

Binary Data Transfer

Transfer binary data efficiently (images, arrays, etc.):

Widget Lifecycle

From marimo/_plugins/ui/_impl/anywidget/init.py:
marimo automatically:
  • Initializes widgets when cells run
  • Cleans up widgets when cells are re-executed or deleted
  • Manages communication channels between Python and JavaScript

Best Practices

Only sync essential state between Python and JavaScript:
Ensure your widget handles initial state correctly:
Document widget traits with proper types:
Write tests for widget behavior:

Publishing Widgets

Package Structure

pyproject.toml

Publishing to PyPI

Usage After Publishing

Data Visualization Widget

Form Builder Widget

Debugging Widgets

Console Logging

Error Handling