The following code works in a jupyterlab cell, and I can see the output from my chrome console,
from IPython.display import Javascript
my_js = “”"
console.log(‘hello world!’);
“”"
Javascript(my_js)
but when I move it into a button click event function, nothing shows up in chrome console,
button = widgets.Button(
description=‘Button’,
disabled=False,
button_style=’’,
tooltip=‘Button’,
icon=‘check’
)
The reason that the Javascript object runs the JS when you run in a cell is because the kernel displays the result of any unterminated (;) expression in the cell. So, just calling Javascript isn’t enough to execute it; it is the fact that it is the last expression in the cell that means it is executed. If you want to run this sort of code inside a cell, you can call display() and pass in the Javascript object.
I have already tried that. Unfortunately, adding “display” won’t solve this issue. It will show up in jupyter log view as "
<IPython.core.display.Javascript object>" instead of executing that javascript in browser.
Now, I believe the reason that a bare display doesn’t work is that the comm message isn’t handled by any particular cell, so only the JupyterLab log console receives it. For whatever reason, this does not execute Javascript display objects. You could solve this by using the HTML display object with a <script> tag instead, or use an Output widget: