##// END OF EJS Templates
Merge pull request #2046 from jstenar/iptest-unicode...
Merge pull request #2046 from jstenar/iptest-unicode iptest unicode - fix space in path issue for iptest when using --with-xunit - fix unicode issue in path for iptest closes #760

File last commit:

r7408:34ddebb4
r7734:bb4488ab merge
Show More
Capturing Output.ipynb
181 lines | 3.5 KiB | text/plain | TextLexer
/ docs / examples / notebooks / Capturing Output.ipynb
MinRK
add demo notebook for %%capture
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": [
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",
"input": [
MinRK
update capture per review...
r7408 "%%capture captured",
MinRK
add demo notebook for %%capture
r7326 "print 'hi, stdout'",
"print >> sys.stderr, 'hi, stderr'"
],
"language": "python",
"outputs": []
},
{
"cell_type": "code",
"input": [
MinRK
update capture per review...
r7408 "captured"
],
"language": "python",
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Calling the object writes the output to stdout/err as appropriate."
]
},
{
"cell_type": "code",
"input": [
"captured()"
MinRK
add demo notebook for %%capture
r7326 ],
"language": "python",
"outputs": []
},
{
"cell_type": "code",
"input": [
MinRK
update capture per review...
r7408 "captured.stdout"
MinRK
add demo notebook for %%capture
r7326 ],
"language": "python",
"outputs": []
},
{
"cell_type": "code",
"input": [
MinRK
update capture per review...
r7408 "captured.stderr"
MinRK
add demo notebook for %%capture
r7326 ],
"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": [
MinRK
update capture per review...
r7408 "%%capture wontshutup",
MinRK
add demo notebook for %%capture
r7326 "",
"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": [
MinRK
update capture per review...
r7408 "wontshutup()"
],
"language": "python",
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"And you can selectively disable capturing stdout or stderr by passing `--no-stdout/err`."
]
},
{
"cell_type": "code",
"input": [
"%%capture cap --no-stderr",
"print 'hi, stdout'",
"print >> sys.stderr, \"hello, stderr\""
],
"language": "python",
"outputs": []
},
{
"cell_type": "code",
"input": [
"cap.stdout"
],
"language": "python",
"outputs": []
},
{
"cell_type": "code",
"input": [
"cap.stderr"
],
"language": "python",
"outputs": []
},
{
"cell_type": "code",
"input": [
""
MinRK
add demo notebook for %%capture
r7326 ],
"language": "python",
"outputs": []
}
]
}
]
}