Support the Anywidget Front-End Module specification #5096
azjps
started this conversation in
Ideas / Feature Requests
Replies: 1 comment
-
Thanks for sharing this interesting idea, @azjps! Anywidget could indeed make NiceGUI more accessible for an even wider audience. To me an integration doesn't seem trivial to implement though. But maybe someone from the community would like to give it a go? Some sketchy proof of concept might be enough to pave the way. 🤞🏻 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation: The set of published widgets using
anywidget
is growing and includes a few established plotting libraries such asplotly.FigureWidget
andaltair.JupyterChart
(example request thread: #657; other examples of published anywidgets). I think its the recommended way to publish custom widgets formarimo
, and maybejupyter
too. I'm personally usinganywidget
to render a couple of custom widgets in Jupyter/Marimo & Panel, and it would be nice to be able to directly take them and use them innicegui
.Details: https://anywidget.dev/en/afm/#what-is-afm AFM describes the basic contract for front-end frameworks to support anywidgets, namely calling
render({model, el})
to create a widget and implementing themodel.get/set/save_changes()
API to synchronize state with the backend. On the python side, anywidgets aretraitlets.HasTraits
with syncable properties tagged assync=True
(analogous tonicegui.binding.BindableProperty
), although there are some experimental internal helpers for adding widget MIME bundles to any popular dataclass-like API, e.g.dataclass/pydantic/msgspec
.In practice there's likely a few options to support this:
ipywidgets
: For example, this is the approach of the ipywidgets_bokeh project used bybokeh
&panel
. Pros:anywidget
is (currently) just anipywidget
; otheripywidget
s elements could be supported too. Cons:ipywidgets
is relatively heavy; would have to hook into the internals of thejupyter-widgets
extension module; there's little documentation on the internals but at least there is thisipywidgets-bokeh
example to follow.traitlets.HasTraits
: For example, this is the approach of streamlit-anywidget: iterateHasTraits.traits(sync=True)
to get awidget_state
json dict, sync it to JS via streamlit's components API, implement themodel
API here using streamlit's websockets API to sync changes towidget_state
back to python. [Note that thisstreamlit-anywidget
is a community extension and misses a few features, for example it currently doesn't support thetraits_to_json
serialization escape-hatch fromipywidgets
, or specifying_esm
as aPath
.] I guess this would probably be the easiest approach, using bindings in place of streamlit's components?anywidgets
helper APIs to get the widget state, and thecomm
bridge to handle 2-way state communication. Pros: not tied totraitlets
. Cons: experimental and I believe its a hidden API at the moment.Other details on integrating the JavaScript and comms side with nicegui are completely beyond me, but I'm sure the
anywidgets
maintainers would be happy to provide advice. Also please note these suggestions are just from a quick skim of existing projects I'm aware of, I have no development experience here beyond being a happy end-user.Beta Was this translation helpful? Give feedback.
All reactions