Closed jakobtroidl closed 7 months ago
I think this could be related to https://github.com/flekschas/jupyter-scatter/issues/37. Specifically, I think there is some race condition with dynamically using clientWidth
from the element given to then render function. In JupyterLab, things work just fine. I think you could use a setTimeout
trick to append elements to the DOM, and wait a tick to inspect clientWidth:
import * as d3 from "https://cdn.jsdelivr.net/npm/d3@6/+esm";
import { BarChart } from "./barchart.js";
import "./widget.css";
function render({ model, el }) {
/** ... */
setTimeout(() => {
barchart.init();
barchart.update("outgoing_syn");
})
}
export default { render };
Adding a setTimeout
before initializing the bar chart works. However, I don't think it relates to dynamically using clientWidth
. Even if I hardcode my widget's width, this problem happens. Anyway, the above method solves my problem. Thanks so much for your help, @manzt!
Sorry, I think it's related to whether the el provided is already inserted in the DOM. In jupyter it seems like it is, but in VS Code is seems like it may not be (which is why you need to wait a tick). If i had time to dig in, i'd maybe open an issue in vscode-jupyter.
I am running into issues with my widget rendering only upon the first cell execution. For the second and following executions, the widget does not show up (in particular, the SVG component that I use to draw a D3 chart -- see the repo link below). However, if I modify the javscript code while
npm run dev
is running and the esm code rebuilds, my widget reappears.I think it has to do with how often the js
render
function is called. For some reason, the render function gets called three times on the first run after building the JS code. Then, my widget renders correctly. For all other later cell executions, the widget only renders once, and then my widget doesn't display.Here are the core components of my widget:
widget.js
__init__.py
example.ipynb
Here's a relatively minimal repository to reproduce the problem. The issue is consistent across VS Code (v1.87.2) and JupyterLab. I am using
anywidget
version0.9.6
.