clear_output and display widgets (from ipywidgets) connection

1,741 views
Skip to first unread message

Ilya Kazakevich

unread,
Jul 10, 2019, 8:00:44 PM7/10/19
to Project Jupyter
Hello,

According to manual, ``clear_output`` event should clear whole cell output.

But with the following code

from ipywidgets import widgets
from IPython.display import clear_output
out = widgets.Output(layout={'border': '1px solid black'})
out.append_display_data(10)
display(out)
display("20")
out.clear_output()

only "10" is cleared, while "20" stays untouched.

I sniffed websocket, and I see absolutely regular "clear_output" is sent. 
Then, how does jupyter "understands" that only widget content must be cleared? This is not comm message, so no connection to widget is made.
How does it work?

Any help (doc or source link probably) is appreciated.

Thank you.
Ilya.

William Stein

unread,
Jul 10, 2019, 8:50:02 PM7/10/19
to jup...@googlegroups.com
On Wed, Jul 10, 2019 at 5:00 PM Ilya Kazakevich
<ilya.ka...@jetbrains.com> wrote:
>
> Hello,
>
> According to manual, ``clear_output`` event should clear whole cell output.
>
> But with the following code
>
> from ipywidgets import widgets
> from IPython.display import clear_output
> out = widgets.Output(layout={'border': '1px solid black'})
> out.append_display_data(10)
> display(out)
> display("20")
> out.clear_output()
>
> only "10" is cleared, while "20" stays untouched.

The above behavior is correct, and what I would expect. The reason is
because `out.clear_output()` **only** clears the output of the widget
"out". The "20" that you've output has nothing to do with that output
widget (the one called "out"). The "20" is simply a completely
separate output. To clear everything, which is I think the behavior
you expect, do this, which should work as you want:

from ipywidgets import widgets
from IPython.display import clear_output
out = widgets.Output(layout={'border': '1px solid black'})
out.append_display_data(10)
display(out)
display("20")
clear_output() # NOTICE: I'm calling the global clear_output
function, not the clear_output method of out.

-- William

>
> I sniffed websocket, and I see absolutely regular "clear_output" is sent.
> Then, how does jupyter "understands" that only widget content must be cleared? This is not comm message, so no connection to widget is made.
> How does it work?

It's because you type "out.clear_output()" instead of
"clear_output()". This causes a comm message to get sent from the
kernel to the widget saying "clear me". The way out.clear_output
works is a bit surprising, since it involves different back and forth
communication between the kernel and the frontend than you might
expect. This confused me a lot when I was implementing widgets for
CoCalc recently. Search for "clear_output" on this page
https://github.com/jupyter-widgets/ipywidgets/issues/2385#issuecomment-484742927
for where Jason Grout clears up my confusion about how this work...

-- William

>
> Any help (doc or source link probably) is appreciated.
>
> Thank you.
> Ilya.
>
> --
> You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/11d9b97c-5827-4b48-b424-63c92c8f6de5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
William (http://wstein.org)

Ilya Kazakevich

unread,
Jul 10, 2019, 8:51:50 PM7/10/19
to Project Jupyter
Thank you!
I was trying to understand how is it implemented technically, how it works under the hood

William Stein

unread,
Jul 10, 2019, 9:01:35 PM7/10/19
to jup...@googlegroups.com
If that GitHub issue I pointed to isn't sufficiently clear, please
reopen it and add additional questions or make a new one. Jason Grout
told me that he really likes using GitHub issues to answer technical
questions about how ipywidgets works.

Are you guys implementing IPywidgets support for PyCharm? Having
recently implemented a new client (not jupyterlab or jupyter classic),
I think it will be very good for the ipywidgets project if there are
even more integrations of ipywidgets into different clients out there...


-- William

Jason Grout

unread,
Jul 11, 2019, 7:53:37 AM7/11/19
to Project Jupyter
On Wed, Jul 10, 2019 at 8:01 PM William Stein <wst...@gmail.com> wrote:
On Wed, Jul 10, 2019 at 5:51 PM Ilya Kazakevich
<ilya.ka...@jetbrains.com> wrote:
>
> Thank you!
> I was trying to understand how is it implemented technically, how it works under the hood


I'll also add that the answer to

> Then, how does jupyter "understands" that only widget content must be cleared? This is not comm message, so no connection to widget is made. How does it work?


with self:
    clear_output(*pargs, **kwargs)

which means that calling that method *first* sends a comm message redirecting output, *then* sends the global clear_output message (which is redirected to the widget), then exits the context manager undoing the output redirection.

 
If that GitHub issue I pointed to isn't sufficiently clear, please
reopen it and add additional questions or make a new one.  Jason Grout
told me that he really likes using GitHub issues to answer technical
questions about how ipywidgets works.


I like using GitHub issues because they seem to better support answers (for example, I can inline code and links and they are formatted nicely), and they are more searchable than chat or I think even mailing list posts. So our "reference" issue milestone has accumulated a number of stack overflow type questions and answers.

 
Are you guys implementing IPywidgets support for PyCharm?   Having
recently implemented a new client (not jupyterlab or jupyter classic),
I think it will be very good for the ipywidgets project if there are
even more integrations of ipywidgets into different clients out there...


+1 to having more clients implement ipywidgets support. Are you? Let us know if you are having any other issues. Output widgets in particular are still a bit tricky.

Thanks (and thanks William for answering as well!)

Jason


Ilya Kazakevich

unread,
Jul 11, 2019, 8:38:46 AM7/11/19
to Project Jupyter
Jason, William, thank you.
I now understand how this redirection works. It would be nice to document it somewhere in https://jupyter-notebook.readthedocs.io/en/stable/comms.html

Yes, we are implementing ipywidgets for PyCharm:)

Ilya.



Jason Grout

unread,
Jul 11, 2019, 8:42:43 AM7/11/19
to Project Jupyter
That's great to hear!

If you're at scipy right now, I'd love to touch base with you in person about this effort.

Thanks,

Jason


--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages