Skip to content

Modifying a particular dictionary element in the model #641

Closed Answered by manzt
psychemedia asked this question in JavaScript
Discussion options

You must be logged in to vote

Two things here. You are mutating the inner value and not calling model.save_changes() to sync back with Python.

I believe ipywidgets checks by reference for equality (to decide whether a sync message is needed), so you need to create a shallow copy for the current dict. Here is the counter example from the docs, using a dict to hold the state.

import anywidget
import traitlets

class Widget(anywidget.AnyWidget):
    _esm = """
    function render({ model, el }) {
      let btn = document.createElement("button");
      btn.innerHTML = `count is ${model.get("data").value}`;
      btn.addEventListener("click", () => {
        const data = { ...model.get("data") }; // create a shallow copy

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by psychemedia
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants