jupyter / help

:sparkles: Need some help or have some questions? Please visit our Discourse page.
https://discourse.jupyter.org
291 stars 97 forks source link

An ipywidget button on_click event runs locally but not on nbviewer #425

Closed bjsmith closed 6 years ago

bjsmith commented 6 years ago

I created a simple button test ipynb file.

The code is here:

from ipywidgets import widgets
from IPython.display import display
from IPython.display import Markdown

txtArea = widgets.Text()
display(txtArea)

myb= widgets.Button(description="234")
def add_text(b):
    print("hello")
    b.description=b.description+"1"
    txtArea.value = txtArea.value + txtArea.value

myb.on_click(add_text)

display(myb)

And you can see it live on nbviewer at the following link:

https://nbviewer.jupyter.org/github/bjsmith/motivation-simulation/blob/master/buttontest.ipynb?flush_cache=please

I want to use the button to update the text area but not only does the text area not update but I can't seem to do anything else with the button. It seems like it is not binding correctly.

I have ensured that I saved the state locally before uploading it to my github.

Any ideas?

jasongrout commented 6 years ago

The widgets typically require a kernel to work. Nbviewer does not provide a kernel.

You can do some things with jslink to hook things up in just javascript, and those work in nbconvert. But anything that depends on python code will need a kernel. For those cases, something like Binder works better (which provides the kernel).

bjsmith commented 6 years ago

Thanks for your answer. For my particular case, binder works very well.