我们可以创建一个自定义魔法register_cell_magic
, 基于capture https://github.com/ipython/ipython/blob/35331a37b2cf29e779d937bffee8606aa2418bc7/IPython/core/magics/execution.py#L1454来自 IPython 的代码,我可以执行这个模拟 Linux 的函数tee
命令(因此得名)。我做的唯一一件事就是从类中提取它并调用io
事情在最后。
from IPython import get_ipython
from IPython.core import magic_arguments
from IPython.core.magic import register_cell_magic
from IPython.utils.capture import capture_output
@magic_arguments.magic_arguments()
@magic_arguments.argument('output', type=str, default='', nargs='?',
help="""The name of the variable in which to store output.
This is a utils.io.CapturedIO object with stdout/err attributes
for the text of the captured output.
CapturedOutput also has a show() method for displaying the output,
and __call__ as well, so you can use that to quickly display the
output.
If unspecified, captured output is discarded.
"""
)
@magic_arguments.argument('--no-stderr', action="store_true",
help="""Don't capture stderr."""
)
@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."""
)
@register_cell_magic
def tee(line, cell):
args = magic_arguments.parse_argstring(tee, line)
out = not args.no_stdout
err = not args.no_stderr
disp = not args.no_display
with capture_output(out, err, disp) as io:
get_ipython().run_cell(cell)
if args.output:
get_ipython().user_ns[args.output] = io
io()
As you can see in the following image, It is working correctly