Capturing Output.ipynb
130 lines
| 2.7 KiB
| text/plain
|
TextLexer
MinRK
|
r7326 | { | |
"metadata": { | |||
"name": "Capturing Output" | |||
}, | |||
"nbformat": 3, | |||
"worksheets": [ | |||
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "heading", | |||
"level": 1, | |||
"source": [ | |||
"Capturing Output with <tt>%%capture</tt>" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"source": [ | |||
"One of IPython's new cell magics is `%%capture`, which captures stdout/err for a cell,", | |||
"and discards them or stores them in variables in your namespace." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"input": [ | |||
"import sys" | |||
], | |||
"language": "python", | |||
"outputs": [] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"source": [ | |||
"By default, it just swallows it up. This is a simple way to suppress unwanted output." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"input": [ | |||
"%%capture", | |||
"print 'hi, stdout'", | |||
"print >> sys.stderr, 'hi, stderr'" | |||
], | |||
"language": "python", | |||
"outputs": [] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"source": [ | |||
"If you specify `-o` or `-e`, then stdout and/or stderr will be stored in those variables in your namespace." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"input": [ | |||
"%%capture -o my_stdout", | |||
"print 'hi, stdout'", | |||
"print >> sys.stderr, 'hi, stderr'" | |||
], | |||
"language": "python", | |||
"outputs": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"input": [ | |||
"my_stdout" | |||
], | |||
"language": "python", | |||
"outputs": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"input": [ | |||
"%%capture -o my_stdout2 -e my_stderr", | |||
"print 'hi again, stdout'", | |||
"print >> sys.stderr, 'hi there, stderr'" | |||
], | |||
"language": "python", | |||
"outputs": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"input": [ | |||
"sys.stdout.write(my_stdout2)", | |||
"sys.stderr.write(my_stderr)" | |||
], | |||
"language": "python", | |||
"outputs": [] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
"source": [ | |||
"`%%capture` only captures stdout/err, not displaypub, so you can still do plots and use the display protocol inside %%capture" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"input": [ | |||
"%pylab inline" | |||
], | |||
"language": "python", | |||
"outputs": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"input": [ | |||
"%%capture -o wontshutup", | |||
"", | |||
"print \"setting up X\"", | |||
"x = np.linspace(0,5,1000)", | |||
"print \"step 2: constructing y-data\"", | |||
"y = np.sin(x)", | |||
"print \"step 3: display info about y\"", | |||
"plt.plot(x,y)", | |||
"print \"okay, I'm done now\"" | |||
], | |||
"language": "python", | |||
"outputs": [] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
"input": [ | |||
"print wontshutup" | |||
], | |||
"language": "python", | |||
"outputs": [] | |||
} | |||
] | |||
} | |||
] | |||
} |