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

clear_output from code #432

Closed
zivy opened this issue Dec 21, 2016 · 7 comments · Fixed by IRkernel/IRdisplay#54
Closed

clear_output from code #432

zivy opened this issue Dec 21, 2016 · 7 comments · Fixed by IRkernel/IRdisplay#54

Comments

@zivy
Copy link

zivy commented Dec 21, 2016

This is repeat of a question I posted on stack-overflow (no response there so hopefully someone here can help):

I am trying to incrementally plot a graph, animating the progress of an image registration process. In a Python notebook this is done by repeatedly plotting and clearing the output. A MWE:

import matplotlib.pyplot as plt
%matplotlib inline
import time

from IPython.display import clear_output

values = (1.23, 4.5, 1.23, 4.5)
for i in range(1,5):
    clear_output(wait=True)
    plt.plot(0, values[0], 'b*')
    plt.plot(values[0:i], 'r')
    time.sleep(0.5)
    plt.show()

The equivalent in R (missing the clear-output):

values <- c(1.23, 4.5, 1.23, 4.5)

for (i in 1:4) {
  # clear output
  plot(values[1:i], type="l", las=1, col="red")
  points(1, values[1], col="blue", pch=18)
  Sys.sleep(0.5)
}

In practice the plots will not be created in a for-loop, these are callbacks from the computationally intensive image registration. I am using the for loop as the MWE.

Is it possible to programmatically clear the output using the ir kernel? If yes, how? If not, any suggestions on creating animations in the notebook. This needs to be an online solution - saving images and creating an animated gif is not relevant for this purpose.

For those curious about the specific goal, this is part of our development of Jupyter SimpleITK notebooks.

Any advice is much appreciated.

@flying-sheep
Copy link
Member

there currently isn’t.

@takluyver can you point me to where this is specified in the protocol? do we already handle the request in the kernel but don’t provide an API in IRdisplay?

@takluyver
Copy link
Member

Here you go.

I don't think we implement any part of it yet. It's pretty simple, though - there's no request/reply, just a display message emitted by code executing in the kernel. We should be able to add support without too much difficulty.

@zivy
Copy link
Author

zivy commented Dec 22, 2016

I'd also like to point out that this feature would enable live animation via callbacks which in general are useful for visualizing algorithm behavior/progress. In the CS domain this has been identified as a useful way of enhancing understanding, for example see here.

The embedded ability to visualize algorithm progress was one of the reasons we originally adopted IPython/Jupyter as our tool of choice for illustrating the usage of our toolkit.

Would really appreciate it if this was supported in the near future.

@switalaartur
Copy link

Hey,
I know that this is quite old post but I have the same issue, was that maybe solved in the meantime?

@flying-sheep
Copy link
Member

Sadly no, but it should be easy to do. Do you want to do a pair of PRs? You’d probably

  1. Add the option jupyter.clear_output_func here: https://github.com/IRkernel/IRdisplay/blob/80e758a/R/options.r#L43-L45
  2. Add a clear_output function using send_response next to display_data

    IRkernel/R/execution.r

    Lines 95 to 104 in dcfb7d0

    display_data = function(data, metadata = NULL) {
    if (is.null(metadata)) {
    metadata <- namedlist()
    }
    send_response('display_data', current_request, 'iopub', list(
    data = data,
    metadata = metadata))
    invisible(TRUE)
    },
  3. Set the option to that function here:
    options(jupyter.base_display_func = .self$display_data)

As @takluyver said, the docs here: http://jupyter-client.readthedocs.io/en/latest/messaging.html#clear-output

@switalaartur
Copy link

switalaartur commented Oct 31, 2020

To be honest I am not sure I understand well enough how IRKernel works internally to make a PR.

I found that in IJulia clear_output function looks like that:

function clear_output(wait=false)
    # flush pending stdio
    flush_all()
    empty!(displayqueue) # discard pending display requests
    send_ipython(publish[], msg_reply(execute_msg::Msg, "clear_output",
                                    Dict("wait" => wait)))
    stdio_bytes[] = 0 # reset output throttling
end

And flush_all is:

function flush_all()
    flush_cstdio() # flush writes to stdout/stderr by external C code
    flush(stdout)
    flush(stderr)
end

As well as I know in r there is flush.console() but I'm not sure if that what would work here, maybe just dev.off() would be better.
where flush is standard Julia function.

However I have hard time understanding what is pending display request in IRKernel and how to transalte this send_ipython to send_response.

@flying-sheep
Copy link
Member

I don’t think we need to flush, as things aren’t asynchronous on our end.

You could start with what I said above, the method you’d add would just contain the send_response(...) call.

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

Successfully merging a pull request may close this issue.

4 participants