Show More
@@ -0,0 +1,448 b'' | |||||
|
1 | { | |||
|
2 | "metadata": { | |||
|
3 | "name": "Script Magics" | |||
|
4 | }, | |||
|
5 | "nbformat": 3, | |||
|
6 | "worksheets": [ | |||
|
7 | { | |||
|
8 | "cells": [ | |||
|
9 | { | |||
|
10 | "cell_type": "heading", | |||
|
11 | "level": 1, | |||
|
12 | "source": [ | |||
|
13 | "Running Scripts from IPython" | |||
|
14 | ] | |||
|
15 | }, | |||
|
16 | { | |||
|
17 | "cell_type": "markdown", | |||
|
18 | "source": [ | |||
|
19 | "IPython has a `%%script` cell magic, which lets you run a cell in", | |||
|
20 | "a subprocess of any interpreter on your system, such as: bash, ruby, perl, zsh, R, etc.", | |||
|
21 | "", | |||
|
22 | "It can even be a script of your own, which expects input on stdin." | |||
|
23 | ] | |||
|
24 | }, | |||
|
25 | { | |||
|
26 | "cell_type": "code", | |||
|
27 | "input": [ | |||
|
28 | "import sys" | |||
|
29 | ], | |||
|
30 | "language": "python", | |||
|
31 | "outputs": [], | |||
|
32 | "prompt_number": 1 | |||
|
33 | }, | |||
|
34 | { | |||
|
35 | "cell_type": "markdown", | |||
|
36 | "source": [ | |||
|
37 | "To use it, simply pass a path or shell command to the program you want to run on the `%%script` line,", | |||
|
38 | "and the rest of the cell will be run by that script, and stdout/err from the subprocess are captured and displayed." | |||
|
39 | ] | |||
|
40 | }, | |||
|
41 | { | |||
|
42 | "cell_type": "code", | |||
|
43 | "input": [ | |||
|
44 | "%%script python", | |||
|
45 | "import sys", | |||
|
46 | "print 'hello from Python %s' % sys.version" | |||
|
47 | ], | |||
|
48 | "language": "python", | |||
|
49 | "outputs": [ | |||
|
50 | { | |||
|
51 | "output_type": "stream", | |||
|
52 | "stream": "stdout", | |||
|
53 | "text": [ | |||
|
54 | "hello from Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) ", | |||
|
55 | "[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]", | |||
|
56 | "" | |||
|
57 | ] | |||
|
58 | } | |||
|
59 | ], | |||
|
60 | "prompt_number": 2 | |||
|
61 | }, | |||
|
62 | { | |||
|
63 | "cell_type": "code", | |||
|
64 | "input": [ | |||
|
65 | "%%script python3", | |||
|
66 | "import sys", | |||
|
67 | "print('hello from Python: %s' % sys.version)" | |||
|
68 | ], | |||
|
69 | "language": "python", | |||
|
70 | "outputs": [ | |||
|
71 | { | |||
|
72 | "output_type": "stream", | |||
|
73 | "stream": "stdout", | |||
|
74 | "text": [ | |||
|
75 | "hello from Python: 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) ", | |||
|
76 | "[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]", | |||
|
77 | "" | |||
|
78 | ] | |||
|
79 | } | |||
|
80 | ], | |||
|
81 | "prompt_number": 3 | |||
|
82 | }, | |||
|
83 | { | |||
|
84 | "cell_type": "markdown", | |||
|
85 | "source": [ | |||
|
86 | "IPython also creates aliases for a few common interpreters, such as bash, ruby, perl, etc.", | |||
|
87 | "", | |||
|
88 | "These are all equivalent to `%%script <name>`" | |||
|
89 | ] | |||
|
90 | }, | |||
|
91 | { | |||
|
92 | "cell_type": "code", | |||
|
93 | "input": [ | |||
|
94 | "%%ruby", | |||
|
95 | "puts \"Hello from Ruby #{RUBY_VERSION}\"" | |||
|
96 | ], | |||
|
97 | "language": "python", | |||
|
98 | "outputs": [ | |||
|
99 | { | |||
|
100 | "output_type": "stream", | |||
|
101 | "stream": "stdout", | |||
|
102 | "text": [ | |||
|
103 | "Hello from Ruby 1.8.7", | |||
|
104 | "" | |||
|
105 | ] | |||
|
106 | } | |||
|
107 | ], | |||
|
108 | "prompt_number": 4 | |||
|
109 | }, | |||
|
110 | { | |||
|
111 | "cell_type": "code", | |||
|
112 | "input": [ | |||
|
113 | "%%bash", | |||
|
114 | "echo \"hello from $BASH\"" | |||
|
115 | ], | |||
|
116 | "language": "python", | |||
|
117 | "outputs": [ | |||
|
118 | { | |||
|
119 | "output_type": "stream", | |||
|
120 | "stream": "stdout", | |||
|
121 | "text": [ | |||
|
122 | "hello from /usr/local/bin/bash", | |||
|
123 | "" | |||
|
124 | ] | |||
|
125 | } | |||
|
126 | ], | |||
|
127 | "prompt_number": 5 | |||
|
128 | }, | |||
|
129 | { | |||
|
130 | "cell_type": "heading", | |||
|
131 | "level": 2, | |||
|
132 | "source": [ | |||
|
133 | "Capturing output" | |||
|
134 | ] | |||
|
135 | }, | |||
|
136 | { | |||
|
137 | "cell_type": "markdown", | |||
|
138 | "source": [ | |||
|
139 | "You can also capture stdout/err from these subprocesses into Python variables, instead of letting them go directly to stdout/err" | |||
|
140 | ] | |||
|
141 | }, | |||
|
142 | { | |||
|
143 | "cell_type": "code", | |||
|
144 | "input": [ | |||
|
145 | "%%bash", | |||
|
146 | "echo \"hi, stdout\"", | |||
|
147 | "echo \"hello, stderr\" >&2", | |||
|
148 | "" | |||
|
149 | ], | |||
|
150 | "language": "python", | |||
|
151 | "outputs": [ | |||
|
152 | { | |||
|
153 | "output_type": "stream", | |||
|
154 | "stream": "stdout", | |||
|
155 | "text": [ | |||
|
156 | "hi, stdout", | |||
|
157 | "" | |||
|
158 | ] | |||
|
159 | }, | |||
|
160 | { | |||
|
161 | "output_type": "stream", | |||
|
162 | "stream": "stderr", | |||
|
163 | "text": [ | |||
|
164 | "hello, stderr", | |||
|
165 | "" | |||
|
166 | ] | |||
|
167 | } | |||
|
168 | ], | |||
|
169 | "prompt_number": 6 | |||
|
170 | }, | |||
|
171 | { | |||
|
172 | "cell_type": "code", | |||
|
173 | "input": [ | |||
|
174 | "%%bash --out output --err error", | |||
|
175 | "echo \"hi, stdout\"", | |||
|
176 | "echo \"hello, stderr\" >&2" | |||
|
177 | ], | |||
|
178 | "language": "python", | |||
|
179 | "outputs": [], | |||
|
180 | "prompt_number": 7 | |||
|
181 | }, | |||
|
182 | { | |||
|
183 | "cell_type": "code", | |||
|
184 | "input": [ | |||
|
185 | "print error", | |||
|
186 | "print output" | |||
|
187 | ], | |||
|
188 | "language": "python", | |||
|
189 | "outputs": [ | |||
|
190 | { | |||
|
191 | "output_type": "stream", | |||
|
192 | "stream": "stdout", | |||
|
193 | "text": [ | |||
|
194 | "hello, stderr", | |||
|
195 | "", | |||
|
196 | "hi, stdout", | |||
|
197 | "", | |||
|
198 | "" | |||
|
199 | ] | |||
|
200 | } | |||
|
201 | ], | |||
|
202 | "prompt_number": 8 | |||
|
203 | }, | |||
|
204 | { | |||
|
205 | "cell_type": "heading", | |||
|
206 | "level": 2, | |||
|
207 | "source": [ | |||
|
208 | "Background Scripts" | |||
|
209 | ] | |||
|
210 | }, | |||
|
211 | { | |||
|
212 | "cell_type": "markdown", | |||
|
213 | "source": [ | |||
|
214 | "These scripts can be run in the background, by adding the `--bg` flag.", | |||
|
215 | "", | |||
|
216 | "When you do this, output is discarded unless you use the `--out/err`", | |||
|
217 | "flags to store output as above." | |||
|
218 | ] | |||
|
219 | }, | |||
|
220 | { | |||
|
221 | "cell_type": "code", | |||
|
222 | "input": [ | |||
|
223 | "%%ruby --bg --out ruby_lines", | |||
|
224 | "for n in 1...10", | |||
|
225 | " sleep 1", | |||
|
226 | " puts \"line #{n}\"", | |||
|
227 | " STDOUT.flush", | |||
|
228 | "end" | |||
|
229 | ], | |||
|
230 | "language": "python", | |||
|
231 | "outputs": [ | |||
|
232 | { | |||
|
233 | "output_type": "stream", | |||
|
234 | "stream": "stdout", | |||
|
235 | "text": [ | |||
|
236 | "Starting job # 0 in a separate thread.", | |||
|
237 | "" | |||
|
238 | ] | |||
|
239 | } | |||
|
240 | ], | |||
|
241 | "prompt_number": 9 | |||
|
242 | }, | |||
|
243 | { | |||
|
244 | "cell_type": "markdown", | |||
|
245 | "source": [ | |||
|
246 | "When you do store output of a background thread, these are the stdout/err *pipes*,", | |||
|
247 | "rather than the text of the output." | |||
|
248 | ] | |||
|
249 | }, | |||
|
250 | { | |||
|
251 | "cell_type": "code", | |||
|
252 | "input": [ | |||
|
253 | "ruby_lines" | |||
|
254 | ], | |||
|
255 | "language": "python", | |||
|
256 | "outputs": [ | |||
|
257 | { | |||
|
258 | "output_type": "pyout", | |||
|
259 | "prompt_number": 10, | |||
|
260 | "text": [ | |||
|
261 | "<open file '<fdopen>', mode 'rb' at 0x10dc651e0>" | |||
|
262 | ] | |||
|
263 | } | |||
|
264 | ], | |||
|
265 | "prompt_number": 10 | |||
|
266 | }, | |||
|
267 | { | |||
|
268 | "cell_type": "code", | |||
|
269 | "input": [ | |||
|
270 | "print ruby_lines.read()" | |||
|
271 | ], | |||
|
272 | "language": "python", | |||
|
273 | "outputs": [ | |||
|
274 | { | |||
|
275 | "output_type": "stream", | |||
|
276 | "stream": "stdout", | |||
|
277 | "text": [ | |||
|
278 | "line 1", | |||
|
279 | "line 2", | |||
|
280 | "line 3", | |||
|
281 | "line 4", | |||
|
282 | "line 5", | |||
|
283 | "line 6", | |||
|
284 | "line 7", | |||
|
285 | "line 8", | |||
|
286 | "line 9", | |||
|
287 | "", | |||
|
288 | "" | |||
|
289 | ] | |||
|
290 | } | |||
|
291 | ], | |||
|
292 | "prompt_number": 11 | |||
|
293 | }, | |||
|
294 | { | |||
|
295 | "cell_type": "heading", | |||
|
296 | "level": 2, | |||
|
297 | "source": [ | |||
|
298 | "Arguments to subcommand" | |||
|
299 | ] | |||
|
300 | }, | |||
|
301 | { | |||
|
302 | "cell_type": "markdown", | |||
|
303 | "source": [ | |||
|
304 | "You can pass arguments the subcommand as well,", | |||
|
305 | "such as this example instructing Python to use integer division from Python 3:" | |||
|
306 | ] | |||
|
307 | }, | |||
|
308 | { | |||
|
309 | "cell_type": "code", | |||
|
310 | "input": [ | |||
|
311 | "%%script python -Qnew", | |||
|
312 | "print 1/3" | |||
|
313 | ], | |||
|
314 | "language": "python", | |||
|
315 | "outputs": [ | |||
|
316 | { | |||
|
317 | "output_type": "stream", | |||
|
318 | "stream": "stdout", | |||
|
319 | "text": [ | |||
|
320 | "0.333333333333", | |||
|
321 | "" | |||
|
322 | ] | |||
|
323 | } | |||
|
324 | ], | |||
|
325 | "prompt_number": 12 | |||
|
326 | }, | |||
|
327 | { | |||
|
328 | "cell_type": "markdown", | |||
|
329 | "source": [ | |||
|
330 | "You can really specify *any* program for `%%script`,", | |||
|
331 | "for instance here is a 'program' that echos the lines of stdin, with delays between each line." | |||
|
332 | ] | |||
|
333 | }, | |||
|
334 | { | |||
|
335 | "cell_type": "code", | |||
|
336 | "input": [ | |||
|
337 | "%%script --bg --out bashout bash -c \"while read line; do echo $line; sleep 1; done\"", | |||
|
338 | "line 1", | |||
|
339 | "line 2", | |||
|
340 | "line 3", | |||
|
341 | "line 4", | |||
|
342 | "line 5", | |||
|
343 | "" | |||
|
344 | ], | |||
|
345 | "language": "python", | |||
|
346 | "outputs": [ | |||
|
347 | { | |||
|
348 | "output_type": "stream", | |||
|
349 | "stream": "stdout", | |||
|
350 | "text": [ | |||
|
351 | "Starting job # 2 in a separate thread.", | |||
|
352 | "" | |||
|
353 | ] | |||
|
354 | } | |||
|
355 | ], | |||
|
356 | "prompt_number": 13 | |||
|
357 | }, | |||
|
358 | { | |||
|
359 | "cell_type": "markdown", | |||
|
360 | "source": [ | |||
|
361 | "Remember, since the output of a background script is just the stdout pipe,", | |||
|
362 | "you can read it as lines become available:" | |||
|
363 | ] | |||
|
364 | }, | |||
|
365 | { | |||
|
366 | "cell_type": "code", | |||
|
367 | "input": [ | |||
|
368 | "import time", | |||
|
369 | "tic = time.time()", | |||
|
370 | "line = True", | |||
|
371 | "while True:", | |||
|
372 | " line = bashout.readline()", | |||
|
373 | " if not line:", | |||
|
374 | " break", | |||
|
375 | " sys.stdout.write(\"%.1fs: %s\" %(time.time()-tic, line))", | |||
|
376 | " sys.stdout.flush()", | |||
|
377 | "" | |||
|
378 | ], | |||
|
379 | "language": "python", | |||
|
380 | "outputs": [ | |||
|
381 | { | |||
|
382 | "output_type": "stream", | |||
|
383 | "stream": "stdout", | |||
|
384 | "text": [ | |||
|
385 | "0.0s: line 1", | |||
|
386 | "" | |||
|
387 | ] | |||
|
388 | }, | |||
|
389 | { | |||
|
390 | "output_type": "stream", | |||
|
391 | "stream": "stdout", | |||
|
392 | "text": [ | |||
|
393 | "1.0s: line 2", | |||
|
394 | "" | |||
|
395 | ] | |||
|
396 | }, | |||
|
397 | { | |||
|
398 | "output_type": "stream", | |||
|
399 | "stream": "stdout", | |||
|
400 | "text": [ | |||
|
401 | "2.0s: line 3", | |||
|
402 | "" | |||
|
403 | ] | |||
|
404 | }, | |||
|
405 | { | |||
|
406 | "output_type": "stream", | |||
|
407 | "stream": "stdout", | |||
|
408 | "text": [ | |||
|
409 | "3.0s: line 4", | |||
|
410 | "" | |||
|
411 | ] | |||
|
412 | }, | |||
|
413 | { | |||
|
414 | "output_type": "stream", | |||
|
415 | "stream": "stdout", | |||
|
416 | "text": [ | |||
|
417 | "4.0s: line 5", | |||
|
418 | "" | |||
|
419 | ] | |||
|
420 | } | |||
|
421 | ], | |||
|
422 | "prompt_number": 14 | |||
|
423 | }, | |||
|
424 | { | |||
|
425 | "cell_type": "heading", | |||
|
426 | "level": 2, | |||
|
427 | "source": [ | |||
|
428 | "Configuring the default ScriptMagics" | |||
|
429 | ] | |||
|
430 | }, | |||
|
431 | { | |||
|
432 | "cell_type": "markdown", | |||
|
433 | "source": [ | |||
|
434 | "The list of aliased script magics is configurable.", | |||
|
435 | "", | |||
|
436 | "The default is to pick from a few common interpreters, and use them if found, but you can specify your own in ipython_config.py:", | |||
|
437 | "", | |||
|
438 | " c.ScriptMagics.scripts = ['R', 'pypy', 'myprogram']", | |||
|
439 | "", | |||
|
440 | "And if any of these programs do not apear on your default PATH, then you would also need to specify their location with:", | |||
|
441 | "", | |||
|
442 | " c.ScriptMagics.script_paths = {'myprogram': '/opt/path/to/myprogram'}" | |||
|
443 | ] | |||
|
444 | } | |||
|
445 | ] | |||
|
446 | } | |||
|
447 | ] | |||
|
448 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now