From 53fede2af1b6ec4d8ef61c5be69ee2863c86a5e7 2013-08-17 02:39:08 From: Paul Ivanov Date: 2013-08-17 02:39:08 Subject: [PATCH] add --no-display flag to %%capture document this new magic in whatsnew, and added docs to the resulting object. --- diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index 4e6d9ae..e01c978 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -1166,13 +1166,17 @@ python-profiler package from non-free.""") @magic_arguments.argument('--no-stdout', action="store_true", help="""Don't capture stdout.""" ) + @magic_arguments.argument('--no-display', action="store_true", + help="""Don't capture IPython's rich display.""" + ) @cell_magic def capture(self, line, cell): - """run the cell, capturing stdout/err""" + """run the cell, capturing stdout, stderr, and IPython's rich display() calls.""" args = magic_arguments.parse_argstring(self.capture, line) out = not args.no_stdout err = not args.no_stderr - with capture_output(out, err) as io: + disp = not args.no_display + with capture_output(out, err, disp) as io: self.shell.run_cell(cell) if args.output: self.shell.user_ns[args.output] = io diff --git a/IPython/utils/capture.py b/IPython/utils/capture.py index 2733257..9db05ca 100644 --- a/IPython/utils/capture.py +++ b/IPython/utils/capture.py @@ -65,7 +65,17 @@ class RichOutput(object): class CapturedIO(object): - """Simple object for containing captured stdout/err StringIO objects""" + """Simple object for containing captured stdout/err and rich display StringIO objects + + Each instance `c` has three attributes: + + c.stdout : standard output as a string + c.stderr : standard error as a string + c.outputs: a list of rich display outputs + + Additionally, there's a `c.show()` method which will print all of the + above in the same order, and can be invoked simply via `c()`. + """ def __init__(self, stdout, stderr, outputs=None): self._stdout = stdout @@ -130,7 +140,7 @@ class capture_output(object): self.save_display_pub = None self.display = False - stdout = stderr = outputs = False + stdout = stderr = outputs = None if self.stdout: stdout = sys.stdout = StringIO() if self.stderr: diff --git a/docs/source/whatsnew/development.rst b/docs/source/whatsnew/development.rst index cf00639..459f2d7 100644 --- a/docs/source/whatsnew/development.rst +++ b/docs/source/whatsnew/development.rst @@ -4,6 +4,9 @@ This document describes in-flight development work. +- `%%capture` cell magic now captures the rich display output, not just + stdout/stderr + Backwards incompatible changes ------------------------------