Giter VIP home page Giter VIP logo

Comments (14)

jasongrout avatar jasongrout commented on May 24, 2024

Also, there are issues with threading. Since the ZMQDisplayPublisher is a global object, installing a message filter there naively will affect messages from everywhere. In the case of a context manager, it seems that the "correct" behavior is to only have the output from the block of code in the current thread. @minrk suggested making the hooks thread local attributes in the ZMQDisplayPublisher object (see https://docs.python.org/2/library/threading.html#threading.local), maybe something like

from threading import local
class ZMQDisplayPublisher(...):
    def __init__(...):
        self.threadlocal = local()

    def register_hook(self, hook):
        if not hasattr(self.threadlocal, 'hooks'):
            self.threadlocal.hooks = []
        self.threadlocal.hooks.append(hook)

from ipykernel.

jasongrout avatar jasongrout commented on May 24, 2024

Code to touch for making a display hook registration system includes the following.

@minrk, can you verify this is right?

from ipykernel.

jasongrout avatar jasongrout commented on May 24, 2024

Interestingly, since clear_output is implemented by just sending a clear_output message to the front end, this works too:

with output_widget.capture_messages():
    clear_output()

from ipykernel.

minrk avatar minrk commented on May 24, 2024

Yup, that should be right. Also to clarify, implementing this hook should need no changes in any other repos, but the intention is to use it in a new implementation of the OutputWidget downstream.

from ipykernel.

jasongrout avatar jasongrout commented on May 24, 2024

@dwillmer, @minrk: next steps:

We can get a handle on the display publisher with get_ipython().display_pub

  • Make a context manager that will register a hook and store a list of display outputs. You may need to flush something when exiting the context manager to make sure you get all of the display messages.
  • Make a widget that has such a context manager. It should take over any display_pub messages and store the messages in a list as its synced widget state. A clear_output message should clear the message queue (and probably doesn't need to be sent, since the empty message queue will be synced).
  • Make a front end widget that will render the display pub message queue (this will likely involve using a rendermime instance that is passed in...)

@minrk - can you remind us what needs to be done for capturing stdout/stderr?

from ipykernel.

dwillmer avatar dwillmer commented on May 24, 2024

message-blah3

@jasongrout - is this ^^ what you're expecting?

The print calls go to the normal output area, but any display_data messages within the new context manager are sent to the custom widget area.

Clearly we'll change the name from MessageWidget ;) just wanted some early feedback!

from ipykernel.

jasongrout avatar jasongrout commented on May 24, 2024

Yep, that's the idea. Thanks!

Things to still do:

  • hook into the stdout/stderr (we also want to capture stdout). We have to write these extension points as well.
  • make sure a clear_output() message clears the messages

And when I finish the widgets in the new notebook, actually render the messages sent to the message widget.

from ipykernel.

jasongrout avatar jasongrout commented on May 24, 2024

Just checking, the hook unregisters itself when the context manager exits, right? (that's not illustrated in your screenshot, but it should work that way)

from ipykernel.

dwillmer avatar dwillmer commented on May 24, 2024

yep, it does, i'll post an updated screenshot in a bit.

from ipykernel.

minrk avatar minrk commented on May 24, 2024

For stdout/err, the simple thing is to swap out sys.stdout/err to an io.StringIO object, but that won't have the same threadlocal behavior the hooks now have. To preserve that, some amount of threadlocal things will need to be added to the IOStream code, so that writes go to the short-circuit buffer rather than the default.

from ipykernel.

jasongrout avatar jasongrout commented on May 24, 2024

@dwillmer - no need to post a new screenshot for the exit status. I trust it's there.

from ipykernel.

dwillmer avatar dwillmer commented on May 24, 2024

widget-hook

Update:

  • Using with widget.capture(): instead of 'with widget:`.
  • capture() takes an optional positional arg for the message type, so you can intercept different messages in each context.
  • Moved context manager to ipykernel, left widget code in ipywidgets

from ipykernel.

jasongrout avatar jasongrout commented on May 24, 2024

I think this issue is solved with #115. Let's document the widget work on an issue in the ipywidgets repo. @dwillmer, can you open a work-in-progress PR over there?

from ipykernel.

jasongrout avatar jasongrout commented on May 24, 2024

@minrk or @dwillmer, if this issue is solved by #115, can you close this? Apparently I don't have permissions to close issues on this repo.

from ipykernel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.