Skip to content

Mental model

Wybthon is SolidJS for Python: the same fine-grained reactive model, a Pythonic API, and a renderer built for Pyodide. Read this page once and the rest of the docs will click into place.

The five big ideas

  1. Signals are the source of truth. A signal is a (getter, setter) pair. Reading it inside a tracking scope subscribes that scope; writing it stages a change that becomes visible at the next flush. Every write batches; there's no batch().
  2. Derivations are lazy and glitch-free. A memo recomputes only when it's read after a source changed, and it notifies its observers only when its value actually changed. An async def memo is the data-fetching primitive.
  3. Components run once. The body executes a single time when the component mounts and returns its markup. Its parameters are its props: the compiler turns every read of title in the body into a tracked read of the parent's current value, as reading Solid's props object does. Anything reactive lives in the markup, a memo, or an effect, never in the body itself.
  4. The compiler makes markup reactive; holes update. Wybthon's compiler rewrites each html(t"...") call before its module runs, as Solid's compiler rewrites JSX. Every dynamic expression ({count() * 2}, or a parameter such as {title}) becomes a getter, and each literal becomes a native <template> plus a list of slots, so every instance is a clone with its slots filled. Each getter becomes a hole or a binding that updates only its text, attribute, or subtree. A signal change re-runs the holes that read it, never whole components.
  5. Ownership, not lifecycle methods. Effects, memos, cleanups, and context attach to the owner that was active when they were created. Disposing the owner tears everything down depth-first.

The data flow

flowchart LR
    A[Signal write] -->|staged| B[Flush]
    B -->|render phase| C[Holes and bindings]
    C -->|batched ops| D[JS kernel commits DOM]
    D -->|effect phase| E[create_effect]
  • A create_signal write is staged. The graph flushes on the next microtask, at the end of every event handler, or when you call flush yourself (in tests).
  • In the render phase, every dirty hole and reactive binding re-runs and emits DOM operations into a buffer. Nothing static is revisited: a template's static markup was cloned once, at mount.
  • The buffer is handed to the JavaScript kernel in one bridge crossing.
  • In the effect phase, create_effect computations run and observe the committed DOM.

What this looks like in practice

from wybthon import Show, component, create_signal, html


@component
def Counter(step: int = 1):
    count, set_count = create_signal(0)

    def increment():
        set_count(lambda n: n + step)

    return html(t"""
      <div>
        <p>Count: {count()} (doubled: {count() * 2})</p>
        <button onclick={increment}>+</button>
        {Show(count() > 5, html(t"<p>That's a lot of clicks.</p>"))}
      </div>
    """)

Template strings (t"...") are PEP 750 syntax, so Wybthon requires Python 3.14.

What happens here:

  • Counter runs once. The compiler prepared the template when the module was compiled; every mount clones the compiled <template> with one kernel command.
  • {count()} and {count() * 2} are dynamic expressions, so each slot becomes its own reactive binding. Only those two text nodes patch when the count changes.
  • {Show(...)} is a call in a child position. Show is a built-in, so it's built once with a getter for its condition, count() > 5, and it swaps its branch only when the condition's truthiness flips. Its content is markup, so the compiler makes it lazy: the <p> is built only when the count passes 5.
  • step is a parameter with a default. The parent may pass step=5, or an expression such as <{Counter} step={size()} />, which arrives as a getter; step in the handler reads the current value either way.
  • onclick={increment} binds a delegated handler. It takes no arguments because it doesn't need the event. It stages a functional update, the handler returns, the graph flushes, and the DOM op commits before the browser paints.

How props become reactive

from wybthon import component, html


@component
def Greeting(name: str):
    return html(t"<p>Hello, {name}!</p>")


html(t"<{Greeting} name={user().name} />")
  • The parent's name={user().name} is dynamic, so the compiler passes it as a getter, the way Solid passes a JSX prop.
  • name is a parameter, so the compiler rewrites each read of it into a read of the props object, which returns the value, tracked. A bare parameter name in the template is dynamic (unlike other bare names), so {name} becomes a binding, and when user changes only that text node updates.
  • Reading name at the top level of the body (greeting = f"Hello, {name}") would freeze the value at mount, as destructuring props does in Solid, and dev mode warns about it when the parent passed something reactive. Read parameters in the markup, handlers, memos, and effects, or use untrack(lambda: name) when a one-time read is what you want.
  • Parameters are read-only: assigning to one is a SyntaxError when the module is compiled, so a value you want to change belongs in a new name or a signal.
  • A parameter annotated as callable (on_close: Callable[[], None] | None) returns exactly what the parent passed, so callbacks are never called by the read.
  • A prop costs only what it needs. When the parent passes a constant (Greeting(name="Ada")) to a component that can never receive new props, the read subscribes to nothing.

See Components for the full prop story.

Why a virtual DOM at all?

SolidJS compiles JSX ahead of time into a static template plus the expressions that change. Wybthon's compiler does the same with t-strings: each literal becomes a native <template>, a mount function written into the module, and a plan of its slots, and each dynamic expression becomes a getter. Mounting an instance is one clone command, with no VNode tree for its static parts.

In Pyodide every DOM call crosses the Python-to-JavaScript bridge, which dominates rendering cost. So Wybthon keeps a small VDOM as an implementation detail: holes and bindings emit compact operations into a buffer that the JavaScript kernel applies in one crossing per flush. When a hole re-renders, the reconciler diffs only that hole's subtree, and a template from the same literal is patched slot by slot. h(tag, props, *children) is the programmatic path for markup built by code; it builds VNodes that mount node by node, in the same batch. The reactive model is Solid's; the VDOM is the batching layer that makes it fast under Pyodide. See Templates and Virtual DOM.

Where it differs from React

  • No per-component re-render. The tree is built once; updates target individual holes.
  • No hooks rules. State and effects are created with ordinary Python calls and live for the lifetime of the owning scope.
  • Props never need memoizing. A new object identity doesn't re-render anything; only reads inside holes react.
  • Data fetching is a memo, not a hook. Read an async memo like any other accessor; the nearest Loading boundary handles the not-ready state.

If you're coming from React, read Migrating from React next.

Where it matches Solid

Wybthon follows SolidJS 2.0 closely: signals, memos, and effects with the same semantics; staged writes with no batch(); async as part of the graph; Loading, Reveal, and Errored boundaries; draft-first stores; actions and optimistic state. Wybthon tracks Solid 2.0 RC.21, which isn't a stable release yet. The differences are the language, the markup (t-string templates in place of JSX, compiled on Python's syntax tree), typed function parameters in place of TypeScript prop interfaces, and a handful of names. See Migrating from Solid.

Next steps