Skip to content

Using ipywidgets.embed on an anywidget widget throws a JavaScript error #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
juba opened this issue Oct 10, 2023 · 2 comments
Closed

Comments

@juba
Copy link
Contributor

juba commented Oct 10, 2023

Hi,

I'm having trouble trying to save a widget created with anywidget to an HTML file with ipywidgets.embed.embed_minimal_html().

Here is a simple example:

import anywidget
import traitlets


class CounterWidget(anywidget.AnyWidget):
    _esm = """
    export function render({ model, el }) {
      let getCount = () => model.get("count");
      let button = document.createElement("button");
      button.classList.add("counter-button");
      button.innerHTML = `count is ${getCount()}`;
      button.addEventListener("click", () => {
        model.set("count", getCount() + 1);
        model.save_changes();
      });
      model.on("change:count", () => {
        button.innerHTML = `count is ${getCount()}`;
      });
      el.appendChild(button);
    }
    """
    _css = """
    .counter-button { background-color: #ea580c; }
    .counter-button:hover { background-color: #9a3412; }
    """
    count = traitlets.Int(0).tag(sync=True)


counter = CounterWidget()
counter.count = 42
counter

from ipywidgets.embed import embed_minimal_html

embed_minimal_html("/tmp/out.html", views=[counter])

Once opened in a browser, the resulting HTML file throws a JavaScript error:

Class null not found in module @jupyter-widgets/[email protected]

I'm not sure if it is a problem related to anywidget or if I don't use this feature correctly.
I'm using anywidget 0.6.5.

Many thanks for all your work !

@manzt
Copy link
Owner

manzt commented Oct 10, 2023

Thanks for opening this issue. I just had a look, and it seems the _esm and _css traits are considered defaults, which embed_minimal_html explicitly ignores when serializing state.

The error message you got doesn't seem to be helpful at all in resolving this issue, but I had the following work:

embed_minimal_html("/tmp/out.html", views=[counter], drop_defaults=False)

Can you let me know if this resolves your issue?

@juba
Copy link
Contributor Author

juba commented Oct 10, 2023

Fantastic, I can confirm it solves the issue for my use case.

Many thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants