How to debug _esm javascript code? #776
-
Hi, I am new to Python and AnyWidget, and this is my first time posting to this forrum. I am using PyCharm and am trying to debug javascript that is assigned to _esm in my widget. Please can you help me undertand what I need to do to be able to do this? I am testing the problem with the AnyWidget counter example with the javascript in a js file. When I set a breakpoint in the javascript and run the widget, the program does not stop at the breakpoint. When I debug in chrome, I can see that the reason for this seems to be that a new js file is dynamically created each time I run the program, so the newly created file does not have the breakpoint, hence not stopping, as shown in the below screenshot. I can't find where I can view the actual counterwidget.js file in the Chrome developer tools. This is the code I am using to show the problem: Jupyter:
Widget:
JavaScript (counterwidget.js):
Please can you help me figure out what I need to do to stop in the javascript? Thanks in advance for your help with this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Here is another image that better illustrates the issue. |
Beta Was this translation helpful? Give feedback.
-
Thanks for bringing this up! The way Personally, if I need to debug, I usually add a function render({ model, el }) {
let count = () => model.get("value");
let btn = document.createElement("button");
btn.innerHTML = `count is ${count()}`;
btn.addEventListener("click", () => {
++ debugger;
model.set("value", count() + 1);
model.save_changes();
});
model.on("change:value", () => {
btn.innerHTML = `count is ${count()}`;
});
el.appendChild(btn);
}
export default { render }; Finally, if you really want to set breakpoints via the Chrome UI, you could serve the module separately (e.g., with a local development server) and load that directly in the notebook during development. For example: class Widget(anywidget.AnyWidget):
# _esm = "counterwidget.js
_esm = "http://localhost:8000/counterwidget.js"
value = traitlets.Int(0).tag(sync=True) In this setup, the browser will load |
Beta Was this translation helpful? Give feedback.
Thanks for bringing this up! The way
anywidget
ensures compatibility with Jupyter involves dynamically regenerating the JavaScript module (via dataurl), which is why I'm guessing setting breakpoints directly in Chrome DevTools doesn’t seem to work. If/when #628 lands, I think this could change this behavior and allow setting breakpoints in the UI... but not sadly not currently.Personally, if I need to debug, I usually add a
debugger
statement directly in the source code. This reliably triggers the debugger in Chrome DevTools. For example: