Reactive UI#

An ngapp user interface is built once and then follows the data it shows. Values that change are bound to the components that display them, and the structure of the page changes only through three constructs that keep every component they can keep. A parameter change then updates exactly the text and props that depend on it: inputs keep their focus and cursor, plots and viewers are not recreated, and the page does not jump.

The authoring rule#

Important

Build components once. Show changing values by binding observables or computed() values. Change structure only with Show, For or Dynamic. Put imperative updates (figures, viewers) in effect(). Expensive content is built in on_mounted or effect, never in a constructor.

Two further rules complete it:

  • Constructors are cheap. The app constructor builds the whole component tree, including the content of Show, For and Dynamic, in every environment, also in compute jobs (see Where code runs).

  • Values in the case state are replaced, not mutated in place. After an in-place change, call State.notify(key).

The same rule ships with the package as ngapp/AGENTS.md, a compact reference for coding agents.

A complete example#

The following app has three inputs bound to the case state, two labels with derived values, a list of warnings, an optional input, a region whose layout depends on a selection, and a plot.

import math

from ngapp.app import App
from ngapp.components import (
    Centered, Dynamic, For, Label, NumberInput, PlotlyComponent,
    QBtnToggle, QInput, Row, Show,
)
from ngapp.observable import computed


def check(amplitude, frequency, points):
    warnings = []
    if amplitude > 10:
        warnings.append("Amplitude above 10")
    if frequency > 0 and points / frequency < 8:
        warnings.append("Fewer than 8 points per period")
    return warnings


class SineApp(App):
    def __init__(self):
        super().__init__()
        s = self.state
        self.amplitude = s.ref("amplitude", 1.0, float)
        self.frequency = s.ref("frequency", 2.0, float)
        self.points = s.ref("points", 200, int)
        self.advanced = s.ref("advanced", False)
        self.view = s.ref("view", "summary")

        warnings = computed(
            lambda: check(
                self.amplitude.value,
                self.frequency.value,
                self.points.value,
            )
        )

        self.plot = PlotlyComponent()
        self.plot.effect(self.draw)

        self.component = Centered(
            Row(
                NumberInput(ui_label="Amplitude", ui_model_value=self.amplitude),
                NumberInput(ui_label="Frequency", ui_model_value=self.frequency),
                NumberInput(ui_label="Points", ui_model_value=self.points),
            ),
            QBtnToggle(
                ui_model_value=self.advanced,
                ui_options=[
                    {"label": "Basic", "value": False},
                    {"label": "Advanced", "value": True},
                ],
            ),
            Show(self.advanced, lambda: QInput(ui_label="Note")),
            Label(computed(lambda: f"Peak to peak: {2 * self.amplitude.value:g}")),
            For(warnings, Label, otherwise=lambda: "No warnings"),
            QBtnToggle(
                ui_model_value=self.view,
                ui_options=[
                    {"label": "Summary", "value": "summary"},
                    {"label": "Samples", "value": "samples"},
                ],
            ),
            Dynamic(self.details),
            self.plot,
        )

    def details(self):
        if self.view.value == "summary":
            return Label(computed(lambda: f"{self.points.value} points"))
        return [self.sample(x) for x in (0.0, 0.125, 0.25)]

    def sample(self, x):
        return Label(
            computed(
                lambda: f"y({x:g}) = "
                f"{self.amplitude.value * math.sin(2 * math.pi * self.frequency.value * x):+.3f}"
            )
        )

    def draw(self):
        import numpy as np
        import plotly.graph_objects as go

        x = np.linspace(0, 1, self.points.value)
        y = self.amplitude.value * np.sin(2 * np.pi * self.frequency.value * x)
        self.plot.draw(go.Figure(go.Scatter(x=x, y=y, mode="lines")))

There is no refresh function and no event handler. Typing an amplitude writes the state key amplitude; the peak-to-peak label, the warning list, the sample labels (when shown) and the plot follow. The other components are not touched. The ngapp repository contains this app in a slightly larger form as tests/local_app_reactive.

Where code runs#

ngapp builds the app in the browser session and also in every compute job, where compute functions are addressed by their path in the component tree. Code is therefore divided into two groups:

Runs in every environment

Constructors of the app and of all components, and the content of Show, For and Dynamic, which is built and kept up to date whether a browser shows it or not. The component tree in a job is the same as in the browser, so a compute function inside a For row is found.

Runs only where a browser shows the component

on_mounted handlers, component.effect functions and the evaluation of props and text children bound to a computed. They run when the component is mounted (or serialised for the browser), never in a compute job.

Constructors must be cheap. A meshing step, a figure, a solve, a file or network access in a constructor runs in every job. ngapp logs a warning on logger ngapp.components when the constructor of a component takes longer than NGAPP_SLOW_INIT seconds of its own time (default 0.1, without the constructors of nested components) or the app constructor takes longer than NGAPP_SLOW_APP_INIT seconds in total (default 1.0):

Slow constructor: MeshView took 840 ms (own time, limit 100 ms), created at
app.py:42. Constructors also run in compute jobs: build expensive content
in on_mounted or in an effect.

Both variables are read when ngapp is imported; 0 disables the corresponding warning.

Derived values: computed#

computed() creates a read-only observable whose value is computed by a function:

from ngapp.observable import Observable, computed

radius = Observable(1.0)
height = Observable(2.0)
volume = computed(lambda: math.pi * radius.value**2 * height.value)
label = computed(lambda: f"Volume: {volume.value:.3g} m³")
  • Every observable, computed value and state key read inside the function is a dependency. Dependencies are recorded on each evaluation, so a branch that is not taken creates none.

  • Evaluation is lazy: on the first read and on the next read after a dependency changed. A computed that nothing reads is never evaluated.

  • Dependents are notified only when the result changed. Results are compared by identity, then with ==; a comparison that raises or is ambiguous (numpy arrays) counts as a change. Pass equal= for another comparison.

  • An exception in the function propagates to the reader, and the value is computed again on the next read.

  • Assigning .value raises AttributeError.

  • on_change(cb) calls cb(new, old) after the result changed.

Inside one event each computed is evaluated at most once, however many of its dependencies changed.

Binding props and text#

Any ui_* prop accepts an observable or a computed value:

QBtn(ui_label=computed(lambda: f"Run {n.value} cases"),
     ui_disable=computed(lambda: n.value == 0))
Div(ui_class=computed(lambda: "text-negative" if error.value else ""))
  • A plain Observable on ui_model_value is bound in both directions. When the value the user typed converts to the value already stored, nothing is sent back, so typing 3. into an input bound to a float stays 3..

  • A prop bound to a computed is evaluated when the component is shown in a browser and updated in place afterwards. It is never stored in the case.

  • Assigning a plain value to a prop bound to a computed removes the binding and logs a warning. A prop bound to a plain observable keeps its binding: a plain value assigned to it is shown until the observable changes again. Write the observable instead.

  • ui_style, ui_class, ui_hidden and ui_invisible accept observables as well.

Observables in a child list become text nodes that are updated in place:

Label(computed(lambda: f"Volume: {volume.value:.3g} m³"))
Div("Height: ", computed(lambda: f"{height.value:g} m"))

Label.text accepts a plain string or an observable.

Case state#

self.state of the app or of a component holds the data of the case. Reading a key inside a computed, a structure or an effect makes the key a dependency. A change of the key from any source notifies its dependents: a local assignment, a loaded case, a pulled revision of another tab, a merged job result, a restored revision.

s = self.state
self.lsc = computed(lambda: inductance(s["winding"], s.get("gap", 0.1)))
Label(computed(lambda: f"L = {self.lsc.value * 1e3:.4g} mH"))

State.ref(key, default=None, converter=None) returns an observable view of one key (the same object for each call with the same key). Its value is the value of the key, or default while the key is not set. Assigning it sets the key to converter(value); a converter that raises ValueError or TypeError ignores the assignment. As ui_model_value it binds an input to the key:

QInput(ui_label="Frequency", ui_suffix="Hz",
       ui_model_value=self.state.ref("frequency", 50.0, float))

Opening a case whose inputs are bound with state.ref does not create an edit.

The state detects changes by assignment. A value mutated in place is not seen:

s["layers"] = [*s["layers"], new_layer]   # notifies

s["layers"].append(new_layer)             # not seen ...
s.notify("layers")                        # ... until notified

notify() updates the dependents of the key and stores the value with the next autosave. Append-only lists created with state.list(key) notify on append.

Typed state#

A StateModel declares the keys of a state with their types and defaults. With App[TankState] (or Component[TankState]) self.state is a TankState: fields are read and set as attributes, with type checking and completion in the editor, and every value is validated by pydantic when it is set and when it is read from a case (a stored 50 is read as 50.0 for a float field, an invalid stored value as the default). A key that is not set reads as its default.

from typing import Annotated
from annotated_types import Gt
from pydantic import Field
from ngapp.state_model import StateModel, ref

class TankState(StateModel):
    radius: Annotated[float, Gt(0)] = 1.0
    layers: int = Field(3, ge=1, title="Layers")
    names: list[str] = []

class Tank(App[TankState]):
    def __init__(self):
        super().__init__()
        s = self.state
        area = computed(lambda: 3.14 * s.radius**2)
        radius = ref(lambda: s.radius)
        self.component = Div(
            QInput(ui_model_value=radius, ui_error=computed(lambda: bool(radius.error.value)),
                   ui_error_message=radius.error),
            Label(computed(lambda: f"A = {area.value:.3g}")))

A field whose type is another StateModel is a nested state: its fields are keys in the namespace wall/ of the state, each followed on its own, so a computed that reads s.wall.thickness is not updated by a change of s.wall.material. ref(lambda: s.wall.thickness) binds an input to it, s.wall.update({...}) sets several fields.

class Wall(StateModel):
    thickness: float = 0.01
    material: str = "steel"

class TankState(StateModel):
    radius: float = 1.0
    wall: Wall          # no default: the defaults of Wall apply

Reads of fields are followed like reads of keys. ref() returns the state.ref of the field its function reads, with the field’s validation as converter: a value from the input that does not validate is not stored, and the ref’s error holds the reason until a value of the key is stored. state.ref(name) does the same by name, for code that loops over fields. Everything else of the state works as before (state[key], ns, notify); field names must not be names of these methods. Values are replaced, not changed in place.

Structure: Show, For, Dynamic#

The three constructs are components without an element of their own: their content appears in the parent at their position. They build their content in the constructor and keep it up to date in every environment.

build, render and otherwise functions may return a component, a string, an observable or a list of these. They run without tracking, so values they read do not rebuild the content; values that change belong in bindings inside the content.

Show#

Show(when, build, otherwise=None, keep=True) shows content while a condition holds. when is an observable or a function.

Show(lambda: s.get("mode") == "expert",
     lambda: Row(QInput(ui_label="Tolerance"), QInput(ui_label="Note")),
     otherwise=lambda: Label("Expert settings hidden"))

build runs the first time the condition is true. With keep=True the content is hidden, not removed, while the condition is false: typed text, scroll positions and expensive content survive. With keep=False the content is removed and built again next time, for large content that is rarely shown. Show replaces visibility toggles written as ui_style="display: none".

For#

For(items, render, key=None, otherwise=None, reactive=False) shows one row per item of a list. items is an observable or a function returning an iterable.

For(computed(lambda: check(s["radius"], s["height"])),
    lambda warning: Label(warning, ui_class="text-negative"),
    otherwise=lambda: "No warnings")

For(lambda: s.get("studies", []),
    lambda study: StudyCard(study),
    key=lambda study: study["name"])
  • Rows are identified by key(item), the item itself by default (it must be hashable). Rows of keys that stay keep their components and are moved when the order changes; only new keys call render.

  • Without reactive, render receives the item, and a row whose item changed (by ==) under the same key is rendered again.

  • With reactive=True, render receives an observable of the item, which is updated in place; the row is never rendered again. Bind the row’s content to it:

    For(lambda: s.get("studies", []),
        lambda study: Row(Label(computed(lambda: study.value["name"])),
                          Label(computed(lambda: study.value["status"]))),
        key=lambda study: study["name"], reactive=True)
    
  • otherwise builds the content shown for an empty list.

  • Components with an id inside a row need an id unique per row, for example a row component with namespace=True and id=<key>.

Dynamic#

Dynamic(build) builds a region with a function and builds it again when a dependency of that function changes. It covers structure that does not fit Show or For:

Dynamic(lambda: ResultTable(s) if s.get("view") == "table" else ResultPlot(s))

Unlike the other two constructs, build runs tracked. Values that only change what the components show are bound inside them or read with untracked(), so the region is not built again.

Fragment#

Fragment is the base of the three: a component whose children appear in the slot of its parent. A fragment is never mounted, so on_mounted and effect belong on its children.

Effects#

Some updates cannot be expressed as bound props: drawing a Plotly figure, updating a 3D viewer, scrolling a row into view. component.effect(fn) runs fn when the component is mounted and again after a dependency read in fn changed, while the component stays mounted:

plot = PlotlyComponent()
plot.effect(lambda: plot.draw(profile_figure(s["radius"], s["height"])))
  • Effects run after the bindings of the same event were updated, once per event.

  • An effect runs again on every mount, since a new browser element needs its content again. While the component is unmounted it does not run.

  • Effects never run in a compute job.

  • effect returns a function that removes the effect.

ngapp.observable.effect() is the same without a component: it runs at once and after each change until the returned function is called.

on_mounted keeps working and is the place for one-time setup that does not depend on observables.

Untracked reads#

untracked() reads values without creating a dependency, as call untracked(fn, *args) or as context manager. Observable.peek() reads one value untracked:

def draw():
    fig = profile(s["radius"])                    # redraw on radius changes
    fig.update_layout(title=untracked(lambda: s.get("title", "")))
    plot.draw(fig)

Events, batching and updates#

ngapp groups the work of one event. While an event handler runs (a click, an input, a state change applied from another tab or a job), changed values are collected. At the end the dependents are updated once and the browser receives the changed props and children of all components in one message; then the effects run, and what they change (props, JavaScript calls such as drawing a figure) follows. A component created and removed in the same handler is never sent. New components arrive together with their content, so a rebuilt panel renders in one step.

Plain on_change() listeners stay synchronous inside event handlers.

Outside of event handlers (timers, threads, scripts) each change is applied at once. batch() groups several changes there:

from ngapp.observable import batch

with batch():
    a.value = 1
    b.value = 2
# listeners, computed values and the browser update once here

ngapp.flush_updates() sends the collected updates immediately, for example to show progress inside a long handler or before a JavaScript call that relies on the updated page. In a local app, updates of a handler that runs longer than 50 ms are sent as they occur; in the browser (pyodide) setup a synchronous handler cannot be interrupted, so flush_updates() is needed there.

NGAPP_BATCH_UPDATES=0 sends every update at once, which helps when debugging the order of messages.

Imperative code#

Imperative updates keep working: label.text = ..., comp.ui_children = [...], on_update_model_value handlers. They can be mixed with bindings and migrated piece by piece. Replacing children recreates components, though: focus, scroll positions and the content of plots and viewers in the replaced part are lost.

Development warnings#

ngapp logs warnings on logger ngapp.components for patterns that work against the rule, once per component class and code location. Each message names the line that assigned the children or registered the handler.

<Class>.ui_children replaced <n> times within 2 s with the same component types

A child list was replaced four times within two seconds by new components of the same types. Bind the changing values, or use Show, For or Dynamic.

Handler of <Class> '<event>' created <n> components

One event handler created more than 100 components. A parameter change should update values, not rebuild the user interface.

<Class>.<prop> bound to a computed was overwritten with a plain value

The binding is removed. Usually a leftover imperative update.

NGAPP_DEV_WARNINGS=0 disables these warnings; they are never emitted in compute jobs. The slow-constructor warning (see Where code runs) is controlled by NGAPP_SLOW_INIT and NGAPP_SLOW_APP_INIT.

Testing#

ngapp.test_utils.mount() shows an app in a browser stand-in that speaks the real protocol, and ngapp.test_utils.assert_stable() fails when components were recreated:

from ngapp.test_utils import assert_stable, mount, standalone_app_test

@standalone_app_test
def test_amplitude_updates_in_place():
    app = SineApp()
    fe = mount(app)
    fe.messages.clear()
    with assert_stable(app):
        app.amplitude.value = 3.0
    assert len(fe.messages) == 1

See Testing ngapp applications for details.

API reference#

Structural components: content that follows observables, computed values and state keys without an element of its own in the browser

class ngapp.components.reactive.Dynamic(build: Callable[[], object])#

A region that is built by a function and built again when an observable, computed value or state key the function read has changed.

For structure that does not fit Show or For. Values that only change what the components show are better bound inside them (or read with untracked), so the region is not built again.

>>> Dynamic(lambda: Plot() if self.state["kind"] == "plot" else Table())
Parameters:

build (Callable[[], object])

class ngapp.components.reactive.For(
items: Observable | Callable[[], Iterable],
render: Callable[[object], object],
key: Callable[[object], object] | None = None,
otherwise: Callable[[], object] | None = None,
reactive: bool = False,
)#

A list of rows that follows a list of items: rows of items that stay keep their components.

Args:
items: an observable (usually a computed value) or a function

returning the items; the observables and state keys a function reads are followed.

render: function returning the content of one row (a component, a

text, an observable or a list of these).

key: function returning the identity of an item (hashable), the

item itself by default. Rows are kept per key and moved when the order changes.

otherwise: function returning the content for an empty list. reactive: render gets an observable of the item, which is

updated when the item of the key changes, so the row is never built again. Without it render gets the item and a row whose item changed is built again.

Components with an id inside a row need an id that is unique per row (or a row component with namespace=True and a unique id).

>>> For(computed(lambda: check(self.state["radius"])),
...     lambda warning: Label(warning))
Parameters:
  • items (Observable | Callable[[], Iterable])

  • render (Callable[[object], object])

  • key (Callable[[object], object] | None)

  • otherwise (Callable[[], object] | None)

  • reactive (bool)

class ngapp.components.reactive.Fragment(*children: Component | str)#

Children without an element of their own: they appear in the slot of the parent of the fragment, at its position.

The fragment is a component of the tree (ids, namespaces and compute functions of its children work), but it is never mounted: on_mounted and effect belong on its children.

Parameters:

children (Component | str)

class ngapp.components.reactive.Show(
when: Observable | Callable[[], object],
build: Callable[[], object],
otherwise: Callable[[], object] | None = None,
keep: bool = True,
)#

Content that is shown while a condition holds.

Args:
when: an observable (usually a computed value) or a function; the

observables and state keys a function reads are followed.

build: function returning the content (a component, a text, an

observable or a list of these); called when the condition is true for the first time.

otherwise: the same for the content shown while the condition is

false.

keep: the content stays in the page and is only hidden while it is

not shown, so inputs, scroll positions and expensive content survive. With False it is removed and built again.

>>> Show(computed(lambda: self.state["mode"] == "expert"),
...      lambda: Div(QInput(ui_label="Tolerance")))
Parameters:
  • when (Observable | Callable[[], object])

  • build (Callable[[], object])

  • otherwise (Callable[[], object] | None)

  • keep (bool)

ngapp.observable.computed(
fn: Callable[[], T],
name: str = '',
equal: Callable[[Any, Any], bool] | None = None,
) → Computed[T]

Derived read-only observable.

fn is evaluated lazily on the first read and again on a read after one of the observables (or state keys) it read has changed. Dependents are notified only when the result changed: equal(new, old) if given, else identity or ==.

Example#

radius = Observable(1.0, "radius")
area = computed(lambda: math.pi * radius.value**2)
Label(computed(lambda: f"Area: {area.value:.3g}"))
Parameters:
  • fn (Callable[[], T])

  • name (str)

  • equal (Callable[[Any, Any], bool] | None)

Return type:

Computed[T]

ngapp.observable.effect(
fn: Callable[[], Any],
name: str = '',
) → Callable[[], None]

Run fn now and again after an observable it read has changed (at the end of the batch, after bindings were updated).

Returns a dispose function, the effect lives until it is called. For work that belongs to a component use component.effect.

Parameters:
  • fn (Callable[[], Any])

  • name (str)

Return type:

Callable[[], None]

ngapp.observable.untracked(fn: Callable | None = None, /, *args, **kwargs)

Read observables without registering a dependency.

with untracked(): ... or untracked(fn, *args), which returns the result of fn.

Parameters:

fn (Callable | None)

ngapp.observable.batch(defer_listeners: bool = True) → Batch

Group changes: reactions (computed listeners, bindings, effects) and frontend updates run once when the outermost block exits.

With defer_listeners=True plain Observable.on_change() listeners are deferred to the end of this block too, with False they stay synchronous (the scope the framework opens around event handlers).

Parameters:

defer_listeners (bool)

Return type:

Batch

ngapp.flush_updates()#

Run pending reactions and send the component updates collected so far to the browser now (they are sent when the running event handler returns otherwise). Use it before raw JavaScript calls that rely on the updated page and for progress inside a long handler.

Component.effect(fn: Callable[[], None], name: str = '')

Run fn when the component is mounted and again, while it stays mounted, after an observable, computed value or state key it read has changed (once per event, after the bound props were updated).

For imperative updates of the browser: drawing a figure, updating a viewer. Never runs where nothing is shown (compute jobs). Returns a function that removes the effect.

>>> plot.effect(lambda: plot.draw(figure(self.state["radius"])))
Parameters:
  • fn (Callable[[], None])

  • name (str)

State.ref(key: str, default=None, converter=None) → StateRef#

Observable view of a key (the same object per key): its value is the value of the key (default while the key is not set), assigning it sets the key to converter(value), and its on_change listeners are called after a change of the key from any source. As value of a two-way prop (ui_model_value=state.ref("x", 1.0)) it binds an input to the key.

Parameters:

key (str)

Return type:

StateRef

State.notify(key: str) → None#

Report that the value of a key was changed in place: what depends on the key is updated and the value is stored by the next autosave

Parameters:

key (str)

Return type:

None

class ngapp.state_model.StateModel(component, prefix: str = '')#

State with declared keys: use it as App[MyState] or Component[MyState], self.state is then a MyState. Every field needs a default.

Reading a field returns the stored value validated (the default while the key is not set or its value is invalid); setting a field, or the key with state[key] = ..., validates the value. Defaults are plain values or pydantic.Field(...) (constraints, title, json_schema_extra). Types are anything pydantic validates; values are stored as validated, so use types the case store saves (numbers, strings, lists, dicts, TypedDict, Literal).

A field whose type is a StateModel (no default needed) is a nested state: its fields are keys of the namespace name/, each followed on its own (s.core.mur); assigning a dict to it replaces its values.

Everything else of the state works as before: keys that are not fields (state[key]), ns, notify, on_change. Reads of fields are followed like reads of keys; values are replaced, not changed in place.

Parameters:

prefix (str)

classmethod defaults() → dict#

{name: default} of all fields, nested states as dicts

Return type:

dict

dump() → dict#

{name: value} of all fields, nested states as dicts

Return type:

dict

classmethod field_info() → dict[str, FieldInfo]#

{name: pydantic FieldInfo} of the fields (not the nested states), in declaration order

Return type:

dict[str, FieldInfo]

classmethod nested() → dict[str, type[StateModel]]#

{name: StateModel} of the nested states, in declaration order

Return type:

dict[str, type[StateModel]]

ngapp.state_model.ref(
read: Callable[[], T],
) → Observable[T]#

Observable of the field that read returns, bound two-way to its state key when used as ui_model_value: QInput(ui_model_value=ref(lambda: s.radius)).

A value from the input that does not validate is not stored; the observable’s error holds the reason until a value of the key is stored.

Parameters:

read (Callable[[], T])

Return type:

Observable[T]