Capturing Output.ipynb
208 lines
| 4.2 KiB
| text/plain
|
TextLexer
MinRK
|
r7326 | { | |
"metadata": { | |||
"name": "Capturing Output" | |||
}, | |||
"nbformat": 3, | |||
MinRK
|
r7739 | "nbformat_minor": 0, | |
MinRK
|
r7326 | "worksheets": [ | |
{ | |||
"cells": [ | |||
{ | |||
"cell_type": "heading", | |||
"level": 1, | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "source": [ | |
"Capturing Output with <tt>%%capture</tt>" | |||
] | |||
}, | |||
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "source": [ | |
MinRK
|
r7739 | "One of IPython's new cell magics is `%%capture`, which captures stdout/err for a cell,\n", | |
MinRK
|
r7326 | "and discards them or stores them in variables in your namespace." | |
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7326 | "input": [ | |
"import sys" | |||
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "source": [ | |
"By default, it just swallows it up. This is a simple way to suppress unwanted output." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7326 | "input": [ | |
MinRK
|
r7739 | "%%capture\n", | |
"print 'hi, stdout'\n", | |||
MinRK
|
r7326 | "print >> sys.stderr, 'hi, stderr'" | |
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "source": [ | |
MinRK
|
r7408 | "If you specify a name, then stdout and stderr will be stored in an object in your namespace." | |
MinRK
|
r7326 | ] | |
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7326 | "input": [ | |
MinRK
|
r7739 | "%%capture captured\n", | |
"print 'hi, stdout'\n", | |||
MinRK
|
r7326 | "print >> sys.stderr, 'hi, stderr'" | |
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7326 | "input": [ | |
MinRK
|
r7408 | "captured" | |
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7408 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7408 | "source": [ | |
"Calling the object writes the output to stdout/err as appropriate." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7408 | "input": [ | |
"captured()" | |||
MinRK
|
r7326 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7326 | "input": [ | |
MinRK
|
r7408 | "captured.stdout" | |
MinRK
|
r7326 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7326 | "input": [ | |
MinRK
|
r7408 | "captured.stderr" | |
MinRK
|
r7326 | ], | |
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "source": [ | |
"`%%capture` only captures stdout/err, not displaypub, so you can still do plots and use the display protocol inside %%capture" | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7326 | "input": [ | |
"%pylab inline" | |||
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7326 | "input": [ | |
MinRK
|
r7739 | "%%capture wontshutup\n", | |
"\n", | |||
"print \"setting up X\"\n", | |||
"x = np.linspace(0,5,1000)\n", | |||
"print \"step 2: constructing y-data\"\n", | |||
"y = np.sin(x)\n", | |||
"print \"step 3: display info about y\"\n", | |||
"plt.plot(x,y)\n", | |||
MinRK
|
r7326 | "print \"okay, I'm done now\"" | |
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7326 | "input": [ | |
MinRK
|
r7408 | "wontshutup()" | |
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7408 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "markdown", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7408 | "source": [ | |
"And you can selectively disable capturing stdout or stderr by passing `--no-stdout/err`." | |||
] | |||
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7408 | "input": [ | |
MinRK
|
r7739 | "%%capture cap --no-stderr\n", | |
"print 'hi, stdout'\n", | |||
MinRK
|
r7408 | "print >> sys.stderr, \"hello, stderr\"" | |
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7408 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7408 | "input": [ | |
"cap.stdout" | |||
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7408 | "outputs": [] | |
}, | |||
{ | |||
"cell_type": "code", | |||
MinRK
|
r7739 | "collapsed": false, | |
MinRK
|
r7408 | "input": [ | |
"cap.stderr" | |||
], | |||
"language": "python", | |||
MinRK
|
r7739 | "metadata": {}, | |
MinRK
|
r7326 | "outputs": [] | |
} | |||
MinRK
|
r7739 | ], | |
"metadata": {} | |||
MinRK
|
r7326 | } | |
] | |||
} |