1

my question is about interactive Jupyter notebooks.

I want to display JavaScript button and print something to cell as result of button click.

We can do this easely with ipywidgets:

def OnClick(b=None): print 'qq'
b = widgets.Button(description='Button')
b.on_click(OnClick)
display(b)

But when we jumps to pure JavaScript things goes wrong. I.e. I have JavaScript Button, in on_click() event handler I use kernel interaction like:

var kernel = IPython.notebook.kernel;
kernel.execute('onClick()');         

Callback is called successfuly (I checked by beep), but print produce no output in cell, where by button is displayed. So I suppose there should be some magic (like everyting in Python world!) to acceess print area, could you please help me to cast it?

1 Answer 1

7

The key is to use Output widget

from ipywidgets import widgets
from IPython.display import display, HTML

out = widgets.Output()

def OnClick():
    with out:
        print 'QQQ'

display(HTML('<a onclick="IPython.notebook.kernel.execute(\'OnClick()\')">Click!</a>'))
display(out)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.