-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathipython_directive.html
More file actions
654 lines (533 loc) · 30.3 KB
/
ipython_directive.html
File metadata and controls
654 lines (533 loc) · 30.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ipython Directive — sampledoc 1.0 documentation</title>
<link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Sphinx cheat sheet" href="cheatsheet.html" />
<link rel="prev" title="Sphinx extensions for embedded plots, math and more" href="extensions.html" />
</head>
<body role="document">
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="index.html"><h1 style="font-size: 3em;">Sampledoc</h1></a>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="cheatsheet.html" title="Sphinx cheat sheet"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="extensions.html" title="Sphinx extensions for embedded plots, math and more"
accesskey="P">previous</a> |</li>
<li><a href="index.html">home</a>| </li>
<li><a href="search.html">search</a>| </li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Ipython Directive</a><ul>
<li><a class="reference internal" href="#pseudo-decorators">Pseudo-Decorators</a></li>
<li><a class="reference internal" href="#sphinx-source-for-this-tutorial">Sphinx source for this tutorial</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="extensions.html"
title="previous chapter">Sphinx extensions for embedded plots, math and more</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="cheatsheet.html"
title="next chapter">Sphinx cheat sheet</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/ipython_directive.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="ipython-directive">
<span id="id1"></span><h1>Ipython Directive<a class="headerlink" href="#ipython-directive" title="Permalink to this headline">¶</a></h1>
<p>The ipython directive is a stateful ipython shell for embedding in
sphinx documents. It knows about standard ipython prompts, and
extracts the input and output lines. These prompts will be renumbered
starting at <code class="docutils literal"><span class="pre">1</span></code>. The inputs will be fed to an embedded ipython
interpreter and the outputs from that interpreter will be inserted as
well. For example, code blocks like the following:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">..</span> <span class="n">ipython</span><span class="p">::</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">136</span><span class="p">]:</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">137</span><span class="p">]:</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">137</span><span class="p">]:</span> <span class="mi">8</span>
</pre></div>
</div>
<p>will be rendered as</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">In [2]: </span><span class="n">x</span><span class="o">**</span><span class="mi">3</span>
<span class="gh">Out[2]: </span><span class="go">8</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This tutorial should be read side-by-side with the Sphinc source
for this document (see <a class="reference internal" href="#ipython-literal"><span class="std std-ref">Sphinx source for this tutorial</span></a>) because otherwise
you will see only the rendered output and not the code that
generated it. Excepting the example above, we will not in general
be showing the liuteral rest in this document that generates the
rendered output.</p>
</div>
<p>The state from previous sessions is stored, and standard error is
trapped. At doc build time, ipython’s output and std err will be
inserted, and prompts will be renumbered. So the prompt below should
be renumbered in the rendered docs, and pick up where the block above
left off.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="n">z</span> <span class="o">=</span> <span class="n">x</span><span class="o">*</span><span class="mi">3</span> <span class="c1"># x is recalled from previous block</span>
<span class="gp">In [4]: </span><span class="n">z</span>
<span class="gh">Out[4]: </span><span class="go">6</span>
<span class="gp">In [5]: </span><span class="k">print</span> <span class="n">z</span>
<span class="go"> File "<ipython-input-5-771f2b45b9f9>", line 1</span>
<span class="go"> print z</span>
<span class="go"> ^</span>
<span class="go">SyntaxError: Missing parentheses in call to 'print'</span>
<span class="gp">In [6]: </span><span class="n">q</span> <span class="o">=</span> <span class="n">z</span><span class="p">[)</span> <span class="c1"># this is a syntax error -- we trap ipy exceptions</span>
<span class="go"> File "<ipython-input-6-9e4a6c3ff9f7>", line 1</span>
<span class="go"> q = z[) # this is a syntax error -- we trap ipy exceptions</span>
<span class="go"> ^</span>
<span class="go">SyntaxError: invalid syntax</span>
</pre></div>
</div>
<p>The embedded interpreter supports some limited markup. For example,
you can put comments in your ipython sessions, which are reported
verbatim. There are some handy “pseudo-decorators” that let you
doctest the output. The inputs are fed to an embedded ipython
session and the outputs from the ipython session are inserted into
your doc. If the output in your doc and in the ipython session don’t
match on a doctest assertion, an error will be</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [7]: </span><span class="n">x</span> <span class="o">=</span> <span class="s1">'hello world'</span>
<span class="go"># this will raise an error if the ipython output is different</span>
<span class="gp">In [8]: </span><span class="n">x</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
<span class="gh">Out[8]: </span><span class="go">'HELLO WORLD'</span>
<span class="go"># some readline features cannot be supported, so we allow</span>
<span class="go"># "verbatim" blocks, which are dumped in verbatim except prompts</span>
<span class="go"># are continuously numbered</span>
<span class="gp">In [9]: </span><span class="n">x</span><span class="o">.</span><span class="n">st</span><span class="o"><</span><span class="n">TAB</span><span class="o">></span>
<span class="go">x.startswith x.strip</span>
</pre></div>
</div>
<p>Multi-line input is supported.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [10]: </span><span class="n">url</span> <span class="o">=</span> <span class="s1">'http://ichart.finance.yahoo.com/table.csv?s=CROX</span><span class="se">\</span>
<span class="gp"> ....: </span><span class="s1">&d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv'</span>
<span class="gp"> ....: </span>
<span class="gp">In [11]: </span><span class="k">print</span> <span class="n">url</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'&'</span><span class="p">)</span>
<span class="gt"> File</span><span class="nn"> "<ipython-input-11-f46480457c17>"</span><span class="gt">, line </span><span class="mi">1</span>
<span class="k">print</span> <span class="n">url</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'&'</span><span class="p">)</span>
<span class="o">^</span>
<span class="ne">SyntaxError</span>: invalid syntax
</pre></div>
</div>
<p>‘f=2009’, ‘g=d’, ‘a=1’, ‘b=8’, ‘c=2006’, ‘ignore=.csv’]</p>
<blockquote>
<div>In [60]: import urllib</div></blockquote>
<p>You can do doctesting on multi-line output as well. Just be careful
when using non-deterministic inputs like random numbers in the ipython
directive, because your inputs are ruin through a live interpreter, so
if you are doctesting random output you will get an error. Here we
“seed” the random number generator for deterministic output, and we
suppress the seed line so it doesn’t show up in the rendered output</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [12]: </span><span class="kn">import</span> <span class="nn">numpy.random</span>
<span class="gp">In [13]: </span><span class="n">numpy</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="gh">Out[13]: </span><span class="go"></span>
<span class="go">array([[ 0.64524308, 0.59943846],</span>
<span class="go"> [ 0.47102322, 0.8715456 ],</span>
<span class="go"> [ 0.29370834, 0.74776844],</span>
<span class="go"> [ 0.99539577, 0.1313423 ],</span>
<span class="go"> [ 0.16250302, 0.21103583],</span>
<span class="go"> [ 0.81626524, 0.1312433 ],</span>
<span class="go"> [ 0.67338089, 0.72302393],</span>
<span class="go"> [ 0.7566368 , 0.07033696],</span>
<span class="go"> [ 0.22591016, 0.77731835],</span>
<span class="go"> [ 0.0072729 , 0.34273127]])</span>
</pre></div>
</div>
<p>Another demonstration of multi-line input and output</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [14]: </span><span class="k">print</span> <span class="n">x</span>
<span class="gt"> File</span><span class="nn"> "<ipython-input-14-2d264e11d975>"</span><span class="gt">, line </span><span class="mi">1</span>
<span class="k">print</span> <span class="n">x</span>
<span class="o">^</span>
<span class="ne">SyntaxError</span>: Missing parentheses in call to 'print'
<span class="gp">In [15]: </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
<span class="gp"> ....: </span> <span class="k">print</span> <span class="n">i</span>
<span class="gp"> ....: </span>
<span class="gp"> ....: </span>
<span class="go"> File "<ipython-input-15-9f1225234c19>", line 2</span>
<span class="go"> print i</span>
<span class="go"> ^</span>
<span class="go">SyntaxError: Missing parentheses in call to 'print'</span>
</pre></div>
</div>
<p>Most of the “pseudo-decorators” can be used an options to ipython
mode. For example, to setup matplotlib pylab but suppress the output,
you can do. When using the matplotlib <code class="docutils literal"><span class="pre">use</span></code> directive, it should
occur before any import of pylab. This will not show up in the
rendered docs, but the commands will be executed in the embedded
interpreter and subsequent line numbers will be incremented to reflect
the inputs:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">..</span> <span class="n">ipython</span><span class="p">::</span>
<span class="p">:</span><span class="n">suppress</span><span class="p">:</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">144</span><span class="p">]:</span> <span class="kn">from</span> <span class="nn">pylab</span> <span class="k">import</span> <span class="o">*</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">145</span><span class="p">]:</span> <span class="n">ion</span><span class="p">()</span>
</pre></div>
</div>
<p>Likewise, you can set <code class="docutils literal"><span class="pre">:doctest:</span></code> or <code class="docutils literal"><span class="pre">:verbatim:</span></code> to apply these
settings to the entire block. For example,</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [16]: </span><span class="n">cd</span> <span class="n">mpl</span><span class="o">/</span><span class="n">examples</span><span class="o">/</span>
<span class="go">/home/jdhunter/mpl/examples</span>
<span class="gp">In [17]: </span><span class="n">pwd</span>
<span class="gh">Out[17]: </span><span class="go">'/home/jdhunter/mpl/examples'</span>
<span class="gp">In [18]: </span><span class="n">cd</span> <span class="n">mpl</span><span class="o">/</span><span class="n">examples</span><span class="o">/<</span><span class="n">TAB</span><span class="o">></span>
<span class="go">mpl/examples/animation/ mpl/examples/misc/</span>
<span class="go">mpl/examples/api/ mpl/examples/mplot3d/</span>
<span class="go">mpl/examples/axes_grid/ mpl/examples/pylab_examples/</span>
<span class="go">mpl/examples/event_handling/ mpl/examples/widgets</span>
<span class="gp">In [19]: </span><span class="n">cd</span> <span class="n">mpl</span><span class="o">/</span><span class="n">examples</span><span class="o">/</span><span class="n">widgets</span><span class="o">/</span>
<span class="go">/home/jdhunter/mpl/examples/widgets</span>
<span class="gp">In [20]: </span><span class="o">!</span>wc *
<span class="go"> 2 12 77 README.txt</span>
<span class="go"> 40 97 884 buttons.py</span>
<span class="go"> 26 90 712 check_buttons.py</span>
<span class="go"> 19 52 416 cursor.py</span>
<span class="go"> 180 404 4882 menu.py</span>
<span class="go"> 16 45 337 multicursor.py</span>
<span class="go"> 36 106 916 radio_buttons.py</span>
<span class="go"> 48 226 2082 rectangle_selector.py</span>
<span class="go"> 43 118 1063 slider_demo.py</span>
<span class="go"> 40 124 1088 span_selector.py</span>
<span class="go"> 450 1274 12457 total</span>
</pre></div>
</div>
<p>You can create one or more pyplot plots and insert them with the
<code class="docutils literal"><span class="pre">@savefig</span></code> decorator.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [21]: </span><span class="n">plot</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]);</span>
<span class="go"># use a semicolon to suppress the output</span>
<span class="gp">In [22]: </span><span class="n">hist</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">10000</span><span class="p">),</span> <span class="mi">100</span><span class="p">);</span>
</pre></div>
</div>
<a class="reference internal image-reference" href="_images/plot_simple.png"><img alt="_images/plot_simple.png" src="_images/plot_simple.png" style="width: 4in;" /></a>
<a class="reference internal image-reference" href="_images/hist_simple.png"><img alt="_images/hist_simple.png" src="_images/hist_simple.png" style="width: 4in;" /></a>
<p>In a subsequent session, we can update the current figure with some
text, and then resave</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [23]: </span><span class="n">ylabel</span><span class="p">(</span><span class="s1">'number'</span><span class="p">)</span>
<span class="gh">Out[23]: </span><span class="go"><matplotlib.text.Text at 0x7f39fb36ab38></span>
<span class="gp">In [24]: </span><span class="n">title</span><span class="p">(</span><span class="s1">'normal distribution'</span><span class="p">)</span>
<span class="go">Out[24]: <matplotlib.text.Text at 0x7f39fb37b208></span>
<span class="gp">In [25]: </span><span class="n">grid</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<a class="reference internal image-reference" href="_images/hist_with_text.png"><img alt="_images/hist_with_text.png" src="_images/hist_with_text.png" style="width: 4in;" /></a>
<div class="section" id="pseudo-decorators">
<h2>Pseudo-Decorators<a class="headerlink" href="#pseudo-decorators" title="Permalink to this headline">¶</a></h2>
<p>Here are the supported decorators, and any optional arguments they
take. Some of the decorators can be used as options to the entire
block (eg <code class="docutils literal"><span class="pre">verbatim</span></code> and <code class="docutils literal"><span class="pre">suppress</span></code>), and some only apply to the
line just below them (eg <code class="docutils literal"><span class="pre">savefig</span></code>).</p>
<p>@suppress</p>
<blockquote>
<div>execute the ipython input block, but suppress the input and output
block from the rendered output. Also, can be applied to the entire
<code class="docutils literal"><span class="pre">..ipython</span></code> block as a directive option with <code class="docutils literal"><span class="pre">:suppress:</span></code>.</div></blockquote>
<p>@verbatim</p>
<blockquote>
<div>insert the input and output block in verbatim, but auto-increment
the line numbers. Internally, the interpreter will be fed an empty
string, so it is a no-op that keeps line numbering consistent.
Also, can be applied to the entire <code class="docutils literal"><span class="pre">..ipython</span></code> block as a
directive option with <code class="docutils literal"><span class="pre">:verbatim:</span></code>.</div></blockquote>
<p>@savefig OUTFILE [IMAGE_OPTIONS]</p>
<blockquote>
<div>save the figure to the static directory and insert it into the
document, possibly binding it into a minipage and/or putting
code/figure label/references to associate the code and the
figure. Takes args to pass to the image directive (<em>scale</em>,
<em>width</em>, etc can be kwargs); see <a class="reference external" href="http://docutils.sourceforge.net/docs/ref/rst/directives.html#image">image options</a>
for details.</div></blockquote>
<p>@doctest</p>
<blockquote>
<div>Compare the pasted in output in the ipython block with the output
generated at doc build time, and raise errors if they don’t
match. Also, can be applied to the entire <code class="docutils literal"><span class="pre">..ipython</span></code> block as a
directive option with <code class="docutils literal"><span class="pre">:doctest:</span></code>.</div></blockquote>
</div>
<div class="section" id="sphinx-source-for-this-tutorial">
<span id="ipython-literal"></span><h2>Sphinx source for this tutorial<a class="headerlink" href="#sphinx-source-for-this-tutorial" title="Permalink to this headline">¶</a></h2>
<div class="highlight-default"><div class="highlight"><pre><span></span>.. _ipython_directive:
=================
Ipython Directive
=================
The ipython directive is a stateful ipython shell for embedding in
sphinx documents. It knows about standard ipython prompts, and
extracts the input and output lines. These prompts will be renumbered
starting at ``1``. The inputs will be fed to an embedded ipython
interpreter and the outputs from that interpreter will be inserted as
well. For example, code blocks like the following::
.. ipython::
In [136]: x = 2
In [137]: x**3
Out[137]: 8
will be rendered as
.. ipython::
In [136]: x = 2
In [137]: x**3
Out[137]: 8
.. note::
This tutorial should be read side-by-side with the Sphinc source
for this document (see :ref:`ipython_literal`) because otherwise
you will see only the rendered output and not the code that
generated it. Excepting the example above, we will not in general
be showing the liuteral rest in this document that generates the
rendered output.
The state from previous sessions is stored, and standard error is
trapped. At doc build time, ipython's output and std err will be
inserted, and prompts will be renumbered. So the prompt below should
be renumbered in the rendered docs, and pick up where the block above
left off.
.. ipython::
In [138]: z = x*3 # x is recalled from previous block
In [139]: z
Out[139]: 6
In [140]: print z
--------> print(z)
6
In [141]: q = z[) # this is a syntax error -- we trap ipy exceptions
------------------------------------------------------------
File "<ipython console>", line 1
q = z[) # this is a syntax error -- we trap ipy exceptions
^
SyntaxError: invalid syntax
The embedded interpreter supports some limited markup. For example,
you can put comments in your ipython sessions, which are reported
verbatim. There are some handy "pseudo-decorators" that let you
doctest the output. The inputs are fed to an embedded ipython
session and the outputs from the ipython session are inserted into
your doc. If the output in your doc and in the ipython session don't
match on a doctest assertion, an error will be
.. ipython::
In [1]: x = 'hello world'
# this will raise an error if the ipython output is different
@doctest
In [2]: x.upper()
Out[2]: 'HELLO WORLD'
# some readline features cannot be supported, so we allow
# "verbatim" blocks, which are dumped in verbatim except prompts
# are continuously numbered
@verbatim
In [3]: x.st<TAB>
x.startswith x.strip
Multi-line input is supported.
.. ipython::
In [130]: url = 'http://ichart.finance.yahoo.com/table.csv?s=CROX\
.....: &d=9&e=22&f=2009&g=d&a=1&br=8&c=2006&ignore=.csv'
In [131]: print url.split('&')
--------> print(url.split('&'))
['http://ichart.finance.yahoo.com/table.csv?s=CROX', 'd=9', 'e=22',
'f=2009', 'g=d', 'a=1', 'b=8', 'c=2006', 'ignore=.csv']
In [60]: import urllib
You can do doctesting on multi-line output as well. Just be careful
when using non-deterministic inputs like random numbers in the ipython
directive, because your inputs are ruin through a live interpreter, so
if you are doctesting random output you will get an error. Here we
"seed" the random number generator for deterministic output, and we
suppress the seed line so it doesn't show up in the rendered output
.. ipython::
In [133]: import numpy.random
@suppress
In [134]: numpy.random.seed(2358)
@doctest
In [135]: numpy.random.rand(10,2)
Out[135]:
array([[ 0.64524308, 0.59943846],
[ 0.47102322, 0.8715456 ],
[ 0.29370834, 0.74776844],
[ 0.99539577, 0.1313423 ],
[ 0.16250302, 0.21103583],
[ 0.81626524, 0.1312433 ],
[ 0.67338089, 0.72302393],
[ 0.7566368 , 0.07033696],
[ 0.22591016, 0.77731835],
[ 0.0072729 , 0.34273127]])
Another demonstration of multi-line input and output
.. ipython::
In [106]: print x
--------> print(x)
jdh
In [109]: for i in range(10):
.....: print i
.....:
.....:
0
1
2
3
4
5
6
7
8
9
Most of the "pseudo-decorators" can be used an options to ipython
mode. For example, to setup matplotlib pylab but suppress the output,
you can do. When using the matplotlib ``use`` directive, it should
occur before any import of pylab. This will not show up in the
rendered docs, but the commands will be executed in the embedded
interpreter and subsequent line numbers will be incremented to reflect
the inputs::
.. ipython::
:suppress:
In [144]: from pylab import *
In [145]: ion()
.. ipython::
:suppress:
In [144]: from pylab import *
In [145]: ion()
Likewise, you can set ``:doctest:`` or ``:verbatim:`` to apply these
settings to the entire block. For example,
.. ipython::
:verbatim:
In [9]: cd mpl/examples/
/home/jdhunter/mpl/examples
In [10]: pwd
Out[10]: '/home/jdhunter/mpl/examples'
In [14]: cd mpl/examples/<TAB>
mpl/examples/animation/ mpl/examples/misc/
mpl/examples/api/ mpl/examples/mplot3d/
mpl/examples/axes_grid/ mpl/examples/pylab_examples/
mpl/examples/event_handling/ mpl/examples/widgets
In [14]: cd mpl/examples/widgets/
/home/jdhunter/mpl/examples/widgets
In [15]: !wc *
2 12 77 README.txt
40 97 884 buttons.py
26 90 712 check_buttons.py
19 52 416 cursor.py
180 404 4882 menu.py
16 45 337 multicursor.py
36 106 916 radio_buttons.py
48 226 2082 rectangle_selector.py
43 118 1063 slider_demo.py
40 124 1088 span_selector.py
450 1274 12457 total
You can create one or more pyplot plots and insert them with the
``@savefig`` decorator.
.. ipython::
@savefig plot_simple.png width=4in
In [151]: plot([1,2,3]);
# use a semicolon to suppress the output
@savefig hist_simple.png width=4in
In [151]: hist(np.random.randn(10000), 100);
In a subsequent session, we can update the current figure with some
text, and then resave
.. ipython::
In [151]: ylabel('number')
In [152]: title('normal distribution')
@savefig hist_with_text.png width=4in
In [153]: grid(True)
Pseudo-Decorators
=================
Here are the supported decorators, and any optional arguments they
take. Some of the decorators can be used as options to the entire
block (eg ``verbatim`` and ``suppress``), and some only apply to the
line just below them (eg ``savefig``).
@suppress
execute the ipython input block, but suppress the input and output
block from the rendered output. Also, can be applied to the entire
``..ipython`` block as a directive option with ``:suppress:``.
@verbatim
insert the input and output block in verbatim, but auto-increment
the line numbers. Internally, the interpreter will be fed an empty
string, so it is a no-op that keeps line numbering consistent.
Also, can be applied to the entire ``..ipython`` block as a
directive option with ``:verbatim:``.
@savefig OUTFILE [IMAGE_OPTIONS]
save the figure to the static directory and insert it into the
document, possibly binding it into a minipage and/or putting
code/figure label/references to associate the code and the
figure. Takes args to pass to the image directive (*scale*,
*width*, etc can be kwargs); see `image options
<http://docutils.sourceforge.net/docs/ref/rst/directives.html#image>`_
for details.
@doctest
Compare the pasted in output in the ipython block with the output
generated at doc build time, and raise errors if they don’t
match. Also, can be applied to the entire ``..ipython`` block as a
directive option with ``:doctest:``.
.. _ipython_literal:
Sphinx source for this tutorial
====================================
.. literalinclude:: ipython_directive.rst
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="cheatsheet.html" title="Sphinx cheat sheet"
>next</a> |</li>
<li class="right" >
<a href="extensions.html" title="Sphinx extensions for embedded plots, math and more"
>previous</a> |</li>
<li><a href="index.html">home</a>| </li>
<li><a href="search.html">search</a>| </li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2009, John Hunter, Fernando Perez, Michael Droettboom.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
</div>
</body>
</html>