manzt / anywidget

jupyter widgets made easy
https://anywidget.dev
MIT License
451 stars 35 forks source link

DOM classes do not update in frontend. #618

Closed asaboor-gh closed 3 weeks ago

asaboor-gh commented 2 months ago

After displaying a widget Inherited from anywidget, CSS dom_classes are not updated in frontend. See an example below. It should remove styling after running last cell, but it doesn't.

image

manzt commented 3 weeks ago

This behavior is intentional, although not well documented - sorry for that!! We're intentionally narrowing down the APIs required for widgets in anywidget (#579), which is what makes it compatible with more platforms than regular Jupyter Widgets, such as Marimo. Relying on inherited behavior from ipywidgets breaks these compatibility guarantees. Not every widget needs dom_classes, so we're moving away from it as an API since it can easily be added with traitlets.

For context, building on top of ipywidgets (Python) introduces a long tail of APIs that are difficult to maintain and provide guarantees for. This also means that every class inheriting from ipywidgets inherits behaviors that can lead to inconsistencies and unnecessary complexity across all widget APIs.

In the long run, I believe anywidget will be bounded and limited by a reliance on ipywidgets, which will constrain its potential as a portable widget standard beyond Jupyter. By reducing dependencies and focusing on a minimal, maintainable base class, we're aiming to create a more flexible and robust foundation that can support a wider range of platforms and use cases.

asaboor-gh commented 3 weeks ago

I think it will become harder for people with little knowledge of javascript to build complex widgets on top of anywidget. For example, in this _dom_classes case, I found that adding classes is pretty easy, but mapping existing classes on removal of a class requires to know a little bit more javascript.

The intriguing aspect here lies in the minimal hurdle remaining to construct intricate widgets using AnyWidget, as opposed to Ipywidgets. This situation could actually motivate folks to dip their toes into a bit more JavaScript—a beneficial practice, indeed!

manzt commented 3 weeks ago

That's a fair critique, and I appreciate you bringing it up. The concept of "easy" vs. "hard" is indeed subjective, and I recognize that many Python-oriented frameworks aim to make web development more accessible by abstracting away JavaScript or simplifying common tasks. However, my philosophy with anywidget has been to make the web less intimidating by lowering barriers to try learning some JavaScript, rather than hiding JavaScript.

This approach distinguishes anywidget from other frameworks and has resonated with many of the people now building widgets on top of it. Whether this pays off in the long term remains to be seen, but I see it as a core part of our ethos and something I want to continue pushing forward.

That said, I also believe there’s room for abstractions to be layered onto anywidget's "core" that could make patterns like _dom_classes easier for non-JS developers. However, I think baking those abstractions into the core would be a mistake, as it could dilute the flexibility and learning opportunities that anywidget offers.

manzt commented 3 weeks ago

I’d also argue that _dom_classes is a debatable API. While some might prefer not to learn JS, _dom_classes exposes web details to Python, which is a leaky abstraction. Users still need to understand the DOM to use it effectively. From a design perspective, I’d even call it an antipattern for authoring widgets—changes in model state should trigger adding and removing classes rather than directly manipulating the DOM through Python. This API also underscores a fundamental limitation of ipywidgets: inheriting from a large base class leads to ambiguity between APIs for widget developers and APIs for end users.

asaboor-gh commented 3 weeks ago

Yeah, I understand it's a lot of baggage of inherited API in ipywidgets(a lot of which is unnecessary for common user). I appreciate Anywidget made it as simple as possible while encouraging users to learn Javascript. I feel more in control and satisfied when I do Javascript, CSS etc. than just pasting images of results from Python into PowerPoint. I was already a fan of interactivity which led me to make a presentation tool in Jupyter and Anywidget helped me to bring up full scale interactivity which otherwise was too hard to implement. 🚀