-
Notifications
You must be signed in to change notification settings - Fork 949
Add @capture decorator to output widget #1934
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
Add @capture decorator to output widget #1934
Conversation
I like it 👍. I think capture is a good name. An alternative would be redirect, coming from redirecting stdout/stderr, but I like it less than capture. |
By using functools.wraps, we avoid overwriting the __repr__ and __doc__ of the wrapped callback.
Thanks!
I tend to agree. Jason isn't so keen. An alternative could be |
What about making it callable? @out
def f(arg):
print(arg) |
So replacing |
Another possible question is -- we could clear the current outputs at each new entrance to |
Indeed, I think the name
|
The @out.capture
def f():
"""hello"""
print('hi')
display(HTML('<p>hello</p>'))
raise Exception('bla')
f()
# =>
({'name': 'stdout', 'output_type': 'stream', 'text': 'hi\n'},
{'data': {'text/html': '<p>hello</p>',
'text/plain': '<IPython.core.display.HTML object>'},
'metadata': {},
'output_type': 'display_data'},
{'ename': 'Exception',
'evalue': 'bla',
'output_type': 'error',
'traceback': ['some long traceback']}) Given the number of output types, I'm not sure there'd be a strong use case for redirecting some outputs but not others. That behaviour would also be substantially different to the current context manager. |
I think this is ready for review and merging as is. |
What if we made it We could support both @output.capture.options(clear=True)
def f():
... |
I don't like the |
Once we move to python 3, it will be even cleaner since we can insist that clear=True is a keyword argument. |
Is it maybe a plan to put in comments in the code, that we can grep to make that easy? |
Thanks for the feedback -- I'll add a |
The decorator now supports By default, This is ready for another review. |
This is more consistent with the behaviour of the context manager, which does not clear the output every time it is entered.
Thanks! |
Great, thank you! |
Yeah, great work! |
This PR adds a
@out.capture
decorator that pushes all the output of a function to an output widget. It addresses issue #1846 .For instance,
This is useful for capturing output from widget callbacks:
To do:
- [x] Documentation on output widget (probably in a separate PR)(moved to issue #1935)functools.wraps
to avoid losing the function name and docstring (https://docs.python.org/3.6/library/functools.html#functools.wraps)- [ ] Is there a better name thanout.capture
? (I'm comfortable without.capture
).- [ ] Should current outputs be cleared on entry? (I think that would be more confusing -- we could add a recipe to the documentation for creating an output widget with a clear button).