{ "metadata": { "name": "Capturing Output" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Capturing Output with %%capture" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of IPython's new cell magics is `%%capture`, which captures stdout/err for a cell,\n", "and discards them or stores them in variables in your namespace." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, it just swallows it up. This is a simple way to suppress unwanted output." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%capture\n", "print 'hi, stdout'\n", "print >> sys.stderr, 'hi, stderr'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you specify a name, then stdout and stderr will be stored in an object in your namespace." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%capture captured\n", "print 'hi, stdout'\n", "print >> sys.stderr, 'hi, stderr'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "captured" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calling the object writes the output to stdout/err as appropriate." ] }, { "cell_type": "code", "collapsed": false, "input": [ "captured()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "captured.stdout" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "captured.stderr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`%%capture` only captures stdout/err, not displaypub, so you can still do plots and use the display protocol inside %%capture" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%%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", "print \"okay, I'm done now\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "wontshutup()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And you can selectively disable capturing stdout or stderr by passing `--no-stdout/err`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%capture cap --no-stderr\n", "print 'hi, stdout'\n", "print >> sys.stderr, \"hello, stderr\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "cap.stdout" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "cap.stderr" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }