##// END OF EJS Templates
use print function in module with `print >>`
use print function in module with `print >>`

File last commit:

r7739:dff285da
r7817:89cac280
Show More
Capturing Output.ipynb
208 lines | 4.2 KiB | text/plain | TextLexer
/ docs / examples / notebooks / Capturing Output.ipynb
MinRK
add demo notebook for %%capture
r7326 {
"metadata": {
"name": "Capturing Output"
},
"nbformat": 3,
MinRK
rebuild example notebooks...
r7739 "nbformat_minor": 0,
MinRK
add demo notebook for %%capture
r7326 "worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "source": [
"Capturing Output with <tt>%%capture</tt>"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "source": [
MinRK
rebuild example notebooks...
r7739 "One of IPython's new cell magics is `%%capture`, which captures stdout/err for a cell,\n",
MinRK
add demo notebook for %%capture
r7326 "and discards them or stores them in variables in your namespace."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add demo notebook for %%capture
r7326 "input": [
"import sys"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "outputs": []
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "source": [
"By default, it just swallows it up. This is a simple way to suppress unwanted output."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add demo notebook for %%capture
r7326 "input": [
MinRK
rebuild example notebooks...
r7739 "%%capture\n",
"print 'hi, stdout'\n",
MinRK
add demo notebook for %%capture
r7326 "print >> sys.stderr, 'hi, stderr'"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "outputs": []
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "source": [
MinRK
update capture per review...
r7408 "If you specify a name, then stdout and stderr will be stored in an object in your namespace."
MinRK
add demo notebook for %%capture
r7326 ]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add demo notebook for %%capture
r7326 "input": [
MinRK
rebuild example notebooks...
r7739 "%%capture captured\n",
"print 'hi, stdout'\n",
MinRK
add demo notebook for %%capture
r7326 "print >> sys.stderr, 'hi, stderr'"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add demo notebook for %%capture
r7326 "input": [
MinRK
update capture per review...
r7408 "captured"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update capture per review...
r7408 "outputs": []
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update capture per review...
r7408 "source": [
"Calling the object writes the output to stdout/err as appropriate."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
update capture per review...
r7408 "input": [
"captured()"
MinRK
add demo notebook for %%capture
r7326 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add demo notebook for %%capture
r7326 "input": [
MinRK
update capture per review...
r7408 "captured.stdout"
MinRK
add demo notebook for %%capture
r7326 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add demo notebook for %%capture
r7326 "input": [
MinRK
update capture per review...
r7408 "captured.stderr"
MinRK
add demo notebook for %%capture
r7326 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "outputs": []
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
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
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add demo notebook for %%capture
r7326 "input": [
"%pylab inline"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add demo notebook for %%capture
r7326 "input": [
MinRK
rebuild example notebooks...
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
add demo notebook for %%capture
r7326 "print \"okay, I'm done now\""
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add demo notebook for %%capture
r7326 "input": [
MinRK
update capture per review...
r7408 "wontshutup()"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update capture per review...
r7408 "outputs": []
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update capture per review...
r7408 "source": [
"And you can selectively disable capturing stdout or stderr by passing `--no-stdout/err`."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
update capture per review...
r7408 "input": [
MinRK
rebuild example notebooks...
r7739 "%%capture cap --no-stderr\n",
"print 'hi, stdout'\n",
MinRK
update capture per review...
r7408 "print >> sys.stderr, \"hello, stderr\""
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update capture per review...
r7408 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
update capture per review...
r7408 "input": [
"cap.stdout"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
update capture per review...
r7408 "outputs": []
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
update capture per review...
r7408 "input": [
"cap.stderr"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add demo notebook for %%capture
r7326 "outputs": []
}
MinRK
rebuild example notebooks...
r7739 ],
"metadata": {}
MinRK
add demo notebook for %%capture
r7326 }
]
}