Show More
@@ -0,0 +1,131 b'' | |||
|
1 | { | |
|
2 | "metadata": { | |
|
3 | "name": "Capturing Output" | |
|
4 | }, | |
|
5 | "nbformat": 3, | |
|
6 | "worksheets": [ | |
|
7 | { | |
|
8 | "cells": [ | |
|
9 | { | |
|
10 | "cell_type": "heading", | |
|
11 | "level": 1, | |
|
12 | "source": [ | |
|
13 | "Capturing Output with <tt>%%capture</tt>" | |
|
14 | ] | |
|
15 | }, | |
|
16 | { | |
|
17 | "cell_type": "markdown", | |
|
18 | "source": [ | |
|
19 | "One of IPython's new cell magics is `%%capture`, which captures stdout/err for a cell,", | |
|
20 | "and discards them or stores them in variables in your namespace." | |
|
21 | ] | |
|
22 | }, | |
|
23 | { | |
|
24 | "cell_type": "code", | |
|
25 | "input": [ | |
|
26 | "import sys" | |
|
27 | ], | |
|
28 | "language": "python", | |
|
29 | "outputs": [] | |
|
30 | }, | |
|
31 | { | |
|
32 | "cell_type": "markdown", | |
|
33 | "source": [ | |
|
34 | "By default, it just swallows it up. This is a simple way to suppress unwanted output." | |
|
35 | ] | |
|
36 | }, | |
|
37 | { | |
|
38 | "cell_type": "code", | |
|
39 | "input": [ | |
|
40 | "%%capture", | |
|
41 | "print 'hi, stdout'", | |
|
42 | "print >> sys.stderr, 'hi, stderr'" | |
|
43 | ], | |
|
44 | "language": "python", | |
|
45 | "outputs": [] | |
|
46 | }, | |
|
47 | { | |
|
48 | "cell_type": "markdown", | |
|
49 | "source": [ | |
|
50 | "If you specify `-o` or `-e`, then stdout and/or stderr will be stored in those variables in your namespace." | |
|
51 | ] | |
|
52 | }, | |
|
53 | { | |
|
54 | "cell_type": "code", | |
|
55 | "input": [ | |
|
56 | "%%capture -o my_stdout", | |
|
57 | "print 'hi, stdout'", | |
|
58 | "print >> sys.stderr, 'hi, stderr'" | |
|
59 | ], | |
|
60 | "language": "python", | |
|
61 | "outputs": [] | |
|
62 | }, | |
|
63 | { | |
|
64 | "cell_type": "code", | |
|
65 | "input": [ | |
|
66 | "my_stdout" | |
|
67 | ], | |
|
68 | "language": "python", | |
|
69 | "outputs": [] | |
|
70 | }, | |
|
71 | { | |
|
72 | "cell_type": "code", | |
|
73 | "input": [ | |
|
74 | "%%capture -o my_stdout2 -e my_stderr", | |
|
75 | "print 'hi again, stdout'", | |
|
76 | "print >> sys.stderr, 'hi there, stderr'" | |
|
77 | ], | |
|
78 | "language": "python", | |
|
79 | "outputs": [] | |
|
80 | }, | |
|
81 | { | |
|
82 | "cell_type": "code", | |
|
83 | "input": [ | |
|
84 | "sys.stdout.write(my_stdout2)", | |
|
85 | "sys.stderr.write(my_stderr)" | |
|
86 | ], | |
|
87 | "language": "python", | |
|
88 | "outputs": [] | |
|
89 | }, | |
|
90 | { | |
|
91 | "cell_type": "markdown", | |
|
92 | "source": [ | |
|
93 | "`%%capture` only captures stdout/err, not displaypub, so you can still do plots and use the display protocol inside %%capture" | |
|
94 | ] | |
|
95 | }, | |
|
96 | { | |
|
97 | "cell_type": "code", | |
|
98 | "input": [ | |
|
99 | "%pylab inline" | |
|
100 | ], | |
|
101 | "language": "python", | |
|
102 | "outputs": [] | |
|
103 | }, | |
|
104 | { | |
|
105 | "cell_type": "code", | |
|
106 | "input": [ | |
|
107 | "%%capture -o wontshutup", | |
|
108 | "", | |
|
109 | "print \"setting up X\"", | |
|
110 | "x = np.linspace(0,5,1000)", | |
|
111 | "print \"step 2: constructing y-data\"", | |
|
112 | "y = np.sin(x)", | |
|
113 | "print \"step 3: display info about y\"", | |
|
114 | "plt.plot(x,y)", | |
|
115 | "print \"okay, I'm done now\"" | |
|
116 | ], | |
|
117 | "language": "python", | |
|
118 | "outputs": [] | |
|
119 | }, | |
|
120 | { | |
|
121 | "cell_type": "code", | |
|
122 | "input": [ | |
|
123 | "print wontshutup" | |
|
124 | ], | |
|
125 | "language": "python", | |
|
126 | "outputs": [] | |
|
127 | } | |
|
128 | ] | |
|
129 | } | |
|
130 | ] | |
|
131 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now