Repository navigation
Conversation
…asier widget authoring interface, refs #20
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Companion PR to developmentseed/myst-anywidget-static-export#5
The overall aim is to arrive at an easy-to-understand lightweight structure and API for widget authors, whether its simple widgets defined inline in a notebook, or more complex widgets. The aim is to remove all copy-paste boilerpalte and present a clean API.
I worked through the definition of the user API for widget construction with Claude, and then worked backwards from that for changes in the static export plugin and then here.
AI summary of changes:
Custom widgets without copy-paste: a JS runtime in the wheel,
mw.link,extend, theming everywhereWhy
Writing a widget outside this package was painful:
packages/coreshipped nowhere, so every custom app copiedabout 150 lines of them, and the copies drifted.
camera sync, and re-implemented button and card CSS.
Binderdid not work in a live kernel, layout containers ignoredtheme=, and thedocs contradicted the code in several places.
This PR gives notebook authors a small, importable runtime and removes the workarounds
at their source. It keeps the anywidget model and vocabulary: plain
export default { render },BaseWidget,_esm,_css, traitlets andjslink/jsdlinkall work unchanged.What an author writes now
No Node, no build step, no vendored file. The same code runs in JupyterLab and in a
static export.
What changes
A JS runtime, delivered inside the wheel
import { … } from "manywidgets"works in an inline_esmstring or a.jsfile.BaseWidgetresolves the import when it loads_esm, including on hot reload. Awidget that does not import
"manywidgets"is passed to anywidget untouched.so exported pages stay self-contained and need no extra network request.
widget(render)orwidget({ initialize, render }):model.setsaves;model.on("value", fn)is removed on cleanup.theme=/style=are applied automatically.model.widget("trait")/model.widgets("trait")reach other widgets through onesmall handle:
get,set,on,send,mount(el, { fill: true }).extend(Base, { … })withimport Base from "manywidgets:super"extends a parentclass's JS. Built-ins publish hooks:
Stat.format,Chart.configure,Table.formatCell,GeoMap.onMap.flyTo,syncCameras,lngLatAt,pointAt.mw-*classes (mw-button,mw-card,mw-chip,mw-switch,mw-tile, …) style acustom widget like the built-ins.
manywidgets(packages/core) for authorswho want TypeScript or npm dependencies. It is not published yet.
Python
mw.link((src, "field"), (dst, "field.or.path"), js=None, both=False): a browser-sidelink with a JavaScript transform or a nested target. It works live and static and does
not need to be displayed.
mw.dev()turns on hot reload from inside a notebook.mw.assetis public.manywidgets new widget <name> [--dir DIR] [--bundled]andmanywidgets doctor(checks the plugin pinned inmyst.yml).Built-in widgets
widget().LayerToggle,LayerFilter,FilterBinderandMapComparereact tochange events instead of re-applying state every 50–100 ms.
LayerToggle,LayerFilterandFilterBinderwork without being displayed.MapComparesyncs cameras withsyncCamerasand sizes its maps withmount(el, { fill: true })instead of!importantCSS.Bugs fixed
Binderfailed in a live kernel with "widget model not found".Row,Column,GridandGridItemignoredtheme=/style=; they now apply themand cascade to children.
_csslost to the built-in defaults.grid_item/dist/widget.jswas missing from the wheel'sensured-targets.save_changes()needs wrapping instatic export; neither is true.
Behaviour changes to be aware of
light_theme()/dark_theme()/radix_theme()now overrides the page's coloursmount()renders the child into a wrapper element.slot > .childFilterBinderand link status lines have new text and themw-statusclassToggle/LayerTogglethumb is whitemanywidgets:superhas its_cssappended to the parent'sDeprecated and removed
Binder(usemw.link; emits aDeprecationWarning),and the JS helpers
safeSaveChanges,onChangesandresolveModel(one consolewarning per page).
getStaticRegistryandfindAllInRegistry(internal), and_myst_child_traitsfrom the built-in containers.Upgrading
UPGRADING.md(new) gives each step as before → after with a search pattern, andCHANGELOG.md(new) has the full list. In short:runtime they were built with.
myst-anywidget-static-exportv0.4.0 (host.protocol1).With an older plugin, widgets still render in a reduced mode and the console says to
update; anything that is not displayed (
mw.linkwithjs=or a dotted path,undisplayed
LayerToggle/LayerFilter/FilterBinder) does not run there.is optional in this release.
Testing
npm test: 235 passing.npm run typecheck: clean.pytest: 214 passed, 1 skipped.plugin's v0.4.0 branch: inline widget,
mw.link(transform, nested path, undisplayed),widget references with
set/on/send/mount,extend, theme cascade, andthe reduced mode against plugin v0.3.2.
manywidgets-playgroundwere migrated to this API as theacceptance test:
app.jsshrank by 52–66% with the same behaviour.--bundledscaffold in a browser.Docs
docs/guides/create-your-own-widget.mdis rewritten as a notebook-first guide.docs/guides/theming.mdanddocs/guides/contributing-a-widget.md(the in-packageguide that used to live at the first path).
linking.md,static-export.md,lonboard.md, the README and the agent skill areupdated; the skill gains a theming reference.
Known gaps
MapCompare's internal camera sync cannot be paused from outside. Flying both maps atonce works, but there is no supported control for it.
handle.onon a lonboard layer trait stopped firing in one live probe; causeunconfirmed. Writes always work.
predate this PR.
manywidgets doctorpoint at plugin v0.4.0, which is notreleased yet.
docs/myst.ymlstill pins v0.3.2 and should move at release.Before release
myst-anywidget-static-export, v0.4.0); release that first.packages/coreto npm asmanywidgets(the name is unclaimed) so--bundledworks without a local install.
This is a draft. There's a bunch of work to do here to cleanup, document the clean end-user interface, migrate all examples in
manywidgets-playgroundand test everything. This will be a release with breaking changes, but I think it solves most of the major issues that required awkward hacks on the widget authoring side.To complete this, I'll also publish a
manywidgetspackage tonpmso that it can be imported directly for frontend widget definitions (if using it inline in Python, one can also import it via the Python module).I still need to parse a bunch of these changes - creating the PR as draft and then I'll work on whittling things down, etc. over the next days.
cc @zacdezgeo @dzole0311