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

Fix print outputs in Jupyter notebook #4324

Merged
merged 1 commit into from
Aug 18, 2023

Conversation

jnumainville
Copy link
Contributor

Fixes #4277

Essentially, the parent needs to be updated so that Jupyter's OutputStream can redirect the output properly
More info here: StanfordLegion/legion#1393

Tested with code at #4277
Initial run:
Screenshot 2023-08-15 at 11 07 37 AM
rerun:
Screenshot 2023-08-15 at 11 08 02 AM


def set_parent(self, parent):
# Parent needs to be set on the original stream so that output shows in proper cell
self._stream.set_parent(parent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a glance, I'm not seeing set_parent defined in https://docs.python.org/3/library/io.html#io.TextIOBase or in the io packages for cpython - what is the definition for this, should we explicitly not call super (like the other overrides we provide)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something defined by Jupyter's OutStream
https://github.com/ipython/ipykernel/blob/main/ipykernel/iostream.py#L508
The sys.stdout that is pulled at the time that the TeeStream is created is a Jupyter OutStream, so that's where the issue is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't read deeply enough through the linked code, see the definition in https://github.com/ipython/ipykernel/blob/6bf3fe9e44f1caf4bc371f700ac0c2e9c9c3bd84/ipykernel/iostream.py#L508-L510

I'm wondering if there are circumstances where we don't want this defined, for example to make sure we don't get something else calling set_parent on us if the stream that we wrap itself doesn't define set_parent. I'm not sure what that case would be, but some kind of intermediate "we've replaced sys.stdout too early for jupyter to have replaced it before us" situation?

I do notice that the containing type, OutStream, doesn't have any other supertypes to test if it is an instance, and the linked issue reports that jupyter is "graceful" in its handling if there is no set_parent method defined. Which isn't very graceful (since it leads to this issue) and seems like it could silently fail in other ways if we miss some other poorly documented bit of expected magic...


Backing up for a minute, the purpose of defining our own TeeStream type to begin with is so that a running server can make the full stdout available to any connecting client. @chipkent do we think that's a reasonable use case here, or if something else is mucking with stdout, should the creation of a deephaven_server.Server instead have a flag to be hands-off, and assume that jupyter will handle all the logs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that having the logs everywhere is the least confusing option for a user. I think we leave it like this until we have a compelling reason to make a change.


def set_parent(self, parent):
# Parent needs to be set on the original stream so that output shows in proper cell
self._stream.set_parent(parent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that having the logs everywhere is the least confusing option for a user. I think we leave it like this until we have a compelling reason to make a change.

@jnumainville jnumainville merged commit 7e709b1 into deephaven:main Aug 18, 2023
11 of 20 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Aug 18, 2023
@jnumainville
Copy link
Contributor Author

@chipkent merged, thanks

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

Successfully merging this pull request may close these issues.

Python Server does not reliably print output to Jupyter
3 participants