Show More
@@ -1,529 +1,534 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Usage information for the main IPython applications. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2008-2011 The IPython Development Team |
|
6 | 6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | 9 | # the file COPYING, distributed as part of this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | import sys |
|
13 | 13 | from IPython.core import release |
|
14 | 14 | |
|
15 | 15 | cl_usage = """\ |
|
16 | 16 | ========= |
|
17 | 17 | IPython |
|
18 | 18 | ========= |
|
19 | 19 | |
|
20 | 20 | Tools for Interactive Computing in Python |
|
21 | 21 | ========================================= |
|
22 | 22 | |
|
23 | 23 | A Python shell with automatic history (input and output), dynamic object |
|
24 | 24 | introspection, easier configuration, command completion, access to the |
|
25 | 25 | system shell and more. IPython can also be embedded in running programs. |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | Usage |
|
29 | 29 | |
|
30 | 30 | ipython [subcommand] [options] [files] |
|
31 | 31 | |
|
32 | 32 | If invoked with no options, it executes all the files listed in sequence |
|
33 | 33 | and exits, use -i to enter interactive mode after running the files. Files |
|
34 | 34 | ending in .py will be treated as normal Python, but files ending in .ipy |
|
35 | 35 | can contain special IPython syntax (magic commands, shell expansions, etc.) |
|
36 | 36 | |
|
37 | 37 | Almost all configuration in IPython is available via the command-line. Do |
|
38 | 38 | `ipython --help-all` to see all available options. For persistent |
|
39 | 39 | configuration, look into your `ipython_config.py` configuration file for |
|
40 | 40 | details. |
|
41 | 41 | |
|
42 | 42 | This file is typically installed in the `IPYTHON_DIR` directory, and there |
|
43 | 43 | is a separate configuration directory for each profile. The default profile |
|
44 | 44 | directory will be located in $IPYTHON_DIR/profile_default. For Linux users, |
|
45 | 45 | IPYTHON_DIR defaults to `$HOME/.config/ipython`, and for other Unix systems |
|
46 | 46 | to `$HOME/.ipython`. For Windows users, $HOME resolves to C:\\Documents |
|
47 | 47 | and Settings\\YourUserName in most instances. |
|
48 | 48 | |
|
49 | 49 | To initialize a profile with the default configuration file, do:: |
|
50 | 50 | |
|
51 | 51 | $> ipython profile create |
|
52 | 52 | |
|
53 | 53 | and start editing `IPYTHON_DIR/profile_default/ipython_config.py` |
|
54 | 54 | |
|
55 | 55 | In IPython's documentation, we will refer to this directory as |
|
56 | 56 | `IPYTHON_DIR`, you can change its default location by creating an |
|
57 | 57 | environment variable with this name and setting it to the desired path. |
|
58 | 58 | |
|
59 | 59 | For more information, see the manual available in HTML and PDF in your |
|
60 | 60 | installation, or online at http://ipython.org/documentation.html. |
|
61 | 61 | """ |
|
62 | 62 | |
|
63 | 63 | interactive_usage = """ |
|
64 | 64 | IPython -- An enhanced Interactive Python |
|
65 | 65 | ========================================= |
|
66 | 66 | |
|
67 | 67 | IPython offers a combination of convenient shell features, special commands |
|
68 | 68 | and a history mechanism for both input (command history) and output (results |
|
69 | 69 | caching, similar to Mathematica). It is intended to be a fully compatible |
|
70 | 70 | replacement for the standard Python interpreter, while offering vastly |
|
71 | 71 | improved functionality and flexibility. |
|
72 | 72 | |
|
73 | 73 | At your system command line, type 'ipython -h' to see the command line |
|
74 | 74 | options available. This document only describes interactive features. |
|
75 | 75 | |
|
76 | 76 | MAIN FEATURES |
|
77 | 77 | |
|
78 | 78 | * Access to the standard Python help. As of Python 2.1, a help system is |
|
79 | 79 | available with access to object docstrings and the Python manuals. Simply |
|
80 | 80 | type 'help' (no quotes) to access it. |
|
81 | 81 | |
|
82 | 82 | * Magic commands: type %magic for information on the magic subsystem. |
|
83 | 83 | |
|
84 | 84 | * System command aliases, via the %alias command or the configuration file(s). |
|
85 | 85 | |
|
86 | 86 | * Dynamic object information: |
|
87 | 87 | |
|
88 | 88 | Typing ?word or word? prints detailed information about an object. If |
|
89 | 89 | certain strings in the object are too long (docstrings, code, etc.) they get |
|
90 | 90 | snipped in the center for brevity. |
|
91 | 91 | |
|
92 | 92 | Typing ??word or word?? gives access to the full information without |
|
93 | 93 | snipping long strings. Long strings are sent to the screen through the less |
|
94 | 94 | pager if longer than the screen, printed otherwise. |
|
95 | 95 | |
|
96 | 96 | The ?/?? system gives access to the full source code for any object (if |
|
97 | 97 | available), shows function prototypes and other useful information. |
|
98 | 98 | |
|
99 | 99 | If you just want to see an object's docstring, type '%pdoc object' (without |
|
100 | 100 | quotes, and without % if you have automagic on). |
|
101 | 101 | |
|
102 | 102 | Both %pdoc and ?/?? give you access to documentation even on things which are |
|
103 | 103 | not explicitely defined. Try for example typing {}.get? or after import os, |
|
104 | 104 | type os.path.abspath??. The magic functions %pdef, %source and %file operate |
|
105 | 105 | similarly. |
|
106 | 106 | |
|
107 | 107 | * Completion in the local namespace, by typing TAB at the prompt. |
|
108 | 108 | |
|
109 | 109 | At any time, hitting tab will complete any available python commands or |
|
110 | 110 | variable names, and show you a list of the possible completions if there's |
|
111 | 111 | no unambiguous one. It will also complete filenames in the current directory. |
|
112 | 112 | |
|
113 | 113 | This feature requires the readline and rlcomplete modules, so it won't work |
|
114 | 114 | if your Python lacks readline support (such as under Windows). |
|
115 | 115 | |
|
116 | 116 | * Search previous command history in two ways (also requires readline): |
|
117 | 117 | |
|
118 | 118 | - Start typing, and then use Ctrl-p (previous,up) and Ctrl-n (next,down) to |
|
119 | 119 | search through only the history items that match what you've typed so |
|
120 | 120 | far. If you use Ctrl-p/Ctrl-n at a blank prompt, they just behave like |
|
121 | 121 | normal arrow keys. |
|
122 | 122 | |
|
123 | 123 | - Hit Ctrl-r: opens a search prompt. Begin typing and the system searches |
|
124 | 124 | your history for lines that match what you've typed so far, completing as |
|
125 | 125 | much as it can. |
|
126 | 126 | |
|
127 | 127 | - %hist: search history by index (this does *not* require readline). |
|
128 | 128 | |
|
129 | 129 | * Persistent command history across sessions. |
|
130 | 130 | |
|
131 | 131 | * Logging of input with the ability to save and restore a working session. |
|
132 | 132 | |
|
133 | 133 | * System escape with !. Typing !ls will run 'ls' in the current directory. |
|
134 | 134 | |
|
135 | 135 | * The reload command does a 'deep' reload of a module: changes made to the |
|
136 | 136 | module since you imported will actually be available without having to exit. |
|
137 | 137 | |
|
138 | 138 | * Verbose and colored exception traceback printouts. See the magic xmode and |
|
139 | 139 | xcolor functions for details (just type %magic). |
|
140 | 140 | |
|
141 | 141 | * Input caching system: |
|
142 | 142 | |
|
143 | 143 | IPython offers numbered prompts (In/Out) with input and output caching. All |
|
144 | 144 | input is saved and can be retrieved as variables (besides the usual arrow |
|
145 | 145 | key recall). |
|
146 | 146 | |
|
147 | 147 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
148 | 148 | _i: stores previous input. |
|
149 | 149 | _ii: next previous. |
|
150 | 150 | _iii: next-next previous. |
|
151 | 151 | _ih : a list of all input _ih[n] is the input from line n. |
|
152 | 152 | |
|
153 | 153 | Additionally, global variables named _i<n> are dynamically created (<n> |
|
154 | 154 | being the prompt counter), such that _i<n> == _ih[<n>] |
|
155 | 155 | |
|
156 | 156 | For example, what you typed at prompt 14 is available as _i14 and _ih[14]. |
|
157 | 157 | |
|
158 | 158 | You can create macros which contain multiple input lines from this history, |
|
159 | 159 | for later re-execution, with the %macro function. |
|
160 | 160 | |
|
161 | 161 | The history function %hist allows you to see any part of your input history |
|
162 | 162 | by printing a range of the _i variables. Note that inputs which contain |
|
163 | 163 | magic functions (%) appear in the history with a prepended comment. This is |
|
164 | 164 | because they aren't really valid Python code, so you can't exec them. |
|
165 | 165 | |
|
166 | 166 | * Output caching system: |
|
167 | 167 | |
|
168 | 168 | For output that is returned from actions, a system similar to the input |
|
169 | 169 | cache exists but using _ instead of _i. Only actions that produce a result |
|
170 | 170 | (NOT assignments, for example) are cached. If you are familiar with |
|
171 | 171 | Mathematica, IPython's _ variables behave exactly like Mathematica's % |
|
172 | 172 | variables. |
|
173 | 173 | |
|
174 | 174 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
175 | 175 | _ (one underscore): previous output. |
|
176 | 176 | __ (two underscores): next previous. |
|
177 | 177 | ___ (three underscores): next-next previous. |
|
178 | 178 | |
|
179 | 179 | Global variables named _<n> are dynamically created (<n> being the prompt |
|
180 | 180 | counter), such that the result of output <n> is always available as _<n>. |
|
181 | 181 | |
|
182 | 182 | Finally, a global dictionary named _oh exists with entries for all lines |
|
183 | 183 | which generated output. |
|
184 | 184 | |
|
185 | 185 | * Directory history: |
|
186 | 186 | |
|
187 | 187 | Your history of visited directories is kept in the global list _dh, and the |
|
188 | 188 | magic %cd command can be used to go to any entry in that list. |
|
189 | 189 | |
|
190 | 190 | * Auto-parentheses and auto-quotes (adapted from Nathan Gray's LazyPython) |
|
191 | 191 | |
|
192 | 192 | 1. Auto-parentheses |
|
193 | 193 | Callable objects (i.e. functions, methods, etc) can be invoked like |
|
194 | 194 | this (notice the commas between the arguments): |
|
195 | 195 | In [1]: callable_ob arg1, arg2, arg3 |
|
196 | 196 | and the input will be translated to this: |
|
197 | 197 | ------> callable_ob(arg1, arg2, arg3) |
|
198 | This feature is off by default (in rare cases it can produce | |
|
199 | undesirable side-effects), but you can activate it at the command-line | |
|
200 | by starting IPython with `--autocall 1`, set it permanently in your | |
|
201 | configuration file, or turn on at runtime with `%autocall 1`. | |
|
202 | ||
|
198 | 203 | You can force auto-parentheses by using '/' as the first character |
|
199 | 204 | of a line. For example: |
|
200 | 205 | In [1]: /globals # becomes 'globals()' |
|
201 | 206 | Note that the '/' MUST be the first character on the line! This |
|
202 | 207 | won't work: |
|
203 | 208 | In [2]: print /globals # syntax error |
|
204 | 209 | |
|
205 | 210 | In most cases the automatic algorithm should work, so you should |
|
206 | 211 | rarely need to explicitly invoke /. One notable exception is if you |
|
207 | 212 | are trying to call a function with a list of tuples as arguments (the |
|
208 | 213 | parenthesis will confuse IPython): |
|
209 | 214 | In [1]: zip (1,2,3),(4,5,6) # won't work |
|
210 | 215 | but this will work: |
|
211 | 216 | In [2]: /zip (1,2,3),(4,5,6) |
|
212 | 217 | ------> zip ((1,2,3),(4,5,6)) |
|
213 | 218 | Out[2]= [(1, 4), (2, 5), (3, 6)] |
|
214 | 219 | |
|
215 | 220 | IPython tells you that it has altered your command line by |
|
216 | 221 | displaying the new command line preceded by -->. e.g.: |
|
217 | 222 | In [18]: callable list |
|
218 | 223 | -------> callable (list) |
|
219 | 224 | |
|
220 | 225 | 2. Auto-Quoting |
|
221 | 226 | You can force auto-quoting of a function's arguments by using ',' as |
|
222 | 227 | the first character of a line. For example: |
|
223 | 228 | In [1]: ,my_function /home/me # becomes my_function("/home/me") |
|
224 | 229 | |
|
225 | 230 | If you use ';' instead, the whole argument is quoted as a single |
|
226 | 231 | string (while ',' splits on whitespace): |
|
227 | 232 | In [2]: ,my_function a b c # becomes my_function("a","b","c") |
|
228 | 233 | In [3]: ;my_function a b c # becomes my_function("a b c") |
|
229 | 234 | |
|
230 | 235 | Note that the ',' MUST be the first character on the line! This |
|
231 | 236 | won't work: |
|
232 | 237 | In [4]: x = ,my_function /home/me # syntax error |
|
233 | 238 | """ |
|
234 | 239 | |
|
235 | 240 | interactive_usage_min = """\ |
|
236 | 241 | An enhanced console for Python. |
|
237 | 242 | Some of its features are: |
|
238 | 243 | - Readline support if the readline library is present. |
|
239 | 244 | - Tab completion in the local namespace. |
|
240 | 245 | - Logging of input, see command-line options. |
|
241 | 246 | - System shell escape via ! , eg !ls. |
|
242 | 247 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
243 | 248 | - Keeps track of locally defined variables via %who, %whos. |
|
244 | 249 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
245 | 250 | """ |
|
246 | 251 | |
|
247 | 252 | quick_reference = r""" |
|
248 | 253 | IPython -- An enhanced Interactive Python - Quick Reference Card |
|
249 | 254 | ================================================================ |
|
250 | 255 | |
|
251 | 256 | obj?, obj?? : Get help, or more help for object (also works as |
|
252 | 257 | ?obj, ??obj). |
|
253 | 258 | ?foo.*abc* : List names in 'foo' containing 'abc' in them. |
|
254 | 259 | %magic : Information about IPython's 'magic' % functions. |
|
255 | 260 | |
|
256 | 261 | Magic functions are prefixed by %, and typically take their arguments without |
|
257 | 262 | parentheses, quotes or even commas for convenience. |
|
258 | 263 | |
|
259 | 264 | Example magic function calls: |
|
260 | 265 | |
|
261 | 266 | %alias d ls -F : 'd' is now an alias for 'ls -F' |
|
262 | 267 | alias d ls -F : Works if 'alias' not a python name |
|
263 | 268 | alist = %alias : Get list of aliases to 'alist' |
|
264 | 269 | cd /usr/share : Obvious. cd -<tab> to choose from visited dirs. |
|
265 | 270 | %cd?? : See help AND source for magic %cd |
|
266 | 271 | |
|
267 | 272 | System commands: |
|
268 | 273 | |
|
269 | 274 | !cp a.txt b/ : System command escape, calls os.system() |
|
270 | 275 | cp a.txt b/ : after %rehashx, most system commands work without ! |
|
271 | 276 | cp ${f}.txt $bar : Variable expansion in magics and system commands |
|
272 | 277 | files = !ls /usr : Capture sytem command output |
|
273 | 278 | files.s, files.l, files.n: "a b c", ['a','b','c'], 'a\nb\nc' |
|
274 | 279 | |
|
275 | 280 | History: |
|
276 | 281 | |
|
277 | 282 | _i, _ii, _iii : Previous, next previous, next next previous input |
|
278 | 283 | _i4, _ih[2:5] : Input history line 4, lines 2-4 |
|
279 | 284 | exec _i81 : Execute input history line #81 again |
|
280 | 285 | %rep 81 : Edit input history line #81 |
|
281 | 286 | _, __, ___ : previous, next previous, next next previous output |
|
282 | 287 | _dh : Directory history |
|
283 | 288 | _oh : Output history |
|
284 | 289 | %hist : Command history. '%hist -g foo' search history for 'foo' |
|
285 | 290 | |
|
286 | 291 | Autocall: |
|
287 | 292 | |
|
288 | f 1,2 : f(1,2) | |
|
293 | f 1,2 : f(1,2) # Off by default, enable with %autocall magic. | |
|
289 | 294 | /f 1,2 : f(1,2) (forced autoparen) |
|
290 | 295 | ,f 1 2 : f("1","2") |
|
291 | 296 | ;f 1 2 : f("1 2") |
|
292 | 297 | |
|
293 | 298 | Remember: TAB completion works in many contexts, not just file names |
|
294 | 299 | or python names. |
|
295 | 300 | |
|
296 | 301 | The following magic functions are currently available: |
|
297 | 302 | |
|
298 | 303 | """ |
|
299 | 304 | |
|
300 | 305 | gui_reference = """\ |
|
301 | 306 | =============================== |
|
302 | 307 | The graphical IPython console |
|
303 | 308 | =============================== |
|
304 | 309 | |
|
305 | 310 | This console is designed to emulate the look, feel and workflow of a terminal |
|
306 | 311 | environment, while adding a number of enhancements that are simply not possible |
|
307 | 312 | in a real terminal, such as inline syntax highlighting, true multiline editing, |
|
308 | 313 | inline graphics and much more. |
|
309 | 314 | |
|
310 | 315 | This quick reference document contains the basic information you'll need to |
|
311 | 316 | know to make the most efficient use of it. For the various command line |
|
312 | 317 | options available at startup, type ``ipython qtconsole --help`` at the command line. |
|
313 | 318 | |
|
314 | 319 | |
|
315 | 320 | Multiline editing |
|
316 | 321 | ================= |
|
317 | 322 | |
|
318 | 323 | The graphical console is capable of true multiline editing, but it also tries |
|
319 | 324 | to behave intuitively like a terminal when possible. If you are used to |
|
320 | 325 | IPyhton's old terminal behavior, you should find the transition painless, and |
|
321 | 326 | once you learn a few basic keybindings it will be a much more efficient |
|
322 | 327 | environment. |
|
323 | 328 | |
|
324 | 329 | For single expressions or indented blocks, the console behaves almost like the |
|
325 | 330 | terminal IPython: single expressions are immediately evaluated, and indented |
|
326 | 331 | blocks are evaluated once a single blank line is entered:: |
|
327 | 332 | |
|
328 | 333 | In [1]: print "Hello IPython!" # Enter was pressed at the end of the line |
|
329 | 334 | Hello IPython! |
|
330 | 335 | |
|
331 | 336 | In [2]: for i in range(10): |
|
332 | 337 | ...: print i, |
|
333 | 338 | ...: |
|
334 | 339 | 0 1 2 3 4 5 6 7 8 9 |
|
335 | 340 | |
|
336 | 341 | If you want to enter more than one expression in a single input block |
|
337 | 342 | (something not possible in the terminal), you can use ``Control-Enter`` at the |
|
338 | 343 | end of your first line instead of ``Enter``. At that point the console goes |
|
339 | 344 | into 'cell mode' and even if your inputs are not indented, it will continue |
|
340 | 345 | accepting arbitrarily many lines until either you enter an extra blank line or |
|
341 | 346 | you hit ``Shift-Enter`` (the key binding that forces execution). When a |
|
342 | 347 | multiline cell is entered, IPython analyzes it and executes its code producing |
|
343 | 348 | an ``Out[n]`` prompt only for the last expression in it, while the rest of the |
|
344 | 349 | cell is executed as if it was a script. An example should clarify this:: |
|
345 | 350 | |
|
346 | 351 | In [3]: x=1 # Hit C-Enter here |
|
347 | 352 | ...: y=2 # from now on, regular Enter is sufficient |
|
348 | 353 | ...: z=3 |
|
349 | 354 | ...: x**2 # This does *not* produce an Out[] value |
|
350 | 355 | ...: x+y+z # Only the last expression does |
|
351 | 356 | ...: |
|
352 | 357 | Out[3]: 6 |
|
353 | 358 | |
|
354 | 359 | The behavior where an extra blank line forces execution is only active if you |
|
355 | 360 | are actually typing at the keyboard each line, and is meant to make it mimic |
|
356 | 361 | the IPython terminal behavior. If you paste a long chunk of input (for example |
|
357 | 362 | a long script copied form an editor or web browser), it can contain arbitrarily |
|
358 | 363 | many intermediate blank lines and they won't cause any problems. As always, |
|
359 | 364 | you can then make it execute by appending a blank line *at the end* or hitting |
|
360 | 365 | ``Shift-Enter`` anywhere within the cell. |
|
361 | 366 | |
|
362 | 367 | With the up arrow key, you can retrieve previous blocks of input that contain |
|
363 | 368 | multiple lines. You can move inside of a multiline cell like you would in any |
|
364 | 369 | text editor. When you want it executed, the simplest thing to do is to hit the |
|
365 | 370 | force execution key, ``Shift-Enter`` (though you can also navigate to the end |
|
366 | 371 | and append a blank line by using ``Enter`` twice). |
|
367 | 372 | |
|
368 | 373 | If you've edited a multiline cell and accidentally navigate out of it with the |
|
369 | 374 | up or down arrow keys, IPython will clear the cell and replace it with the |
|
370 | 375 | contents of the one above or below that you navigated to. If this was an |
|
371 | 376 | accident and you want to retrieve the cell you were editing, use the Undo |
|
372 | 377 | keybinding, ``Control-z``. |
|
373 | 378 | |
|
374 | 379 | |
|
375 | 380 | Key bindings |
|
376 | 381 | ============ |
|
377 | 382 | |
|
378 | 383 | The IPython console supports most of the basic Emacs line-oriented keybindings, |
|
379 | 384 | in addition to some of its own. |
|
380 | 385 | |
|
381 | 386 | The keybinding prefixes mean: |
|
382 | 387 | |
|
383 | 388 | - ``C``: Control |
|
384 | 389 | - ``S``: Shift |
|
385 | 390 | - ``M``: Meta (typically the Alt key) |
|
386 | 391 | |
|
387 | 392 | The keybindings themselves are: |
|
388 | 393 | |
|
389 | 394 | - ``Enter``: insert new line (may cause execution, see above). |
|
390 | 395 | - ``C-Enter``: *force* new line, *never* causes execution. |
|
391 | 396 | - ``S-Enter``: *force* execution regardless of where cursor is, no newline added. |
|
392 | 397 | - ``Up``: step backwards through the history. |
|
393 | 398 | - ``Down``: step forwards through the history. |
|
394 | 399 | - ``S-Up``: search backwards through the history (like ``C-r`` in bash). |
|
395 | 400 | - ``S-Down``: search forwards through the history. |
|
396 | 401 | - ``C-c``: copy highlighted text to clipboard (prompts are automatically stripped). |
|
397 | 402 | - ``C-S-c``: copy highlighted text to clipboard (prompts are not stripped). |
|
398 | 403 | - ``C-v``: paste text from clipboard. |
|
399 | 404 | - ``C-z``: undo (retrieves lost text if you move out of a cell with the arrows). |
|
400 | 405 | - ``C-S-z``: redo. |
|
401 | 406 | - ``C-o``: move to 'other' area, between pager and terminal. |
|
402 | 407 | - ``C-l``: clear terminal. |
|
403 | 408 | - ``C-a``: go to beginning of line. |
|
404 | 409 | - ``C-e``: go to end of line. |
|
405 | 410 | - ``C-k``: kill from cursor to the end of the line. |
|
406 | 411 | - ``C-y``: yank (paste) |
|
407 | 412 | - ``C-p``: previous line (like up arrow) |
|
408 | 413 | - ``C-n``: next line (like down arrow) |
|
409 | 414 | - ``C-f``: forward (like right arrow) |
|
410 | 415 | - ``C-b``: back (like left arrow) |
|
411 | 416 | - ``C-d``: delete next character. |
|
412 | 417 | - ``M-<``: move to the beginning of the input region. |
|
413 | 418 | - ``M->``: move to the end of the input region. |
|
414 | 419 | - ``M-d``: delete next word. |
|
415 | 420 | - ``M-Backspace``: delete previous word. |
|
416 | 421 | - ``C-.``: force a kernel restart (a confirmation dialog appears). |
|
417 | 422 | - ``C-+``: increase font size. |
|
418 | 423 | - ``C--``: decrease font size. |
|
419 | 424 | - ``C-M-Space``: toggle full screen. (Command-Control-Space on Mac OS X) |
|
420 | 425 | |
|
421 | 426 | The IPython pager |
|
422 | 427 | ================= |
|
423 | 428 | |
|
424 | 429 | IPython will show long blocks of text from many sources using a builtin pager. |
|
425 | 430 | You can control where this pager appears with the ``--paging`` command-line |
|
426 | 431 | flag: |
|
427 | 432 | |
|
428 | 433 | - ``inside`` [default]: the pager is overlaid on top of the main terminal. You |
|
429 | 434 | must quit the pager to get back to the terminal (similar to how a pager such |
|
430 | 435 | as ``less`` or ``more`` works). |
|
431 | 436 | |
|
432 | 437 | - ``vsplit``: the console is made double-tall, and the pager appears on the |
|
433 | 438 | bottom area when needed. You can view its contents while using the terminal. |
|
434 | 439 | |
|
435 | 440 | - ``hsplit``: the console is made double-wide, and the pager appears on the |
|
436 | 441 | right area when needed. You can view its contents while using the terminal. |
|
437 | 442 | |
|
438 | 443 | - ``none``: the console never pages output. |
|
439 | 444 | |
|
440 | 445 | If you use the vertical or horizontal paging modes, you can navigate between |
|
441 | 446 | terminal and pager as follows: |
|
442 | 447 | |
|
443 | 448 | - Tab key: goes from pager to terminal (but not the other way around). |
|
444 | 449 | - Control-o: goes from one to another always. |
|
445 | 450 | - Mouse: click on either. |
|
446 | 451 | |
|
447 | 452 | In all cases, the ``q`` or ``Escape`` keys quit the pager (when used with the |
|
448 | 453 | focus on the pager area). |
|
449 | 454 | |
|
450 | 455 | Running subprocesses |
|
451 | 456 | ==================== |
|
452 | 457 | |
|
453 | 458 | The graphical IPython console uses the ``pexpect`` module to run subprocesses |
|
454 | 459 | when you type ``!command``. This has a number of advantages (true asynchronous |
|
455 | 460 | output from subprocesses as well as very robust termination of rogue |
|
456 | 461 | subprocesses with ``Control-C``), as well as some limitations. The main |
|
457 | 462 | limitation is that you can *not* interact back with the subprocess, so anything |
|
458 | 463 | that invokes a pager or expects you to type input into it will block and hang |
|
459 | 464 | (you can kill it with ``Control-C``). |
|
460 | 465 | |
|
461 | 466 | We have provided as magics ``%less`` to page files (aliased to ``%more``), |
|
462 | 467 | ``%clear`` to clear the terminal, and ``%man`` on Linux/OSX. These cover the |
|
463 | 468 | most common commands you'd want to call in your subshell and that would cause |
|
464 | 469 | problems if invoked via ``!cmd``, but you need to be aware of this limitation. |
|
465 | 470 | |
|
466 | 471 | Display |
|
467 | 472 | ======= |
|
468 | 473 | |
|
469 | 474 | The IPython console can now display objects in a variety of formats, including |
|
470 | 475 | HTML, PNG and SVG. This is accomplished using the display functions in |
|
471 | 476 | ``IPython.core.display``:: |
|
472 | 477 | |
|
473 | 478 | In [4]: from IPython.core.display import display, display_html |
|
474 | 479 | |
|
475 | 480 | In [5]: from IPython.core.display import display_png, display_svg |
|
476 | 481 | |
|
477 | 482 | Python objects can simply be passed to these functions and the appropriate |
|
478 | 483 | representations will be displayed in the console as long as the objects know |
|
479 | 484 | how to compute those representations. The easiest way of teaching objects how |
|
480 | 485 | to format themselves in various representations is to define special methods |
|
481 | 486 | such as: ``_repr_html_``, ``_repr_svg_`` and ``_repr_png_``. IPython's display formatters |
|
482 | 487 | can also be given custom formatter functions for various types:: |
|
483 | 488 | |
|
484 | 489 | In [6]: ip = get_ipython() |
|
485 | 490 | |
|
486 | 491 | In [7]: html_formatter = ip.display_formatter.formatters['text/html'] |
|
487 | 492 | |
|
488 | 493 | In [8]: html_formatter.for_type(Foo, foo_to_html) |
|
489 | 494 | |
|
490 | 495 | For further details, see ``IPython.core.formatters``. |
|
491 | 496 | |
|
492 | 497 | Inline matplotlib graphics |
|
493 | 498 | ========================== |
|
494 | 499 | |
|
495 | 500 | The IPython console is capable of displaying matplotlib figures inline, in SVG |
|
496 | 501 | or PNG format. If started with the ``pylab=inline``, then all figures are |
|
497 | 502 | rendered inline automatically (PNG by default). If started with ``--pylab`` |
|
498 | 503 | or ``pylab=<your backend>``, then a GUI backend will be used, but IPython's |
|
499 | 504 | ``display()`` and ``getfigs()`` functions can be used to view plots inline:: |
|
500 | 505 | |
|
501 | 506 | In [9]: display(*getfigs()) # display all figures inline |
|
502 | 507 | |
|
503 | 508 | In[10]: display(*getfigs(1,2)) # display figures 1 and 2 inline |
|
504 | 509 | """ |
|
505 | 510 | |
|
506 | 511 | |
|
507 | 512 | quick_guide = """\ |
|
508 | 513 | ? -> Introduction and overview of IPython's features. |
|
509 | 514 | %quickref -> Quick reference. |
|
510 | 515 | help -> Python's own help system. |
|
511 | 516 | object? -> Details about 'object', use 'object??' for extra details. |
|
512 | 517 | """ |
|
513 | 518 | |
|
514 | 519 | gui_note = """\ |
|
515 | 520 | %guiref -> A brief reference about the graphical user interface. |
|
516 | 521 | """ |
|
517 | 522 | |
|
518 | 523 | default_banner_parts = [ |
|
519 | 524 | 'Python %s\n' % (sys.version.split('\n')[0],), |
|
520 | 525 | 'Type "copyright", "credits" or "license" for more information.\n\n', |
|
521 | 526 | 'IPython %s -- An enhanced Interactive Python.\n' % (release.version,), |
|
522 | 527 | quick_guide |
|
523 | 528 | ] |
|
524 | 529 | |
|
525 | 530 | default_gui_banner_parts = default_banner_parts + [gui_note] |
|
526 | 531 | |
|
527 | 532 | default_banner = ''.join(default_banner_parts) |
|
528 | 533 | |
|
529 | 534 | default_gui_banner = ''.join(default_gui_banner_parts) |
@@ -1,1263 +1,1003 b'' | |||
|
1 | 1 | ================= |
|
2 | 2 | IPython reference |
|
3 | 3 | ================= |
|
4 | 4 | |
|
5 | 5 | .. _command_line_options: |
|
6 | 6 | |
|
7 | 7 | Command-line usage |
|
8 | 8 | ================== |
|
9 | 9 | |
|
10 | 10 | You start IPython with the command:: |
|
11 | 11 | |
|
12 | 12 | $ ipython [options] files |
|
13 | 13 | |
|
14 | 14 | .. note:: |
|
15 | 15 | |
|
16 | 16 | For IPython on Python 3, use ``ipython3`` in place of ``ipython``. |
|
17 | 17 | |
|
18 | 18 | If invoked with no options, it executes all the files listed in sequence |
|
19 | 19 | and drops you into the interpreter while still acknowledging any options |
|
20 | 20 | you may have set in your ipython_config.py. This behavior is different from |
|
21 | 21 | standard Python, which when called as python -i will only execute one |
|
22 | 22 | file and ignore your configuration setup. |
|
23 | 23 | |
|
24 | 24 | Please note that some of the configuration options are not available at |
|
25 | 25 | the command line, simply because they are not practical here. Look into |
|
26 | 26 | your configuration files for details on those. There are separate configuration |
|
27 | 27 | files for each profile, and the files look like "ipython_config.py" or |
|
28 | 28 | "ipython_config_<frontendname>.py". Profile directories look like |
|
29 | 29 | "profile_profilename" and are typically installed in the IPYTHON_DIR directory. |
|
30 | 30 | For Linux users, this will be $HOME/.config/ipython, and for other users it |
|
31 | 31 | will be $HOME/.ipython. For Windows users, $HOME resolves to C:\\Documents and |
|
32 | 32 | Settings\\YourUserName in most instances. |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | Eventloop integration |
|
36 | 36 | --------------------- |
|
37 | 37 | |
|
38 | 38 | Previously IPython had command line options for controlling GUI event loop |
|
39 | 39 | integration (-gthread, -qthread, -q4thread, -wthread, -pylab). As of IPython |
|
40 | 40 | version 0.11, these have been removed. Please see the new ``%gui`` |
|
41 | 41 | magic command or :ref:`this section <gui_support>` for details on the new |
|
42 | 42 | interface, or specify the gui at the commandline:: |
|
43 | 43 | |
|
44 | 44 | $ ipython --gui=qt |
|
45 | 45 | |
|
46 | 46 | |
|
47 | Regular Options | |
|
48 | --------------- | |
|
47 | Command-line Options | |
|
48 | -------------------- | |
|
49 | ||
|
50 | To see the options IPython accepts, use ``ipython --help`` (and you probably | |
|
51 | should run the output through a pager such as ``ipython --help | less`` for | |
|
52 | more convenient reading). This shows all the options that have a single-word | |
|
53 | alias to control them, but IPython lets you configure all of its objects from | |
|
54 | the command-line by passing the full class name and a corresponding value; type | |
|
55 | ``ipython --help-all`` to see this full list. For example:: | |
|
56 | ||
|
57 | ipython --pylab qt | |
|
58 | ||
|
59 | is equivalent to:: | |
|
60 | ||
|
61 | ipython --TerminalIPythonApp.pylab='qt' | |
|
62 | ||
|
63 | Note that in the second form, you *must* use the equal sign, as the expression | |
|
64 | is evaluated as an actual Python assignment. While in the above example the | |
|
65 | short form is more convenient, only the most common options have a short form, | |
|
66 | while any configurable variable in IPython can be set at the command-line by | |
|
67 | using the long form. This long form is the same syntax used in the | |
|
68 | configuration files, if you want to set these options permanently. | |
|
49 | 69 | |
|
50 | After the above threading options have been given, regular options can | |
|
51 | follow in any order. All options can be abbreviated to their shortest | |
|
52 | non-ambiguous form and are case-sensitive. | |
|
53 | ||
|
54 | Most options can also be set from your configuration file. See the provided | |
|
55 | example for more details on what the options do. Options given at the command | |
|
56 | line override the values set in the configuration file. | |
|
57 | ||
|
58 | All options with a [no] prepended can be specified in negated form | |
|
59 | (--no-option instead of --option) to turn the feature off. | |
|
60 | ||
|
61 | ``-h, --help`` print a help message and exit. | |
|
62 | ||
|
63 | ``--pylab, pylab=<name>`` | |
|
64 | See :ref:`Matplotlib support <matplotlib_support>` | |
|
65 | for more details. | |
|
66 | ||
|
67 | ``--autocall=<val>`` | |
|
68 | Make IPython automatically call any callable object even if you | |
|
69 | didn't type explicit parentheses. For example, 'str 43' becomes | |
|
70 | 'str(43)' automatically. The value can be '0' to disable the feature, | |
|
71 | '1' for smart autocall, where it is not applied if there are no more | |
|
72 | arguments on the line, and '2' for full autocall, where all callable | |
|
73 | objects are automatically called (even if no arguments are | |
|
74 | present). The default is '1'. | |
|
75 | ||
|
76 | ``--[no-]autoindent`` | |
|
77 | Turn automatic indentation on/off. | |
|
78 | ||
|
79 | ``--[no-]automagic`` | |
|
80 | make magic commands automatic (without needing their first character | |
|
81 | to be %). Type %magic at the IPython prompt for more information. | |
|
82 | ||
|
83 | ``--[no-]autoedit_syntax`` | |
|
84 | When a syntax error occurs after editing a file, automatically | |
|
85 | open the file to the trouble causing line for convenient | |
|
86 | fixing. | |
|
87 | ||
|
88 | ``--[no-]banner`` | |
|
89 | Print the initial information banner (default on). | |
|
90 | ||
|
91 | ``-c <command>`` | |
|
92 | execute the given command string. This is similar to the -c | |
|
93 | option in the normal Python interpreter. | |
|
94 | ||
|
95 | ``--cache-size=<n>`` | |
|
96 | size of the output cache (maximum number of entries to hold in | |
|
97 | memory). The default is 1000, you can change it permanently in your | |
|
98 | config file. Setting it to 0 completely disables the caching system, | |
|
99 | and the minimum value accepted is 20 (if you provide a value less than | |
|
100 | 20, it is reset to 0 and a warning is issued) This limit is defined | |
|
101 | because otherwise you'll spend more time re-flushing a too small cache | |
|
102 | than working. | |
|
103 | ||
|
104 | ``--classic`` | |
|
105 | Gives IPython a similar feel to the classic Python | |
|
106 | prompt. | |
|
107 | ||
|
108 | ``--colors=<scheme>`` | |
|
109 | Color scheme for prompts and exception reporting. Currently | |
|
110 | implemented: NoColor, Linux and LightBG. | |
|
111 | ||
|
112 | ``--[no-]color_info`` | |
|
113 | IPython can display information about objects via a set of functions, | |
|
114 | and optionally can use colors for this, syntax highlighting source | |
|
115 | code and various other elements. However, because this information is | |
|
116 | passed through a pager (like 'less') and many pagers get confused with | |
|
117 | color codes, this option is off by default. You can test it and turn | |
|
118 | it on permanently in your configuration file if it works for you. As a | |
|
119 | reference, the 'less' pager supplied with Mandrake 8.2 works ok, but | |
|
120 | that in RedHat 7.2 doesn't. | |
|
121 | ||
|
122 | Test it and turn it on permanently if it works with your | |
|
123 | system. The magic function %color_info allows you to toggle this | |
|
124 | interactively for testing. | |
|
125 | ||
|
126 | ``--[no-]debug`` | |
|
127 | Show information about the loading process. Very useful to pin down | |
|
128 | problems with your configuration files or to get details about | |
|
129 | session restores. | |
|
130 | ||
|
131 | ``--[no-]deep_reload`` | |
|
132 | IPython can use the deep_reload module which reloads changes in | |
|
133 | modules recursively (it replaces the reload() function, so you don't | |
|
134 | need to change anything to use it). deep_reload() forces a full | |
|
135 | reload of modules whose code may have changed, which the default | |
|
136 | reload() function does not. | |
|
137 | ||
|
138 | When deep_reload is off, IPython will use the normal reload(), | |
|
139 | but deep_reload will still be available as dreload(). This | |
|
140 | feature is off by default [which means that you have both | |
|
141 | normal reload() and dreload()]. | |
|
142 | ||
|
143 | .. this isn't currently working | |
|
144 | .. ``--editor=<name>`` | |
|
145 | Which editor to use with the %edit command. By default, | |
|
146 | IPython will honor your EDITOR environment variable (if not | |
|
147 | set, vi is the Unix default and notepad the Windows one). | |
|
148 | Since this editor is invoked on the fly by IPython and is | |
|
149 | meant for editing small code snippets, you may want to use a | |
|
150 | small, lightweight editor here (in case your default EDITOR is | |
|
151 | something like Emacs). | |
|
152 | ||
|
153 | ``--ipython_dir=<name>`` | |
|
154 | name of your IPython configuration directory IPYTHON_DIR. This | |
|
155 | can also be specified through the environment variable | |
|
156 | IPYTHON_DIR. | |
|
157 | ||
|
158 | ``--logfile=<name>`` | |
|
159 | specify the name of your logfile. | |
|
160 | ||
|
161 | This implies ``%logstart`` at the beginning of your session | |
|
162 | ||
|
163 | generate a log file of all input. The file is named | |
|
164 | ipython_log.py in your current directory (which prevents logs | |
|
165 | from multiple IPython sessions from trampling each other). You | |
|
166 | can use this to later restore a session by loading your | |
|
167 | logfile with ``ipython -i ipython_log.py`` | |
|
168 | ||
|
169 | ``--logplay=<name>`` | |
|
170 | ||
|
171 | NOT AVAILABLE in 0.11 | |
|
172 | ||
|
173 | you can replay a previous log. For restoring a session as close as | |
|
174 | possible to the state you left it in, use this option (don't just run | |
|
175 | the logfile). With -logplay, IPython will try to reconstruct the | |
|
176 | previous working environment in full, not just execute the commands in | |
|
177 | the logfile. | |
|
178 | ||
|
179 | When a session is restored, logging is automatically turned on | |
|
180 | again with the name of the logfile it was invoked with (it is | |
|
181 | read from the log header). So once you've turned logging on for | |
|
182 | a session, you can quit IPython and reload it as many times as | |
|
183 | you want and it will continue to log its history and restore | |
|
184 | from the beginning every time. | |
|
185 | ||
|
186 | Caveats: there are limitations in this option. The history | |
|
187 | variables _i*,_* and _dh don't get restored properly. In the | |
|
188 | future we will try to implement full session saving by writing | |
|
189 | and retrieving a 'snapshot' of the memory state of IPython. But | |
|
190 | our first attempts failed because of inherent limitations of | |
|
191 | Python's Pickle module, so this may have to wait. | |
|
192 | ||
|
193 | ``--[no-]messages`` | |
|
194 | Print messages which IPython collects about its startup | |
|
195 | process (default on). | |
|
196 | ||
|
197 | ``--[no-]pdb`` | |
|
198 | Automatically call the pdb debugger after every uncaught | |
|
199 | exception. If you are used to debugging using pdb, this puts | |
|
200 | you automatically inside of it after any call (either in | |
|
201 | IPython or in code called by it) which triggers an exception | |
|
202 | which goes uncaught. | |
|
203 | ||
|
204 | ``--[no-]pprint`` | |
|
205 | ipython can optionally use the pprint (pretty printer) module | |
|
206 | for displaying results. pprint tends to give a nicer display | |
|
207 | of nested data structures. If you like it, you can turn it on | |
|
208 | permanently in your config file (default off). | |
|
209 | ||
|
210 | ``--profile=<name>`` | |
|
211 | ||
|
212 | Select the IPython profile by name. | |
|
213 | ||
|
214 | This is a quick way to keep and load multiple | |
|
215 | config files for different tasks, especially if you use the | |
|
216 | include option of config files. You can keep a basic | |
|
217 | :file:`IPYTHON_DIR/profile_default/ipython_config.py` file | |
|
218 | and then have other 'profiles' which | |
|
219 | include this one and load extra things for particular | |
|
220 | tasks. For example: | |
|
221 | ||
|
222 | 1. $IPYTHON_DIR/profile_default : load basic things you always want. | |
|
223 | 2. $IPYTHON_DIR/profile_math : load (1) and basic math-related modules. | |
|
224 | 3. $IPYTHON_DIR/profile_numeric : load (1) and Numeric and plotting modules. | |
|
225 | ||
|
226 | Since it is possible to create an endless loop by having | |
|
227 | circular file inclusions, IPython will stop if it reaches 15 | |
|
228 | recursive inclusions. | |
|
229 | ||
|
230 | ``PromptManager.in_template=<string>`` | |
|
231 | ||
|
232 | Specify the string used for input prompts. Note that if you are using | |
|
233 | numbered prompts, the number is represented with a '\#' in the | |
|
234 | string. Don't forget to quote strings with spaces embedded in | |
|
235 | them. Default: 'In [\#]:'. The :ref:`prompts section <prompts>` | |
|
236 | discusses in detail all the available escapes to customize your | |
|
237 | prompts. | |
|
238 | ||
|
239 | ``PromptManager.in2_template=<string>`` | |
|
240 | Similar to the previous option, but used for the continuation | |
|
241 | prompts. The special sequence '\D' is similar to '\#', but | |
|
242 | with all digits replaced dots (so you can have your | |
|
243 | continuation prompt aligned with your input prompt). Default: | |
|
244 | ' .\D.:' (note three spaces at the start for alignment with | |
|
245 | 'In [\#]'). | |
|
246 | ||
|
247 | ``PromptManager.out_template=<string>`` | |
|
248 | String used for output prompts, also uses numbers like | |
|
249 | in_template. Default: 'Out[\#]:' | |
|
250 | ||
|
251 | ``--quick`` | |
|
252 | start in bare bones mode (no config file loaded). | |
|
253 | ||
|
254 | ``config_file=<name>`` | |
|
255 | name of your IPython resource configuration file. Normally | |
|
256 | IPython loads ipython_config.py (from current directory) or | |
|
257 | IPYTHON_DIR/profile_default. | |
|
258 | ||
|
259 | If the loading of your config file fails, IPython starts with | |
|
260 | a bare bones configuration (no modules loaded at all). | |
|
261 | ||
|
262 | ``--[no-]readline`` | |
|
263 | use the readline library, which is needed to support name | |
|
264 | completion and command history, among other things. It is | |
|
265 | enabled by default, but may cause problems for users of | |
|
266 | X/Emacs in Python comint or shell buffers. | |
|
267 | ||
|
268 | Note that X/Emacs 'eterm' buffers (opened with M-x term) support | |
|
269 | IPython's readline and syntax coloring fine, only 'emacs' (M-x | |
|
270 | shell and C-c !) buffers do not. | |
|
271 | ||
|
272 | ``--TerminalInteractiveShell.screen_length=<n>`` | |
|
273 | number of lines of your screen. This is used to control | |
|
274 | printing of very long strings. Strings longer than this number | |
|
275 | of lines will be sent through a pager instead of directly | |
|
276 | printed. | |
|
277 | ||
|
278 | The default value for this is 0, which means IPython will | |
|
279 | auto-detect your screen size every time it needs to print certain | |
|
280 | potentially long strings (this doesn't change the behavior of the | |
|
281 | 'print' keyword, it's only triggered internally). If for some | |
|
282 | reason this isn't working well (it needs curses support), specify | |
|
283 | it yourself. Otherwise don't change the default. | |
|
284 | ||
|
285 | ``--TerminalInteractiveShell.separate_in=<string>`` | |
|
286 | ||
|
287 | separator before input prompts. | |
|
288 | Default: '\n' | |
|
289 | ||
|
290 | ``--TerminalInteractiveShell.separate_out=<string>`` | |
|
291 | separator before output prompts. | |
|
292 | Default: nothing. | |
|
293 | ||
|
294 | ``--TerminalInteractiveShell.separate_out2=<string>`` | |
|
295 | separator after output prompts. | |
|
296 | Default: nothing. | |
|
297 | For these three options, use the value 0 to specify no separator. | |
|
298 | ||
|
299 | ``--nosep`` | |
|
300 | shorthand for setting the above separators to empty strings. | |
|
301 | ||
|
302 | Simply removes all input/output separators. | |
|
303 | ||
|
304 | ``--init`` | |
|
305 | allows you to initialize a profile dir for configuration when you | |
|
306 | install a new version of IPython or want to use a new profile. | |
|
307 | Since new versions may include new command line options or example | |
|
308 | files, this copies updated config files. Note that you should probably | |
|
309 | use %upgrade instead,it's a safer alternative. | |
|
310 | ||
|
311 | ``--version`` print version information and exit. | |
|
312 | ||
|
313 | ``--xmode=<modename>`` | |
|
314 | ||
|
315 | Mode for exception reporting. | |
|
316 | ||
|
317 | Valid modes: Plain, Context and Verbose. | |
|
318 | ||
|
319 | * Plain: similar to python's normal traceback printing. | |
|
320 | * Context: prints 5 lines of context source code around each | |
|
321 | line in the traceback. | |
|
322 | * Verbose: similar to Context, but additionally prints the | |
|
323 | variables currently visible where the exception happened | |
|
324 | (shortening their strings if too long). This can potentially be | |
|
325 | very slow, if you happen to have a huge data structure whose | |
|
326 | string representation is complex to compute. Your computer may | |
|
327 | appear to freeze for a while with cpu usage at 100%. If this | |
|
328 | occurs, you can cancel the traceback with Ctrl-C (maybe hitting it | |
|
329 | more than once). | |
|
330 | 70 | |
|
331 | 71 | Interactive use |
|
332 | 72 | =============== |
|
333 | 73 | |
|
334 | 74 | IPython is meant to work as a drop-in replacement for the standard interactive |
|
335 | 75 | interpreter. As such, any code which is valid python should execute normally |
|
336 | 76 | under IPython (cases where this is not true should be reported as bugs). It |
|
337 | 77 | does, however, offer many features which are not available at a standard python |
|
338 | 78 | prompt. What follows is a list of these. |
|
339 | 79 | |
|
340 | 80 | |
|
341 | 81 | Caution for Windows users |
|
342 | 82 | ------------------------- |
|
343 | 83 | |
|
344 | 84 | Windows, unfortunately, uses the '\\' character as a path separator. This is a |
|
345 | 85 | terrible choice, because '\\' also represents the escape character in most |
|
346 | 86 | modern programming languages, including Python. For this reason, using '/' |
|
347 | 87 | character is recommended if you have problems with ``\``. However, in Windows |
|
348 | 88 | commands '/' flags options, so you can not use it for the root directory. This |
|
349 | 89 | means that paths beginning at the root must be typed in a contrived manner |
|
350 | 90 | like: ``%copy \opt/foo/bar.txt \tmp`` |
|
351 | 91 | |
|
352 | 92 | .. _magic: |
|
353 | 93 | |
|
354 | 94 | Magic command system |
|
355 | 95 | -------------------- |
|
356 | 96 | |
|
357 | 97 | IPython will treat any line whose first character is a % as a special |
|
358 | 98 | call to a 'magic' function. These allow you to control the behavior of |
|
359 | 99 | IPython itself, plus a lot of system-type features. They are all |
|
360 | 100 | prefixed with a % character, but parameters are given without |
|
361 | 101 | parentheses or quotes. |
|
362 | 102 | |
|
363 | 103 | Example: typing ``%cd mydir`` changes your working directory to 'mydir', if it |
|
364 | 104 | exists. |
|
365 | 105 | |
|
366 | 106 | If you have 'automagic' enabled (as it by default), you don't need |
|
367 | 107 | to type in the % explicitly. IPython will scan its internal list of |
|
368 | 108 | magic functions and call one if it exists. With automagic on you can |
|
369 | 109 | then just type ``cd mydir`` to go to directory 'mydir'. The automagic |
|
370 | 110 | system has the lowest possible precedence in name searches, so defining |
|
371 | 111 | an identifier with the same name as an existing magic function will |
|
372 | 112 | shadow it for automagic use. You can still access the shadowed magic |
|
373 | 113 | function by explicitly using the % character at the beginning of the line. |
|
374 | 114 | |
|
375 | 115 | An example (with automagic on) should clarify all this: |
|
376 | 116 | |
|
377 | 117 | .. sourcecode:: ipython |
|
378 | 118 | |
|
379 | 119 | In [1]: cd ipython # %cd is called by automagic |
|
380 | 120 | /home/fperez/ipython |
|
381 | 121 | |
|
382 | 122 | In [2]: cd=1 # now cd is just a variable |
|
383 | 123 | |
|
384 | 124 | In [3]: cd .. # and doesn't work as a function anymore |
|
385 | 125 | File "<ipython-input-3-9fedb3aff56c>", line 1 |
|
386 | 126 | cd .. |
|
387 | 127 | ^ |
|
388 | 128 | SyntaxError: invalid syntax |
|
389 | 129 | |
|
390 | 130 | |
|
391 | 131 | In [4]: %cd .. # but %cd always works |
|
392 | 132 | /home/fperez |
|
393 | 133 | |
|
394 | 134 | In [5]: del cd # if you remove the cd variable, automagic works again |
|
395 | 135 | |
|
396 | 136 | In [6]: cd ipython |
|
397 | 137 | |
|
398 | 138 | /home/fperez/ipython |
|
399 | 139 | |
|
400 | 140 | You can define your own magic functions to extend the system. The |
|
401 | 141 | following example defines a new magic command, %impall: |
|
402 | 142 | |
|
403 | 143 | .. sourcecode:: python |
|
404 | 144 | |
|
405 | 145 | ip = get_ipython() |
|
406 | 146 | |
|
407 | 147 | def doimp(self, arg): |
|
408 | 148 | ip = self.api |
|
409 | 149 | ip.ex("import %s; reload(%s); from %s import *" % (arg,arg,arg) ) |
|
410 | 150 | |
|
411 | 151 | ip.define_magic('impall', doimp) |
|
412 | 152 | |
|
413 | 153 | Type ``%magic`` for more information, including a list of all available magic |
|
414 | 154 | functions at any time and their docstrings. You can also type |
|
415 | 155 | ``%magic_function_name?`` (see :ref:`below <dynamic_object_info>` for information on |
|
416 | 156 | the '?' system) to get information about any particular magic function you are |
|
417 | 157 | interested in. |
|
418 | 158 | |
|
419 | 159 | The API documentation for the :mod:`IPython.core.magic` module contains the full |
|
420 | 160 | docstrings of all currently available magic commands. |
|
421 | 161 | |
|
422 | 162 | |
|
423 | 163 | Access to the standard Python help |
|
424 | 164 | ---------------------------------- |
|
425 | 165 | |
|
426 | 166 | Simply type ``help()`` to access Python's standard help system. You can |
|
427 | 167 | also type ``help(object)`` for information about a given object, or |
|
428 | 168 | ``help('keyword')`` for information on a keyword. You may need to configure your |
|
429 | 169 | PYTHONDOCS environment variable for this feature to work correctly. |
|
430 | 170 | |
|
431 | 171 | .. _dynamic_object_info: |
|
432 | 172 | |
|
433 | 173 | Dynamic object information |
|
434 | 174 | -------------------------- |
|
435 | 175 | |
|
436 | 176 | Typing ``?word`` or ``word?`` prints detailed information about an object. If |
|
437 | 177 | certain strings in the object are too long (e.g. function signatures) they get |
|
438 | 178 | snipped in the center for brevity. This system gives access variable types and |
|
439 | 179 | values, docstrings, function prototypes and other useful information. |
|
440 | 180 | |
|
441 | 181 | If the information will not fit in the terminal, it is displayed in a pager |
|
442 | 182 | (``less`` if available, otherwise a basic internal pager). |
|
443 | 183 | |
|
444 | 184 | Typing ``??word`` or ``word??`` gives access to the full information, including |
|
445 | 185 | the source code where possible. Long strings are not snipped. |
|
446 | 186 | |
|
447 | 187 | The following magic functions are particularly useful for gathering |
|
448 | 188 | information about your working environment. You can get more details by |
|
449 | 189 | typing ``%magic`` or querying them individually (``%function_name?``); |
|
450 | 190 | this is just a summary: |
|
451 | 191 | |
|
452 | 192 | * **%pdoc <object>**: Print (or run through a pager if too long) the |
|
453 | 193 | docstring for an object. If the given object is a class, it will |
|
454 | 194 | print both the class and the constructor docstrings. |
|
455 | 195 | * **%pdef <object>**: Print the definition header for any callable |
|
456 | 196 | object. If the object is a class, print the constructor information. |
|
457 | 197 | * **%psource <object>**: Print (or run through a pager if too long) |
|
458 | 198 | the source code for an object. |
|
459 | 199 | * **%pfile <object>**: Show the entire source file where an object was |
|
460 | 200 | defined via a pager, opening it at the line where the object |
|
461 | 201 | definition begins. |
|
462 | 202 | * **%who/%whos**: These functions give information about identifiers |
|
463 | 203 | you have defined interactively (not things you loaded or defined |
|
464 | 204 | in your configuration files). %who just prints a list of |
|
465 | 205 | identifiers and %whos prints a table with some basic details about |
|
466 | 206 | each identifier. |
|
467 | 207 | |
|
468 | 208 | Note that the dynamic object information functions (?/??, ``%pdoc``, |
|
469 | 209 | ``%pfile``, ``%pdef``, ``%psource``) work on object attributes, as well as |
|
470 | 210 | directly on variables. For example, after doing ``import os``, you can use |
|
471 | 211 | ``os.path.abspath??``. |
|
472 | 212 | |
|
473 | 213 | .. _readline: |
|
474 | 214 | |
|
475 | 215 | Readline-based features |
|
476 | 216 | ----------------------- |
|
477 | 217 | |
|
478 | 218 | These features require the GNU readline library, so they won't work if your |
|
479 | 219 | Python installation lacks readline support. We will first describe the default |
|
480 | 220 | behavior IPython uses, and then how to change it to suit your preferences. |
|
481 | 221 | |
|
482 | 222 | |
|
483 | 223 | Command line completion |
|
484 | 224 | +++++++++++++++++++++++ |
|
485 | 225 | |
|
486 | 226 | At any time, hitting TAB will complete any available python commands or |
|
487 | 227 | variable names, and show you a list of the possible completions if |
|
488 | 228 | there's no unambiguous one. It will also complete filenames in the |
|
489 | 229 | current directory if no python names match what you've typed so far. |
|
490 | 230 | |
|
491 | 231 | |
|
492 | 232 | Search command history |
|
493 | 233 | ++++++++++++++++++++++ |
|
494 | 234 | |
|
495 | 235 | IPython provides two ways for searching through previous input and thus |
|
496 | 236 | reduce the need for repetitive typing: |
|
497 | 237 | |
|
498 | 238 | 1. Start typing, and then use Ctrl-p (previous,up) and Ctrl-n |
|
499 | 239 | (next,down) to search through only the history items that match |
|
500 | 240 | what you've typed so far. If you use Ctrl-p/Ctrl-n at a blank |
|
501 | 241 | prompt, they just behave like normal arrow keys. |
|
502 | 242 | 2. Hit Ctrl-r: opens a search prompt. Begin typing and the system |
|
503 | 243 | searches your history for lines that contain what you've typed so |
|
504 | 244 | far, completing as much as it can. |
|
505 | 245 | |
|
506 | 246 | |
|
507 | 247 | Persistent command history across sessions |
|
508 | 248 | ++++++++++++++++++++++++++++++++++++++++++ |
|
509 | 249 | |
|
510 | 250 | IPython will save your input history when it leaves and reload it next |
|
511 | 251 | time you restart it. By default, the history file is named |
|
512 | 252 | $IPYTHON_DIR/profile_<name>/history.sqlite. This allows you to keep |
|
513 | 253 | separate histories related to various tasks: commands related to |
|
514 | 254 | numerical work will not be clobbered by a system shell history, for |
|
515 | 255 | example. |
|
516 | 256 | |
|
517 | 257 | |
|
518 | 258 | Autoindent |
|
519 | 259 | ++++++++++ |
|
520 | 260 | |
|
521 | 261 | IPython can recognize lines ending in ':' and indent the next line, |
|
522 | 262 | while also un-indenting automatically after 'raise' or 'return'. |
|
523 | 263 | |
|
524 | 264 | This feature uses the readline library, so it will honor your |
|
525 | 265 | :file:`~/.inputrc` configuration (or whatever file your INPUTRC variable points |
|
526 | 266 | to). Adding the following lines to your :file:`.inputrc` file can make |
|
527 | 267 | indenting/unindenting more convenient (M-i indents, M-u unindents):: |
|
528 | 268 | |
|
529 | 269 | $if Python |
|
530 | 270 | "\M-i": " " |
|
531 | 271 | "\M-u": "\d\d\d\d" |
|
532 | 272 | $endif |
|
533 | 273 | |
|
534 | 274 | Note that there are 4 spaces between the quote marks after "M-i" above. |
|
535 | 275 | |
|
536 | 276 | .. warning:: |
|
537 | 277 | |
|
538 | 278 | Setting the above indents will cause problems with unicode text entry in |
|
539 | 279 | the terminal. |
|
540 | 280 | |
|
541 | 281 | .. warning:: |
|
542 | 282 | |
|
543 | 283 | Autoindent is ON by default, but it can cause problems with the pasting of |
|
544 | 284 | multi-line indented code (the pasted code gets re-indented on each line). A |
|
545 | 285 | magic function %autoindent allows you to toggle it on/off at runtime. You |
|
546 | 286 | can also disable it permanently on in your :file:`ipython_config.py` file |
|
547 | 287 | (set TerminalInteractiveShell.autoindent=False). |
|
548 | 288 | |
|
549 | 289 | If you want to paste multiple lines in the terminal, it is recommended that |
|
550 | 290 | you use ``%paste``. |
|
551 | 291 | |
|
552 | 292 | |
|
553 | 293 | Customizing readline behavior |
|
554 | 294 | +++++++++++++++++++++++++++++ |
|
555 | 295 | |
|
556 | 296 | All these features are based on the GNU readline library, which has an |
|
557 | 297 | extremely customizable interface. Normally, readline is configured via a |
|
558 | 298 | file which defines the behavior of the library; the details of the |
|
559 | 299 | syntax for this can be found in the readline documentation available |
|
560 | 300 | with your system or on the Internet. IPython doesn't read this file (if |
|
561 | 301 | it exists) directly, but it does support passing to readline valid |
|
562 | 302 | options via a simple interface. In brief, you can customize readline by |
|
563 | 303 | setting the following options in your configuration file (note |
|
564 | 304 | that these options can not be specified at the command line): |
|
565 | 305 | |
|
566 | 306 | * **readline_parse_and_bind**: this holds a list of strings to be executed |
|
567 | 307 | via a readline.parse_and_bind() command. The syntax for valid commands |
|
568 | 308 | of this kind can be found by reading the documentation for the GNU |
|
569 | 309 | readline library, as these commands are of the kind which readline |
|
570 | 310 | accepts in its configuration file. |
|
571 | 311 | * **readline_remove_delims**: a string of characters to be removed |
|
572 | 312 | from the default word-delimiters list used by readline, so that |
|
573 | 313 | completions may be performed on strings which contain them. Do not |
|
574 | 314 | change the default value unless you know what you're doing. |
|
575 | 315 | |
|
576 | 316 | You will find the default values in your configuration file. |
|
577 | 317 | |
|
578 | 318 | |
|
579 | 319 | Session logging and restoring |
|
580 | 320 | ----------------------------- |
|
581 | 321 | |
|
582 | 322 | You can log all input from a session either by starting IPython with the |
|
583 | 323 | command line switch ``--logfile=foo.py`` (see :ref:`here <command_line_options>`) |
|
584 | 324 | or by activating the logging at any moment with the magic function %logstart. |
|
585 | 325 | |
|
586 | 326 | Log files can later be reloaded by running them as scripts and IPython |
|
587 | 327 | will attempt to 'replay' the log by executing all the lines in it, thus |
|
588 | 328 | restoring the state of a previous session. This feature is not quite |
|
589 | 329 | perfect, but can still be useful in many cases. |
|
590 | 330 | |
|
591 | 331 | The log files can also be used as a way to have a permanent record of |
|
592 | 332 | any code you wrote while experimenting. Log files are regular text files |
|
593 | 333 | which you can later open in your favorite text editor to extract code or |
|
594 | 334 | to 'clean them up' before using them to replay a session. |
|
595 | 335 | |
|
596 | 336 | The `%logstart` function for activating logging in mid-session is used as |
|
597 | 337 | follows:: |
|
598 | 338 | |
|
599 | 339 | %logstart [log_name [log_mode]] |
|
600 | 340 | |
|
601 | 341 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
602 | 342 | current working directory, in 'rotate' mode (see below). |
|
603 | 343 | |
|
604 | 344 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
605 | 345 | history up to that point and then continues logging. |
|
606 | 346 | |
|
607 | 347 | %logstart takes a second optional parameter: logging mode. This can be |
|
608 | 348 | one of (note that the modes are given unquoted): |
|
609 | 349 | |
|
610 | 350 | * [over:] overwrite existing log_name. |
|
611 | 351 | * [backup:] rename (if exists) to log_name~ and start log_name. |
|
612 | 352 | * [append:] well, that says it. |
|
613 | 353 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. |
|
614 | 354 | |
|
615 | 355 | The %logoff and %logon functions allow you to temporarily stop and |
|
616 | 356 | resume logging to a file which had previously been started with |
|
617 | 357 | %logstart. They will fail (with an explanation) if you try to use them |
|
618 | 358 | before logging has been started. |
|
619 | 359 | |
|
620 | 360 | .. _system_shell_access: |
|
621 | 361 | |
|
622 | 362 | System shell access |
|
623 | 363 | ------------------- |
|
624 | 364 | |
|
625 | 365 | Any input line beginning with a ! character is passed verbatim (minus |
|
626 | 366 | the !, of course) to the underlying operating system. For example, |
|
627 | 367 | typing ``!ls`` will run 'ls' in the current directory. |
|
628 | 368 | |
|
629 | 369 | Manual capture of command output |
|
630 | 370 | -------------------------------- |
|
631 | 371 | |
|
632 | 372 | You can assign the result of a system command to a Python variable with the |
|
633 | 373 | syntax ``myfiles = !ls``. This gets machine readable output from stdout |
|
634 | 374 | (e.g. without colours), and splits on newlines. To explicitly get this sort of |
|
635 | 375 | output without assigning to a variable, use two exclamation marks (``!!ls``) or |
|
636 | 376 | the ``%sx`` magic command. |
|
637 | 377 | |
|
638 | 378 | The captured list has some convenience features. ``myfiles.n`` or ``myfiles.s`` |
|
639 | 379 | returns a string delimited by newlines or spaces, respectively. ``myfiles.p`` |
|
640 | 380 | produces `path objects <http://pypi.python.org/pypi/path.py>`_ from the list items. |
|
641 | 381 | See :ref:`string_lists` for details. |
|
642 | 382 | |
|
643 | 383 | IPython also allows you to expand the value of python variables when |
|
644 | 384 | making system calls. Wrap variables or expressions in {braces}:: |
|
645 | 385 | |
|
646 | 386 | In [1]: pyvar = 'Hello world' |
|
647 | 387 | In [2]: !echo "A python variable: {pyvar}" |
|
648 | 388 | A python variable: Hello world |
|
649 | 389 | In [3]: import math |
|
650 | 390 | In [4]: x = 8 |
|
651 | 391 | In [5]: !echo {math.factorial(x)} |
|
652 | 392 | 40320 |
|
653 | 393 | |
|
654 | 394 | For simple cases, you can alternatively prepend $ to a variable name:: |
|
655 | 395 | |
|
656 | 396 | In [6]: !echo $sys.argv |
|
657 | 397 | [/home/fperez/usr/bin/ipython] |
|
658 | 398 | In [7]: !echo "A system variable: $$HOME" # Use $$ for literal $ |
|
659 | 399 | A system variable: /home/fperez |
|
660 | 400 | |
|
661 | 401 | System command aliases |
|
662 | 402 | ---------------------- |
|
663 | 403 | |
|
664 | 404 | The %alias magic function allows you to define magic functions which are in fact |
|
665 | 405 | system shell commands. These aliases can have parameters. |
|
666 | 406 | |
|
667 | 407 | ``%alias alias_name cmd`` defines 'alias_name' as an alias for 'cmd' |
|
668 | 408 | |
|
669 | 409 | Then, typing ``alias_name params`` will execute the system command 'cmd |
|
670 | 410 | params' (from your underlying operating system). |
|
671 | 411 | |
|
672 | 412 | You can also define aliases with parameters using %s specifiers (one per |
|
673 | 413 | parameter). The following example defines the parts function as an |
|
674 | 414 | alias to the command 'echo first %s second %s' where each %s will be |
|
675 | 415 | replaced by a positional parameter to the call to %parts:: |
|
676 | 416 | |
|
677 | 417 | In [1]: %alias parts echo first %s second %s |
|
678 | 418 | In [2]: parts A B |
|
679 | 419 | first A second B |
|
680 | 420 | In [3]: parts A |
|
681 | 421 | ERROR: Alias <parts> requires 2 arguments, 1 given. |
|
682 | 422 | |
|
683 | 423 | If called with no parameters, %alias prints the table of currently |
|
684 | 424 | defined aliases. |
|
685 | 425 | |
|
686 | 426 | The %rehashx magic allows you to load your entire $PATH as |
|
687 | 427 | ipython aliases. See its docstring for further details. |
|
688 | 428 | |
|
689 | 429 | |
|
690 | 430 | .. _dreload: |
|
691 | 431 | |
|
692 | 432 | Recursive reload |
|
693 | 433 | ---------------- |
|
694 | 434 | |
|
695 | 435 | The :mod:`IPython.lib.deepreload` module allows you to recursively reload a |
|
696 | 436 | module: changes made to any of its dependencies will be reloaded without |
|
697 | 437 | having to exit. To start using it, do:: |
|
698 | 438 | |
|
699 | 439 | from IPython.lib.deepreload import reload as dreload |
|
700 | 440 | |
|
701 | 441 | |
|
702 | 442 | Verbose and colored exception traceback printouts |
|
703 | 443 | ------------------------------------------------- |
|
704 | 444 | |
|
705 | 445 | IPython provides the option to see very detailed exception tracebacks, |
|
706 | 446 | which can be especially useful when debugging large programs. You can |
|
707 | 447 | run any Python file with the %run function to benefit from these |
|
708 | 448 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can |
|
709 | 449 | be colored (if your terminal supports it) which makes them much easier |
|
710 | 450 | to parse visually. |
|
711 | 451 | |
|
712 | 452 | See the magic xmode and colors functions for details (just type %magic). |
|
713 | 453 | |
|
714 | 454 | These features are basically a terminal version of Ka-Ping Yee's cgitb |
|
715 | 455 | module, now part of the standard Python library. |
|
716 | 456 | |
|
717 | 457 | |
|
718 | 458 | .. _input_caching: |
|
719 | 459 | |
|
720 | 460 | Input caching system |
|
721 | 461 | -------------------- |
|
722 | 462 | |
|
723 | 463 | IPython offers numbered prompts (In/Out) with input and output caching |
|
724 | 464 | (also referred to as 'input history'). All input is saved and can be |
|
725 | 465 | retrieved as variables (besides the usual arrow key recall), in |
|
726 | 466 | addition to the %rep magic command that brings a history entry |
|
727 | 467 | up for editing on the next command line. |
|
728 | 468 | |
|
729 | 469 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
730 | 470 | |
|
731 | 471 | * _i, _ii, _iii: store previous, next previous and next-next previous inputs. |
|
732 | 472 | * In, _ih : a list of all inputs; _ih[n] is the input from line n. If you |
|
733 | 473 | overwrite In with a variable of your own, you can remake the assignment to the |
|
734 | 474 | internal list with a simple ``In=_ih``. |
|
735 | 475 | |
|
736 | 476 | Additionally, global variables named _i<n> are dynamically created (<n> |
|
737 | 477 | being the prompt counter), so ``_i<n> == _ih[<n>] == In[<n>]``. |
|
738 | 478 | |
|
739 | 479 | For example, what you typed at prompt 14 is available as _i14, _ih[14] |
|
740 | 480 | and In[14]. |
|
741 | 481 | |
|
742 | 482 | This allows you to easily cut and paste multi line interactive prompts |
|
743 | 483 | by printing them out: they print like a clean string, without prompt |
|
744 | 484 | characters. You can also manipulate them like regular variables (they |
|
745 | 485 | are strings), modify or exec them (typing ``exec _i9`` will re-execute the |
|
746 | 486 | contents of input prompt 9. |
|
747 | 487 | |
|
748 | 488 | You can also re-execute multiple lines of input easily by using the |
|
749 | 489 | magic %rerun or %macro functions. The macro system also allows you to re-execute |
|
750 | 490 | previous lines which include magic function calls (which require special |
|
751 | 491 | processing). Type %macro? for more details on the macro system. |
|
752 | 492 | |
|
753 | 493 | A history function %hist allows you to see any part of your input |
|
754 | 494 | history by printing a range of the _i variables. |
|
755 | 495 | |
|
756 | 496 | You can also search ('grep') through your history by typing |
|
757 | 497 | ``%hist -g somestring``. This is handy for searching for URLs, IP addresses, |
|
758 | 498 | etc. You can bring history entries listed by '%hist -g' up for editing |
|
759 | 499 | with the %recall command, or run them immediately with %rerun. |
|
760 | 500 | |
|
761 | 501 | .. _output_caching: |
|
762 | 502 | |
|
763 | 503 | Output caching system |
|
764 | 504 | --------------------- |
|
765 | 505 | |
|
766 | 506 | For output that is returned from actions, a system similar to the input |
|
767 | 507 | cache exists but using _ instead of _i. Only actions that produce a |
|
768 | 508 | result (NOT assignments, for example) are cached. If you are familiar |
|
769 | 509 | with Mathematica, IPython's _ variables behave exactly like |
|
770 | 510 | Mathematica's % variables. |
|
771 | 511 | |
|
772 | 512 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
773 | 513 | |
|
774 | 514 | * [_] (a single underscore) : stores previous output, like Python's |
|
775 | 515 | default interpreter. |
|
776 | 516 | * [__] (two underscores): next previous. |
|
777 | 517 | * [___] (three underscores): next-next previous. |
|
778 | 518 | |
|
779 | 519 | Additionally, global variables named _<n> are dynamically created (<n> |
|
780 | 520 | being the prompt counter), such that the result of output <n> is always |
|
781 | 521 | available as _<n> (don't use the angle brackets, just the number, e.g. |
|
782 | 522 | _21). |
|
783 | 523 | |
|
784 | 524 | These variables are also stored in a global dictionary (not a |
|
785 | 525 | list, since it only has entries for lines which returned a result) |
|
786 | 526 | available under the names _oh and Out (similar to _ih and In). So the |
|
787 | 527 | output from line 12 can be obtained as _12, Out[12] or _oh[12]. If you |
|
788 | 528 | accidentally overwrite the Out variable you can recover it by typing |
|
789 | 529 | 'Out=_oh' at the prompt. |
|
790 | 530 | |
|
791 | 531 | This system obviously can potentially put heavy memory demands on your |
|
792 | 532 | system, since it prevents Python's garbage collector from removing any |
|
793 | 533 | previously computed results. You can control how many results are kept |
|
794 | 534 | in memory with the option (at the command line or in your configuration |
|
795 | 535 | file) cache_size. If you set it to 0, the whole system is completely |
|
796 | 536 | disabled and the prompts revert to the classic '>>>' of normal Python. |
|
797 | 537 | |
|
798 | 538 | |
|
799 | 539 | Directory history |
|
800 | 540 | ----------------- |
|
801 | 541 | |
|
802 | 542 | Your history of visited directories is kept in the global list _dh, and |
|
803 | 543 | the magic %cd command can be used to go to any entry in that list. The |
|
804 | 544 | %dhist command allows you to view this history. Do ``cd -<TAB>`` to |
|
805 | 545 | conveniently view the directory history. |
|
806 | 546 | |
|
807 | 547 | |
|
808 | 548 | Automatic parentheses and quotes |
|
809 | 549 | -------------------------------- |
|
810 | 550 | |
|
811 | 551 | These features were adapted from Nathan Gray's LazyPython. They are |
|
812 | 552 | meant to allow less typing for common situations. |
|
813 | 553 | |
|
814 | 554 | |
|
815 | 555 | Automatic parentheses |
|
816 | 556 | +++++++++++++++++++++ |
|
817 | 557 | |
|
818 | 558 | Callable objects (i.e. functions, methods, etc) can be invoked like this |
|
819 | 559 | (notice the commas between the arguments):: |
|
820 | 560 | |
|
821 | 561 | In [1]: callable_ob arg1, arg2, arg3 |
|
822 | 562 | ------> callable_ob(arg1, arg2, arg3) |
|
823 | 563 | |
|
824 | 564 | You can force automatic parentheses by using '/' as the first character |
|
825 | 565 | of a line. For example:: |
|
826 | 566 | |
|
827 | 567 | In [2]: /globals # becomes 'globals()' |
|
828 | 568 | |
|
829 | 569 | Note that the '/' MUST be the first character on the line! This won't work:: |
|
830 | 570 | |
|
831 | 571 | In [3]: print /globals # syntax error |
|
832 | 572 | |
|
833 | 573 | In most cases the automatic algorithm should work, so you should rarely |
|
834 | 574 | need to explicitly invoke /. One notable exception is if you are trying |
|
835 | 575 | to call a function with a list of tuples as arguments (the parenthesis |
|
836 | 576 | will confuse IPython):: |
|
837 | 577 | |
|
838 | 578 | In [4]: zip (1,2,3),(4,5,6) # won't work |
|
839 | 579 | |
|
840 | 580 | but this will work:: |
|
841 | 581 | |
|
842 | 582 | In [5]: /zip (1,2,3),(4,5,6) |
|
843 | 583 | ------> zip ((1,2,3),(4,5,6)) |
|
844 | 584 | Out[5]: [(1, 4), (2, 5), (3, 6)] |
|
845 | 585 | |
|
846 | 586 | IPython tells you that it has altered your command line by displaying |
|
847 | 587 | the new command line preceded by ->. e.g.:: |
|
848 | 588 | |
|
849 | 589 | In [6]: callable list |
|
850 | 590 | ------> callable(list) |
|
851 | 591 | |
|
852 | 592 | |
|
853 | 593 | Automatic quoting |
|
854 | 594 | +++++++++++++++++ |
|
855 | 595 | |
|
856 | 596 | You can force automatic quoting of a function's arguments by using ',' |
|
857 | 597 | or ';' as the first character of a line. For example:: |
|
858 | 598 | |
|
859 | 599 | In [1]: ,my_function /home/me # becomes my_function("/home/me") |
|
860 | 600 | |
|
861 | 601 | If you use ';' the whole argument is quoted as a single string, while ',' splits |
|
862 | 602 | on whitespace:: |
|
863 | 603 | |
|
864 | 604 | In [2]: ,my_function a b c # becomes my_function("a","b","c") |
|
865 | 605 | |
|
866 | 606 | In [3]: ;my_function a b c # becomes my_function("a b c") |
|
867 | 607 | |
|
868 | 608 | Note that the ',' or ';' MUST be the first character on the line! This |
|
869 | 609 | won't work:: |
|
870 | 610 | |
|
871 | 611 | In [4]: x = ,my_function /home/me # syntax error |
|
872 | 612 | |
|
873 | 613 | IPython as your default Python environment |
|
874 | 614 | ========================================== |
|
875 | 615 | |
|
876 | 616 | Python honors the environment variable PYTHONSTARTUP and will execute at |
|
877 | 617 | startup the file referenced by this variable. If you put the following code at |
|
878 | 618 | the end of that file, then IPython will be your working environment anytime you |
|
879 | 619 | start Python:: |
|
880 | 620 | |
|
881 | 621 | from IPython.frontend.terminal.ipapp import launch_new_instance |
|
882 | 622 | launch_new_instance() |
|
883 | 623 | raise SystemExit |
|
884 | 624 | |
|
885 | 625 | The ``raise SystemExit`` is needed to exit Python when |
|
886 | 626 | it finishes, otherwise you'll be back at the normal Python '>>>' |
|
887 | 627 | prompt. |
|
888 | 628 | |
|
889 | 629 | This is probably useful to developers who manage multiple Python |
|
890 | 630 | versions and don't want to have correspondingly multiple IPython |
|
891 | 631 | versions. Note that in this mode, there is no way to pass IPython any |
|
892 | 632 | command-line options, as those are trapped first by Python itself. |
|
893 | 633 | |
|
894 | 634 | .. _Embedding: |
|
895 | 635 | |
|
896 | 636 | Embedding IPython |
|
897 | 637 | ================= |
|
898 | 638 | |
|
899 | 639 | It is possible to start an IPython instance inside your own Python |
|
900 | 640 | programs. This allows you to evaluate dynamically the state of your |
|
901 | 641 | code, operate with your variables, analyze them, etc. Note however that |
|
902 | 642 | any changes you make to values while in the shell do not propagate back |
|
903 | 643 | to the running code, so it is safe to modify your values because you |
|
904 | 644 | won't break your code in bizarre ways by doing so. |
|
905 | 645 | |
|
906 | 646 | .. note:: |
|
907 | 647 | |
|
908 | 648 | At present, trying to embed IPython from inside IPython causes problems. Run |
|
909 | 649 | the code samples below outside IPython. |
|
910 | 650 | |
|
911 | 651 | This feature allows you to easily have a fully functional python |
|
912 | 652 | environment for doing object introspection anywhere in your code with a |
|
913 | 653 | simple function call. In some cases a simple print statement is enough, |
|
914 | 654 | but if you need to do more detailed analysis of a code fragment this |
|
915 | 655 | feature can be very valuable. |
|
916 | 656 | |
|
917 | 657 | It can also be useful in scientific computing situations where it is |
|
918 | 658 | common to need to do some automatic, computationally intensive part and |
|
919 | 659 | then stop to look at data, plots, etc. |
|
920 | 660 | Opening an IPython instance will give you full access to your data and |
|
921 | 661 | functions, and you can resume program execution once you are done with |
|
922 | 662 | the interactive part (perhaps to stop again later, as many times as |
|
923 | 663 | needed). |
|
924 | 664 | |
|
925 | 665 | The following code snippet is the bare minimum you need to include in |
|
926 | 666 | your Python programs for this to work (detailed examples follow later):: |
|
927 | 667 | |
|
928 | 668 | from IPython import embed |
|
929 | 669 | |
|
930 | 670 | embed() # this call anywhere in your program will start IPython |
|
931 | 671 | |
|
932 | 672 | You can run embedded instances even in code which is itself being run at |
|
933 | 673 | the IPython interactive prompt with '%run <filename>'. Since it's easy |
|
934 | 674 | to get lost as to where you are (in your top-level IPython or in your |
|
935 | 675 | embedded one), it's a good idea in such cases to set the in/out prompts |
|
936 | 676 | to something different for the embedded instances. The code examples |
|
937 | 677 | below illustrate this. |
|
938 | 678 | |
|
939 | 679 | You can also have multiple IPython instances in your program and open |
|
940 | 680 | them separately, for example with different options for data |
|
941 | 681 | presentation. If you close and open the same instance multiple times, |
|
942 | 682 | its prompt counters simply continue from each execution to the next. |
|
943 | 683 | |
|
944 | 684 | Please look at the docstrings in the :mod:`~IPython.frontend.terminal.embed` |
|
945 | 685 | module for more details on the use of this system. |
|
946 | 686 | |
|
947 | 687 | The following sample file illustrating how to use the embedding |
|
948 | 688 | functionality is provided in the examples directory as example-embed.py. |
|
949 | 689 | It should be fairly self-explanatory: |
|
950 | 690 | |
|
951 | 691 | .. literalinclude:: ../../examples/core/example-embed.py |
|
952 | 692 | :language: python |
|
953 | 693 | |
|
954 | 694 | Once you understand how the system functions, you can use the following |
|
955 | 695 | code fragments in your programs which are ready for cut and paste: |
|
956 | 696 | |
|
957 | 697 | .. literalinclude:: ../../examples/core/example-embed-short.py |
|
958 | 698 | :language: python |
|
959 | 699 | |
|
960 | 700 | Using the Python debugger (pdb) |
|
961 | 701 | =============================== |
|
962 | 702 | |
|
963 | 703 | Running entire programs via pdb |
|
964 | 704 | ------------------------------- |
|
965 | 705 | |
|
966 | 706 | pdb, the Python debugger, is a powerful interactive debugger which |
|
967 | 707 | allows you to step through code, set breakpoints, watch variables, |
|
968 | 708 | etc. IPython makes it very easy to start any script under the control |
|
969 | 709 | of pdb, regardless of whether you have wrapped it into a 'main()' |
|
970 | 710 | function or not. For this, simply type '%run -d myscript' at an |
|
971 | 711 | IPython prompt. See the %run command's documentation (via '%run?' or |
|
972 | 712 | in Sec. magic_ for more details, including how to control where pdb |
|
973 | 713 | will stop execution first. |
|
974 | 714 | |
|
975 | 715 | For more information on the use of the pdb debugger, read the included |
|
976 | 716 | pdb.doc file (part of the standard Python distribution). On a stock |
|
977 | 717 | Linux system it is located at /usr/lib/python2.3/pdb.doc, but the |
|
978 | 718 | easiest way to read it is by using the help() function of the pdb module |
|
979 | 719 | as follows (in an IPython prompt):: |
|
980 | 720 | |
|
981 | 721 | In [1]: import pdb |
|
982 | 722 | In [2]: pdb.help() |
|
983 | 723 | |
|
984 | 724 | This will load the pdb.doc document in a file viewer for you automatically. |
|
985 | 725 | |
|
986 | 726 | |
|
987 | 727 | Automatic invocation of pdb on exceptions |
|
988 | 728 | ----------------------------------------- |
|
989 | 729 | |
|
990 | 730 | IPython, if started with the ``--pdb`` option (or if the option is set in |
|
991 | 731 | your config file) can call the Python pdb debugger every time your code |
|
992 | 732 | triggers an uncaught exception. This feature |
|
993 | 733 | can also be toggled at any time with the %pdb magic command. This can be |
|
994 | 734 | extremely useful in order to find the origin of subtle bugs, because pdb |
|
995 | 735 | opens up at the point in your code which triggered the exception, and |
|
996 | 736 | while your program is at this point 'dead', all the data is still |
|
997 | 737 | available and you can walk up and down the stack frame and understand |
|
998 | 738 | the origin of the problem. |
|
999 | 739 | |
|
1000 | 740 | Furthermore, you can use these debugging facilities both with the |
|
1001 | 741 | embedded IPython mode and without IPython at all. For an embedded shell |
|
1002 | 742 | (see sec. Embedding_), simply call the constructor with |
|
1003 | 743 | ``--pdb`` in the argument string and pdb will automatically be called if an |
|
1004 | 744 | uncaught exception is triggered by your code. |
|
1005 | 745 | |
|
1006 | 746 | For stand-alone use of the feature in your programs which do not use |
|
1007 | 747 | IPython at all, put the following lines toward the top of your 'main' |
|
1008 | 748 | routine:: |
|
1009 | 749 | |
|
1010 | 750 | import sys |
|
1011 | 751 | from IPython.core import ultratb |
|
1012 | 752 | sys.excepthook = ultratb.FormattedTB(mode='Verbose', |
|
1013 | 753 | color_scheme='Linux', call_pdb=1) |
|
1014 | 754 | |
|
1015 | 755 | The mode keyword can be either 'Verbose' or 'Plain', giving either very |
|
1016 | 756 | detailed or normal tracebacks respectively. The color_scheme keyword can |
|
1017 | 757 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same |
|
1018 | 758 | options which can be set in IPython with ``--colors`` and ``--xmode``. |
|
1019 | 759 | |
|
1020 | 760 | This will give any of your programs detailed, colored tracebacks with |
|
1021 | 761 | automatic invocation of pdb. |
|
1022 | 762 | |
|
1023 | 763 | |
|
1024 | 764 | Extensions for syntax processing |
|
1025 | 765 | ================================ |
|
1026 | 766 | |
|
1027 | 767 | This isn't for the faint of heart, because the potential for breaking |
|
1028 | 768 | things is quite high. But it can be a very powerful and useful feature. |
|
1029 | 769 | In a nutshell, you can redefine the way IPython processes the user input |
|
1030 | 770 | line to accept new, special extensions to the syntax without needing to |
|
1031 | 771 | change any of IPython's own code. |
|
1032 | 772 | |
|
1033 | 773 | In the IPython/extensions directory you will find some examples |
|
1034 | 774 | supplied, which we will briefly describe now. These can be used 'as is' |
|
1035 | 775 | (and both provide very useful functionality), or you can use them as a |
|
1036 | 776 | starting point for writing your own extensions. |
|
1037 | 777 | |
|
1038 | 778 | .. _pasting_with_prompts: |
|
1039 | 779 | |
|
1040 | 780 | Pasting of code starting with Python or IPython prompts |
|
1041 | 781 | ------------------------------------------------------- |
|
1042 | 782 | |
|
1043 | 783 | IPython is smart enough to filter out input prompts, be they plain Python ones |
|
1044 | 784 | (``>>>`` and ``...``) or IPython ones (``In [N]:`` and `` ...:``). You can |
|
1045 | 785 | therefore copy and paste from existing interactive sessions without worry. |
|
1046 | 786 | |
|
1047 | 787 | The following is a 'screenshot' of how things work, copying an example from the |
|
1048 | 788 | standard Python tutorial:: |
|
1049 | 789 | |
|
1050 | 790 | In [1]: >>> # Fibonacci series: |
|
1051 | 791 | |
|
1052 | 792 | In [2]: ... # the sum of two elements defines the next |
|
1053 | 793 | |
|
1054 | 794 | In [3]: ... a, b = 0, 1 |
|
1055 | 795 | |
|
1056 | 796 | In [4]: >>> while b < 10: |
|
1057 | 797 | ...: ... print b |
|
1058 | 798 | ...: ... a, b = b, a+b |
|
1059 | 799 | ...: |
|
1060 | 800 | 1 |
|
1061 | 801 | 1 |
|
1062 | 802 | 2 |
|
1063 | 803 | 3 |
|
1064 | 804 | 5 |
|
1065 | 805 | 8 |
|
1066 | 806 | |
|
1067 | 807 | And pasting from IPython sessions works equally well:: |
|
1068 | 808 | |
|
1069 | 809 | In [1]: In [5]: def f(x): |
|
1070 | 810 | ...: ...: "A simple function" |
|
1071 | 811 | ...: ...: return x**2 |
|
1072 | 812 | ...: ...: |
|
1073 | 813 | |
|
1074 | 814 | In [2]: f(3) |
|
1075 | 815 | Out[2]: 9 |
|
1076 | 816 | |
|
1077 | 817 | .. _gui_support: |
|
1078 | 818 | |
|
1079 | 819 | GUI event loop support |
|
1080 | 820 | ====================== |
|
1081 | 821 | |
|
1082 | 822 | .. versionadded:: 0.11 |
|
1083 | 823 | The ``%gui`` magic and :mod:`IPython.lib.inputhook`. |
|
1084 | 824 | |
|
1085 | 825 | IPython has excellent support for working interactively with Graphical User |
|
1086 | 826 | Interface (GUI) toolkits, such as wxPython, PyQt4/PySide, PyGTK and Tk. This is |
|
1087 | 827 | implemented using Python's builtin ``PyOSInputHook`` hook. This implementation |
|
1088 | 828 | is extremely robust compared to our previous thread-based version. The |
|
1089 | 829 | advantages of this are: |
|
1090 | 830 | |
|
1091 | 831 | * GUIs can be enabled and disabled dynamically at runtime. |
|
1092 | 832 | * The active GUI can be switched dynamically at runtime. |
|
1093 | 833 | * In some cases, multiple GUIs can run simultaneously with no problems. |
|
1094 | 834 | * There is a developer API in :mod:`IPython.lib.inputhook` for customizing |
|
1095 | 835 | all of these things. |
|
1096 | 836 | |
|
1097 | 837 | For users, enabling GUI event loop integration is simple. You simple use the |
|
1098 | 838 | ``%gui`` magic as follows:: |
|
1099 | 839 | |
|
1100 | 840 | %gui [GUINAME] |
|
1101 | 841 | |
|
1102 | 842 | With no arguments, ``%gui`` removes all GUI support. Valid ``GUINAME`` |
|
1103 | 843 | arguments are ``wx``, ``qt``, ``gtk`` and ``tk``. |
|
1104 | 844 | |
|
1105 | 845 | Thus, to use wxPython interactively and create a running :class:`wx.App` |
|
1106 | 846 | object, do:: |
|
1107 | 847 | |
|
1108 | 848 | %gui wx |
|
1109 | 849 | |
|
1110 | 850 | For information on IPython's Matplotlib integration (and the ``pylab`` mode) |
|
1111 | 851 | see :ref:`this section <matplotlib_support>`. |
|
1112 | 852 | |
|
1113 | 853 | For developers that want to use IPython's GUI event loop integration in the |
|
1114 | 854 | form of a library, these capabilities are exposed in library form in the |
|
1115 | 855 | :mod:`IPython.lib.inputhook` and :mod:`IPython.lib.guisupport` modules. |
|
1116 | 856 | Interested developers should see the module docstrings for more information, |
|
1117 | 857 | but there are a few points that should be mentioned here. |
|
1118 | 858 | |
|
1119 | 859 | First, the ``PyOSInputHook`` approach only works in command line settings |
|
1120 | 860 | where readline is activated. The integration with various eventloops |
|
1121 | 861 | is handled somewhat differently (and more simply) when using the standalone |
|
1122 | 862 | kernel, as in the qtconsole and notebook. |
|
1123 | 863 | |
|
1124 | 864 | Second, when using the ``PyOSInputHook`` approach, a GUI application should |
|
1125 | 865 | *not* start its event loop. Instead all of this is handled by the |
|
1126 | 866 | ``PyOSInputHook``. This means that applications that are meant to be used both |
|
1127 | 867 | in IPython and as standalone apps need to have special code to detects how the |
|
1128 | 868 | application is being run. We highly recommend using IPython's support for this. |
|
1129 | 869 | Since the details vary slightly between toolkits, we point you to the various |
|
1130 | 870 | examples in our source directory :file:`docs/examples/lib` that demonstrate |
|
1131 | 871 | these capabilities. |
|
1132 | 872 | |
|
1133 | 873 | .. warning:: |
|
1134 | 874 | |
|
1135 | 875 | The WX version of this is currently broken. While ``--pylab=wx`` works |
|
1136 | 876 | fine, standalone WX apps do not. See |
|
1137 | 877 | https://github.com/ipython/ipython/issues/645 for details of our progress on |
|
1138 | 878 | this issue. |
|
1139 | 879 | |
|
1140 | 880 | |
|
1141 | 881 | Third, unlike previous versions of IPython, we no longer "hijack" (replace |
|
1142 | 882 | them with no-ops) the event loops. This is done to allow applications that |
|
1143 | 883 | actually need to run the real event loops to do so. This is often needed to |
|
1144 | 884 | process pending events at critical points. |
|
1145 | 885 | |
|
1146 | 886 | Finally, we also have a number of examples in our source directory |
|
1147 | 887 | :file:`docs/examples/lib` that demonstrate these capabilities. |
|
1148 | 888 | |
|
1149 | 889 | PyQt and PySide |
|
1150 | 890 | --------------- |
|
1151 | 891 | |
|
1152 | 892 | .. attempt at explanation of the complete mess that is Qt support |
|
1153 | 893 | |
|
1154 | 894 | When you use ``--gui=qt`` or ``--pylab=qt``, IPython can work with either |
|
1155 | 895 | PyQt4 or PySide. There are three options for configuration here, because |
|
1156 | 896 | PyQt4 has two APIs for QString and QVariant - v1, which is the default on |
|
1157 | 897 | Python 2, and the more natural v2, which is the only API supported by PySide. |
|
1158 | 898 | v2 is also the default for PyQt4 on Python 3. IPython's code for the QtConsole |
|
1159 | 899 | uses v2, but you can still use any interface in your code, since the |
|
1160 | 900 | Qt frontend is in a different process. |
|
1161 | 901 | |
|
1162 | 902 | The default will be to import PyQt4 without configuration of the APIs, thus |
|
1163 | 903 | matching what most applications would expect. It will fall back of PySide if |
|
1164 | 904 | PyQt4 is unavailable. |
|
1165 | 905 | |
|
1166 | 906 | If specified, IPython will respect the environment variable ``QT_API`` used |
|
1167 | 907 | by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires |
|
1168 | 908 | PyQt4 to use its v2 API. So if ``QT_API=pyside`` PySide will be used, |
|
1169 | 909 | and if ``QT_API=pyqt`` then PyQt4 will be used *with the v2 API* for |
|
1170 | 910 | QString and QVariant, so ETS codes like MayaVi will also work with IPython. |
|
1171 | 911 | |
|
1172 | 912 | If you launch IPython in pylab mode with ``ipython --pylab=qt``, then IPython |
|
1173 | 913 | will ask matplotlib which Qt library to use (only if QT_API is *not set*), via |
|
1174 | 914 | the 'backend.qt4' rcParam. If matplotlib is version 1.0.1 or older, then |
|
1175 | 915 | IPython will always use PyQt4 without setting the v2 APIs, since neither v2 |
|
1176 | 916 | PyQt nor PySide work. |
|
1177 | 917 | |
|
1178 | 918 | .. warning:: |
|
1179 | 919 | |
|
1180 | 920 | Note that this means for ETS 4 to work with PyQt4, ``QT_API`` *must* be set |
|
1181 | 921 | to work with IPython's qt integration, because otherwise PyQt4 will be |
|
1182 | 922 | loaded in an incompatible mode. |
|
1183 | 923 | |
|
1184 | 924 | It also means that you must *not* have ``QT_API`` set if you want to |
|
1185 | 925 | use ``--gui=qt`` with code that requires PyQt4 API v1. |
|
1186 | 926 | |
|
1187 | 927 | |
|
1188 | 928 | .. _matplotlib_support: |
|
1189 | 929 | |
|
1190 | 930 | Plotting with matplotlib |
|
1191 | 931 | ======================== |
|
1192 | 932 | |
|
1193 | 933 | `Matplotlib`_ provides high quality 2D and 3D plotting for Python. Matplotlib |
|
1194 | 934 | can produce plots on screen using a variety of GUI toolkits, including Tk, |
|
1195 | 935 | PyGTK, PyQt4 and wxPython. It also provides a number of commands useful for |
|
1196 | 936 | scientific computing, all with a syntax compatible with that of the popular |
|
1197 | 937 | Matlab program. |
|
1198 | 938 | |
|
1199 | 939 | To start IPython with matplotlib support, use the ``--pylab`` switch. If no |
|
1200 | 940 | arguments are given, IPython will automatically detect your choice of |
|
1201 | 941 | matplotlib backend. You can also request a specific backend with |
|
1202 | 942 | ``--pylab=backend``, where ``backend`` must be one of: 'tk', 'qt', 'wx', 'gtk', |
|
1203 | 943 | 'osx'. |
|
1204 | 944 | |
|
1205 | 945 | .. _Matplotlib: http://matplotlib.sourceforge.net |
|
1206 | 946 | |
|
1207 | 947 | .. _interactive_demos: |
|
1208 | 948 | |
|
1209 | 949 | Interactive demos with IPython |
|
1210 | 950 | ============================== |
|
1211 | 951 | |
|
1212 | 952 | IPython ships with a basic system for running scripts interactively in |
|
1213 | 953 | sections, useful when presenting code to audiences. A few tags embedded |
|
1214 | 954 | in comments (so that the script remains valid Python code) divide a file |
|
1215 | 955 | into separate blocks, and the demo can be run one block at a time, with |
|
1216 | 956 | IPython printing (with syntax highlighting) the block before executing |
|
1217 | 957 | it, and returning to the interactive prompt after each block. The |
|
1218 | 958 | interactive namespace is updated after each block is run with the |
|
1219 | 959 | contents of the demo's namespace. |
|
1220 | 960 | |
|
1221 | 961 | This allows you to show a piece of code, run it and then execute |
|
1222 | 962 | interactively commands based on the variables just created. Once you |
|
1223 | 963 | want to continue, you simply execute the next block of the demo. The |
|
1224 | 964 | following listing shows the markup necessary for dividing a script into |
|
1225 | 965 | sections for execution as a demo: |
|
1226 | 966 | |
|
1227 | 967 | .. literalinclude:: ../../examples/lib/example-demo.py |
|
1228 | 968 | :language: python |
|
1229 | 969 | |
|
1230 | 970 | In order to run a file as a demo, you must first make a Demo object out |
|
1231 | 971 | of it. If the file is named myscript.py, the following code will make a |
|
1232 | 972 | demo:: |
|
1233 | 973 | |
|
1234 | 974 | from IPython.lib.demo import Demo |
|
1235 | 975 | |
|
1236 | 976 | mydemo = Demo('myscript.py') |
|
1237 | 977 | |
|
1238 | 978 | This creates the mydemo object, whose blocks you run one at a time by |
|
1239 | 979 | simply calling the object with no arguments. If you have autocall active |
|
1240 | 980 | in IPython (the default), all you need to do is type:: |
|
1241 | 981 | |
|
1242 | 982 | mydemo |
|
1243 | 983 | |
|
1244 | 984 | and IPython will call it, executing each block. Demo objects can be |
|
1245 | 985 | restarted, you can move forward or back skipping blocks, re-execute the |
|
1246 | 986 | last block, etc. Simply use the Tab key on a demo object to see its |
|
1247 | 987 | methods, and call '?' on them to see their docstrings for more usage |
|
1248 | 988 | details. In addition, the demo module itself contains a comprehensive |
|
1249 | 989 | docstring, which you can access via:: |
|
1250 | 990 | |
|
1251 | 991 | from IPython.lib import demo |
|
1252 | 992 | |
|
1253 | 993 | demo? |
|
1254 | 994 | |
|
1255 | 995 | Limitations: It is important to note that these demos are limited to |
|
1256 | 996 | fairly simple uses. In particular, you cannot break up sections within |
|
1257 | 997 | indented code (loops, if statements, function definitions, etc.) |
|
1258 | 998 | Supporting something like this would basically require tracking the |
|
1259 | 999 | internal execution state of the Python interpreter, so only top-level |
|
1260 | 1000 | divisions are allowed. If you want to be able to open an IPython |
|
1261 | 1001 | instance at an arbitrary point in a program, you can use IPython's |
|
1262 | 1002 | embedding facilities, see :func:`IPython.embed` for details. |
|
1263 | 1003 |
General Comments 0
You need to be logged in to leave comments.
Login now