Components

A component is a Python class with state, actions, and a template. It renders on the server and stays reactive: call an action, the server re-renders, and Interlace morphs only what changed. No JavaScript, no separate API.

This is an overview of what a component is made of, with a link to the full page for each piece.

Here is a whole component, all in one class. Click the buttons:

PREVIEW
0
class BasicCounter(Component):
    count: int = 0

    def increment(self):
        self.count += 1

    def decrement(self):
        self.count -= 1

    def inline_template(self):
        return """
        <div class="flex items-center gap-5">
            <button lace:click="decrement">-</button>
            <span>{{ count }}</span>
            <button lace:click="increment">+</button>
        </div>
        """

Three parts, and that is the whole idea:

  • count is reactive state, a type-annotated attribute that syncs between server and browser.
  • increment / decrement are actions the template calls with lace:click.
  • inline_template returns the HTML, with {{ count }} and the lace:* directives wiring it to your state and actions.

Anatomy

Every component is the same handful of parts. Read this top to bottom, the comments name each kind of building block:

from interlace import Component, computed
from django import forms

class Editor(Component):
    # State: type-annotated attributes, synced to the client
    title: str = ""
    saved: bool = False

    # Private: the leading underscore keeps it server-only (never sent to the browser)
    _revision: int = 0

    # Computed: derived value, cached per request, never stored or sent as state
    @computed
    def word_count(self):
        return len(self.title.split())

    # Lifecycle: mount() runs once before the first render
    def mount(self):
        self.title = "Untitled"

    # Validation: bind Django form fields to properties
    def rules(self):
        return {"title": forms.CharField(min_length=3)}

    # Action: called from the template, mutates state, re-renders
    def save(self):
        if self.validate():
            self.saved = True

    # Hook: runs after any bound property changes
    def updated(self, prop, value):
        self.saved = False

    # Template: lace:* directives wire the HTML to your props and actions
    def inline_template(self):
        return """
        <input lace:model.live="title">
        <p>{{ word_count }} words</p>
        <button lace:click="save">Save</button>
        """

The pieces, in full

Each part above has a dedicated page. This is the map.

State

  • Properties, type-annotated attributes become reactive state; supported types, mutable defaults, Locked.
  • Computed properties, @computed derived values, cached per request (or across requests, via the cache).
  • State storage, how component state travels in the page, and keeping secrets out of it.

Behaviour

  • Actions, public methods called from the template with lace:click (or any DOM event), with typed arguments.
  • Lifecycle hooks, mount, updated_<property>, and updated, the full hook set.
  • Events, components talk to each other with emit / emit_to and listener methods.

Templates and binding

  • Templates, inline vs a file, the naming convention, and wrapper-free (multi-root) components.
  • Two-way binding, lace:model (.live / .blur / .change / .debounce) syncs inputs to properties.
  • The {% interlace %} tag, rendering a component in a page, passing props, and lazy.

Data and forms

  • Working with models, bind Django model instances and QuerySets as component state.
  • Forms, carry a Django Form / ModelForm as state and bind straight to its fields.
  • Validation, rules() / validation_rules, validate(), and per-field errors.

Composition and UX

  • Nested components, components render other components; each keeps isolated state.
  • Loading states, lace:loading / lace:target while a request is in flight.
  • Polling, lace:poll for live updates, batched and time-synced across the page.
  • Lazy loading, defer a heavy component until it scrolls into view.
  • Query params, sync properties to the URL for shareable, bookmarkable state.

Isolation. Each component instance has its own state. Nested components don't share state with their parent, they communicate through events.

View this page as markdown - handy as context for your AI tools. Full index at /llms.txt.

Theme
Accent
Ink