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

How to append to the output of a cell? #389

Open
edgarcosta opened this issue Mar 19, 2024 · 3 comments
Open

How to append to the output of a cell? #389

edgarcosta opened this issue Mar 19, 2024 · 3 comments

Comments

@edgarcosta
Copy link

Hello,

I'm used to using Ipykernel, where when one is running a lengthy computation, one can use
send_response to append to the current output, for example

What is the right way to do this with Xeus?

@JohanMabille
Copy link
Member

Hi,

You can call the publish_stream method of the interpreter.

@edgarcosta
Copy link
Author

Thank you very much. We tried publish_stream, and we got to some more questions:

In Xeus, there are two functions to output results:

  • publish_execution_result adds Out[]: and adds a new line at the end.
  • publish_stream() does not add a new line and does not add Out[]: either.

What should we use if we want to display the results progressively as they are computed?

Then the issue is with something like this in python

from time import sleep
import sys
for i in range(5):
    sleep(1)
    sys.stdout.write(f"{i} ")

which should display

Out[1]: 1

updated one second later to

Out[1]: 1 2

updated one second later to

Out[1]: 1 2 3

In case you are interested, here is the kernel we are developing:
https://pari.math.u-bordeaux.fr/cgi-bin/gitweb.cgi?p=xeus-gp.git;a=blob;f=src/xinterpreter.cpp;h=7e4517ec9f4eb7e5b4d6487fb83c26ef0064d090;hb=refs/heads/bill-2.17

@JohanMabille
Copy link
Member

Hi, sorry for the late reply. I'm not 100% sure how the frontend handles this, but I would try the folowing:

  • either publish_execution_result for sending the first output, and the publish_stream to update; this assumes the frontend appends the outputs to the current one.
  • or maintain a string in the kernel, update it by appending a new result when it become available and immediately send it with publish_execution_result. This assumes that the frontend replaces the current output with the new one.

Thanks for the pointer!

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