You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you put a widget in a ipywidgets.Output and using append_stdout on the output the widget will be rerendered once per append.
cell 1:
importanywidgetimporttraitletsimportipywidgetsasiwimportIPython.displayasipdclassTinyWidget(anywidget.AnyWidget):
_esm=""" let num_created = 0 function render({ model, el }) { const my_creation_id = ++num_created; const div = document.createElement("div"); div.innerHTML = `my_creation_id: ${my_creation_id}`; el.appendChild(div); } export default { render }; """out=iw.Output()
without:
ipd.display(TinyWidget())
out
cell 2:
foriinrange(5):
out.append_stdout(f'{i} ')
Expected output:
my_creation_id: 1
0 1 2 3 4
Observed output:
my_creation_id: 6
0 1 2 3 4
Notes:
This only happens when the appends are in a separate cell.
I noticed this and while this is mostly a bug upstreams I wanted to document it here as an issue. There are many open issues with ipywidgets.Output. I don't know exactly which one is relevant in this case but this comment is a good starting point for these issues: jupyter-widgets/ipywidgets#3261 (comment)
In line with that comment, the expected output happens if you use the context manager instead of the append_stdout method, like this:
foriinrange(5):
without:
print(f'{i} ', end='')
The text was updated successfully, but these errors were encountered:
Hi there, thanks for opening the issue! Indeed, this is an implementation detail wrt to how ipywidgets.Output works. In this case, I'd recommend using an ipywidgets.VBox to avoid re-returns (for the custom Jupyter Widget):
I am not sure if anywidget reloads javascript with each render or just runs the render function. If it is just render function, and your num_created is outside render, it is logical to have observed output. Let me explain:
ipywidgets.Output has an outputs attribute that get updated on each time a new display object is created. That attribute is a tuple which is assigned again and again each time an append_<func> is called. Since you are updating 5 times but it was also shown in start, so it becomes 6th time when you changed the outputs attribute. It's infact not appending new objects, it's re-assigning the outputs with all object appended so far.
When you put a widget in a ipywidgets.Output and using append_stdout on the output the widget will be rerendered once per append.
cell 1:
cell 2:
Expected output:
Observed output:
Notes:
This only happens when the appends are in a separate cell.
I noticed this and while this is mostly a bug upstreams I wanted to document it here as an issue. There are many open issues with ipywidgets.Output. I don't know exactly which one is relevant in this case but this comment is a good starting point for these issues: jupyter-widgets/ipywidgets#3261 (comment)
In line with that comment, the expected output happens if you use the context manager instead of the append_stdout method, like this:
The text was updated successfully, but these errors were encountered: