##// END OF EJS Templates
update capture per review...
MinRK -
Show More
@@ -992,25 +992,31 b' python-profiler package from non-free.""")'
992 992 print macro,
993 993
994 994 @magic_arguments.magic_arguments()
995 @magic_arguments.argument('-o', '--out', type=str,
996 help="""The name of the variable in which to store stdout
995 @magic_arguments.argument('output', type=str, default='', nargs='?',
996 help="""The name of the variable in which to store output.
997 This is a utils.io.CapturedIO object with stdout/err attributes
998 for the text of the captured output.
997 999
998 If unspecified: stdout is discarded
999 """
1000 )
1001 @magic_arguments.argument('-e', '--err', type=str,
1002 help="""The name of the variable in which to store stderr
1000 CapturedOutput also has a show() method for displaying the output,
1001 and __call__ as well, so you can use that to quickly display the
1002 output.
1003 1003
1004 If unspecified: stderr is discarded
1004 If unspecified, captured output is discarded.
1005 1005 """
1006 1006 )
1007 @magic_arguments.argument('--no-stderr', action="store_true",
1008 help="""Don't capture stderr."""
1009 )
1010 @magic_arguments.argument('--no-stdout', action="store_true",
1011 help="""Don't capture stdout."""
1012 )
1007 1013 @cell_magic
1008 1014 def capture(self, line, cell):
1009 1015 """run the cell, capturing stdout/err"""
1010 1016 args = magic_arguments.parse_argstring(self.capture, line)
1011 with capture_output() as io:
1017 out = not args.no_stdout
1018 err = not args.no_stderr
1019 with capture_output(out, err) as io:
1012 1020 self.shell.run_cell(cell)
1013 if args.out:
1014 self.shell.user_ns[args.out] = io.stdout
1015 if args.err:
1016 self.shell.user_ns[args.err] = io.stderr
1021 if args.output:
1022 self.shell.user_ns[args.output] = io
@@ -328,26 +328,50 b' class CapturedIO(object):'
328 328 """Simple object for containing captured stdout/err StringIO objects"""
329 329
330 330 def __init__(self, stdout, stderr):
331 self.stdout_io = stdout
332 self.stderr_io = stderr
331 self._stdout = stdout
332 self._stderr = stderr
333 333
334 334 @property
335 335 def stdout(self):
336 return self.stdout_io.getvalue()
336 if not self._stdout:
337 return ''
338 return self._stdout.getvalue()
337 339
338 340 @property
339 341 def stderr(self):
340 return self.stderr_io.getvalue()
342 if not self._stderr:
343 return ''
344 return self._stderr.getvalue()
345
346 def show(self):
347 """write my output to sys.stdout/err as appropriate"""
348 sys.stdout.write(self.stdout)
349 sys.stderr.write(self.stderr)
350 sys.stdout.flush()
351 sys.stderr.flush()
352
353 __call__ = show
341 354
342 355
343 356 class capture_output(object):
344 357 """context manager for capturing stdout/err"""
358 stdout = True
359 stderr = True
360
361 def __init__(self, stdout=True, stderr=True):
362 self.stdout = stdout
363 self.stderr = stderr
345 364
346 365 def __enter__(self):
347 366 self.sys_stdout = sys.stdout
348 367 self.sys_stderr = sys.stderr
368
369 stdout = stderr = False
370 if self.stdout:
349 371 stdout = sys.stdout = StringIO()
372 if self.stderr:
350 373 stderr = sys.stderr = StringIO()
374
351 375 return CapturedIO(stdout, stderr)
352 376
353 377 def __exit__(self, exc_type, exc_value, traceback):
@@ -47,13 +47,13 b''
47 47 {
48 48 "cell_type": "markdown",
49 49 "source": [
50 "If you specify `-o` or `-e`, then stdout and/or stderr will be stored in those variables in your namespace."
50 "If you specify a name, then stdout and stderr will be stored in an object in your namespace."
51 51 ]
52 52 },
53 53 {
54 54 "cell_type": "code",
55 55 "input": [
56 "%%capture -o my_stdout",
56 "%%capture captured",
57 57 "print 'hi, stdout'",
58 58 "print >> sys.stderr, 'hi, stderr'"
59 59 ],
@@ -63,7 +63,21 b''
63 63 {
64 64 "cell_type": "code",
65 65 "input": [
66 "my_stdout"
66 "captured"
67 ],
68 "language": "python",
69 "outputs": []
70 },
71 {
72 "cell_type": "markdown",
73 "source": [
74 "Calling the object writes the output to stdout/err as appropriate."
75 ]
76 },
77 {
78 "cell_type": "code",
79 "input": [
80 "captured()"
67 81 ],
68 82 "language": "python",
69 83 "outputs": []
@@ -71,9 +85,7 b''
71 85 {
72 86 "cell_type": "code",
73 87 "input": [
74 "%%capture -o my_stdout2 -e my_stderr",
75 "print 'hi again, stdout'",
76 "print >> sys.stderr, 'hi there, stderr'"
88 "captured.stdout"
77 89 ],
78 90 "language": "python",
79 91 "outputs": []
@@ -81,8 +93,7 b''
81 93 {
82 94 "cell_type": "code",
83 95 "input": [
84 "sys.stdout.write(my_stdout2)",
85 "sys.stderr.write(my_stderr)"
96 "captured.stderr"
86 97 ],
87 98 "language": "python",
88 99 "outputs": []
@@ -104,7 +115,7 b''
104 115 {
105 116 "cell_type": "code",
106 117 "input": [
107 "%%capture -o wontshutup",
118 "%%capture wontshutup",
108 119 "",
109 120 "print \"setting up X\"",
110 121 "x = np.linspace(0,5,1000)",
@@ -120,7 +131,47 b''
120 131 {
121 132 "cell_type": "code",
122 133 "input": [
123 "print wontshutup"
134 "wontshutup()"
135 ],
136 "language": "python",
137 "outputs": []
138 },
139 {
140 "cell_type": "markdown",
141 "source": [
142 "And you can selectively disable capturing stdout or stderr by passing `--no-stdout/err`."
143 ]
144 },
145 {
146 "cell_type": "code",
147 "input": [
148 "%%capture cap --no-stderr",
149 "print 'hi, stdout'",
150 "print >> sys.stderr, \"hello, stderr\""
151 ],
152 "language": "python",
153 "outputs": []
154 },
155 {
156 "cell_type": "code",
157 "input": [
158 "cap.stdout"
159 ],
160 "language": "python",
161 "outputs": []
162 },
163 {
164 "cell_type": "code",
165 "input": [
166 "cap.stderr"
167 ],
168 "language": "python",
169 "outputs": []
170 },
171 {
172 "cell_type": "code",
173 "input": [
174 ""
124 175 ],
125 176 "language": "python",
126 177 "outputs": []
General Comments 0
You need to be logged in to leave comments. Login now