Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipywidgets button on_click event not getting called in Jupyter Notebook / JupyterLab 7.X #3949

Closed
jsid72 opened this issue Sep 14, 2024 · 1 comment

Comments

@jsid72
Copy link

jsid72 commented Sep 14, 2024

Description

ipywidgets works well in Jupyter Notebook 6.X but does not work in the latest Jupyter Notebook / JupyterLab 7.X.

Reproduce

  1. Setup a new Python Virtual Environment with the latest Jupyter Notebook 7.X and ipywidgets 8.1.5
  2. In a Jupyter Notebook run the following code:
from ipywidgets import widgets
from IPython.display import display
 
def test_function(btn):
    print("Button was clicked")
 
button = widgets.Button(description="Test Button")
button.on_click(test_function)
 
display(button)
  1. Running the above code shows the button but clicking the button does not produce expected output.

The above code works as expected on running it in Jupyter Notebook 6.X

Expected behavior

Click on button should display output but nothing appears.

Context

  • ipywidgets version 8.1.5
  • Operating System and version: Windows 11
  • Browser and version: Chrome 128
@rsnell-usgs
Copy link

@jsid72 You need to send the output to an Output widget as per this page: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html

from IPython.display import display
button = widgets.Button(description="Click Me!")
output = widgets.Output()

display(button, output)

def on_button_clicked(b):
    with output:
        print("Button clicked.")

button.on_click(on_button_clicked)

I was recently caught out by this same issue. In v6 of Notebook this output would be shown but not in v7 which is based on JupyterLab. JupyterLab sends output generated by events that are out of the normal processing flow to the log it would appear. A real pain and significant breaking change for sure. I was also caught out by the fact that VS Code behaves like Notebooks v6 and that si what I used to develop my Notebook, see here: microsoft/vscode-jupyter#15996

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants