bryntime
v2.3.0
Published
The Brython runtime for JavaScript frameworks
Maintainers
Readme
Bryntime
The Brython runtime for JavaScript frameworks.
Brython runs Python in the browser. Bryntime is what a JavaScript framework, or a page with none, needs on top of it to use Python's values as its own: to read them, to write them, to call them, and to know when they change.
Install it from npm:
npm install bryntimeimport { viewOf, track, pyExport } from 'bryntime';
const list = viewOf(todos); // a Python list of dicts, read as an Array of objects
list[1].done = true; // written into the Python dict, as `todos[1]["done"] = True`
track(todos, render); // told of every change, Python's `todos.append(…)` included
const add = pyExport(addTodo); // a Python function JavaScript callsWhat it does
Python and JavaScript do not see a value the same way. A dict, a set and an
instance are Brython objects to JavaScript, None is an object rather than
undefined, and a mutation made in Python, rows.append(x), d["k"] = v,
point.x += 1, happens inside the engine, where no framework sees it.
Bryntime closes each gap:
- Live views (
viewOf): a list read as anArray, a dict and an instance as objects, a set as aSet. Reads are live, and JavaScript's writes are Python's writes. A container of the author's class,class Rows(list), shows its properties and methods too, as a subclass ofArrayshows its getters. An iterable made as it goes, a generator,map, arange, is read as the listlist()makes of it, made at the first read of what it holds; until then Python reads it back as the iterable itself, untouched (listedNowmakes the list at once). A value a framework made of data it had handed out as its own proxy is handed as that proxy from then on, by every door, and read back as the value (handedAs), so a page keeps the object it read. - Change tracking (
track,follow): a subscriber is told of every change Python makes to a value or to anything inside it, whichever operation makes it, and of nothing else: a read, a copy, or a write of the value already held tells nobody, as a framework's own state does. What no tracking follows, Python's reads of JavaScript's own object, is counted while a framework weighs an expression (weighingReads,javascriptReads), so that it reads such an expression again at each render, as it reads its own. - Copies under one rule (
plainData,pyData):Noneis JavaScript'sundefinedat every door andnullin JSON alone,javascript.NULLisnull, and two keys JavaScript cannot tell apart are refused, not merged. - Calls (
pyExport): a Python function JavaScript calls, and JavaScript functions Python calls, their arguments and results crossing both ways. - Errors (
pyError,refused): what Python raises reaches JavaScript with its message and its stack, and what Bryntime refuses is signed with the framework's name. - Modules (
bind,withNamespace,runningLine,runningModule): a Python module whose globals are a framework's states, each name an accessor over one of them, withglobals(),delandimportbehaving as Python's, and the line of its own body a write or a call comes from. - A template's dot (
readAttr,writeAttr,writeItem):pep.titlein a template reads the attribute, or the key of a dict failing that, as Jinja's dot does, and a write lands where that read comes from; one rule for every framework's template, Svelthon's and Vuython's. - Another process (
pickled,unpickled): pickle's protocol written in JavaScript, for a framework that carries a value from its server to its browser: a structure its serialiser writes, out of which comes the same value, its types, its sharing and its cycles, an instance rebuilt by its class's__reduce__,__getstate__,__setstate__and__slots__, the items of a list's or a dict's subclass as its__iter__or itsitems()hands them, a class or a function by its name; refused where pickle refuses.unpickled(…, { restricted: true })reads what came from a side the reader does not trust, values alone.roundTripdoes both at once: the structure that crosses, and the copy the other end rebuilds of it, which a server's own page can read; withkeep, the value itself where no copy could differ from it. A coroutine crosses as the promise of its result, which comes out a future Python awaits. An object a framework hands JavaScript in place of such a value, before it can be rebuilt, reads in Python as that value (standsForPython), wherever Python finds it, in a JavaScript array or object too, and so does one the framework made itself, which the host recognises when Python first meets it (readsAsPython).
The same value on the server and in the browser
A framework that renders a page on its server and again in the browser sends the page's data from one to the other, and both renders must read the same values. For JavaScript's values, its serialiser does that. For Python's, Bryntime has pickle:
import { pickled, unpickled } from 'bryntime';
const sent = pickled(data); // on the server, for the framework's own serialiser
const same = unpickled(sent); // in the browser: the same valueA tuple comes back a tuple, an int key an int, 1.0 a float, one object
reached twice one object, a cycle a cycle, and an instance of a class an
instance of it, rebuilt by its __reduce__ or its __setstate__. Nothing
here is tied to one framework: Svelthon installs it in SvelteKit's
transport, and any framework with a hook for its own types can do the same.
What crosses and what is refused is the list under Another process above.
Two things do not come back by value. What pickle carries by reference: a
class, a function, a builtin, and an object whose __reduce__ returns a
string, a named singleton. Each is written as its module and its qualified
name, which must reach that very object again (pickle's save_global); a
local object, or one with no module, is refused. The reader finds it again by
importing the module and walking the name, never through a dunder: the
engine's own modules are refused, restricted: true refuses every reference,
a host's refused names the modules the other side cannot import, and its
missing is told of one the reader lacks. A reader with no engine cannot
resolve a reference at all, and what it shows in its place is the host's
choice. And the id() a default repr shows, which belongs to each
process. Everything else, values, types, sharing and cycles, comes back equal.
A framework's side
A framework imports one entry, bryntime, and answers through host what
only it knows: whether one of its states owns a value (owns), how it makes
an object its state (adopt), its name (name), whether a reaction is
running (reacting), whether a write would be refused (refuses), a
subscription (subscriber), a read that follows nothing (untrack),
whether an unbound name read at a line is an absence (absent), whether a
module's namespace is read-only through globals() and its other doors
(readOnlyNamespace), and what a value of its own crosses to JavaScript as, at
every door (crossesAs): React's element for a t-string. With no host
at all, every read is taken for a reaction's and nothing is refused, which is
what a page with no framework wants.
import { host } from 'bryntime';A compiler loads Brython's other half onto the same engine:
import 'bryntime';
import 'bryvend/compiler';Demo
demo/ is a page with no framework and a to-do list held by a Python module:
the tasks instances of a class, their tags a set, the filter a global.
JavaScript renders them, ticks a box, stars a task and removes one by writing
into Python's own values; Python adds a task, refuses an empty title with a
ValueError the page shows, deletes the done ones and rebinds todos = [];
and the page is told of each change, whichever side made it.
It is online at https://svelthon.github.io/bryntime/, deployed from main
once the tests pass. Locally, it is served as it is, from the repository's
root:
python3 -m http.serverthen http://localhost:8000/demo/. The suite runs the same page, so the demo
cannot drift from the API it shows.
The engine
Bryntime runs on Bryvend, its peer dependency: Brython's master and the repairs
proposed there and not merged yet, one commit each, as an ES module. Bryvend's
BRYTHON_FIX.md lists them all, with the edits made in the file itself, and
the tests here hold each repair Bryntime rests on.
A page loads bryvend, the half without the parsing chain: 238 KB gzipped. A
compiler adds the other half, bryvend/compiler.
What Bryntime asks of the engine, member by member, is in BRYTHON.md.
License
Copyright (C) 2026 Florent Gallaire [email protected]
Bryntime is licensed under the BSD 3-Clause License, the same license as Brython.
