Show More
@@ -8,29 +8,28 cut and paste use once you understand how to use the system.""" | |||
|
8 | 8 | # embedded in another IPython session (helps avoid confusion) |
|
9 | 9 | |
|
10 | 10 | try: |
|
11 | __IPYTHON__ | |
|
11 | get_ipython | |
|
12 | 12 | except NameError: |
|
13 | argv = [''] | |
|
14 | banner = exit_msg = '' | |
|
13 | banner=exit_msg='' | |
|
15 | 14 | else: |
|
16 | # Command-line options for IPython (a list like sys.argv) | |
|
17 | argv = ['-pi1','In <\\#>:','-pi2',' .\\D.:','-po','Out<\\#>:'] | |
|
18 | 15 | banner = '*** Nested interpreter ***' |
|
19 | 16 | exit_msg = '*** Back in main IPython ***' |
|
20 | 17 | |
|
21 |
# First import the embed |
|
|
22 |
from IPython. |
|
|
18 | # First import the embed function | |
|
19 | from IPython.frontend.terminal.embed import InteractiveShellEmbed | |
|
23 | 20 | # Now create the IPython shell instance. Put ipshell() anywhere in your code |
|
24 | 21 | # where you want it to open. |
|
25 |
ipshell = I |
|
|
22 | ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg) | |
|
26 | 23 | |
|
27 | 24 | #--------------------------------------------------------------------------- |
|
28 | 25 | # This code will load an embeddable IPython shell always with no changes for |
|
29 | 26 | # nested embededings. |
|
30 | 27 | |
|
31 | from IPython.Shell import IPShellEmbed | |
|
32 | ipshell = IPShellEmbed() | |
|
33 | # Now ipshell() will open IPython anywhere in the code. | |
|
28 | # This code will load an embeddable IPython shell always with no changes for | |
|
29 | # nested embededings. | |
|
30 | ||
|
31 | from IPython import embed | |
|
32 | # Now embed() will open IPython anywhere in the code. | |
|
34 | 33 | |
|
35 | 34 | #--------------------------------------------------------------------------- |
|
36 | 35 | # This code loads an embeddable shell only if NOT running inside |
@@ -38,10 +37,10 ipshell = IPShellEmbed() | |||
|
38 | 37 | # dummy function. |
|
39 | 38 | |
|
40 | 39 | try: |
|
41 | __IPYTHON__ | |
|
40 | get_ipython | |
|
42 | 41 | except NameError: |
|
43 |
from IPython. |
|
|
44 |
ipshell = I |
|
|
42 | from IPython.frontend.terminal.embed import InteractiveShellEmbed | |
|
43 | ipshell = InteractiveShellEmbed() | |
|
45 | 44 | # Now ipshell() will open IPython anywhere in the code |
|
46 | 45 | else: |
|
47 | 46 | # Define a dummy ipshell() so the same code doesn't crash inside an |
@@ -17,37 +17,43 The code in this file is deliberately extra-verbose, meant for learning.""" | |||
|
17 | 17 | |
|
18 | 18 | # Try running this code both at the command line and from inside IPython (with |
|
19 | 19 | # %run example-embed.py) |
|
20 | from IPython.config.loader import Config | |
|
20 | 21 | try: |
|
21 | __IPYTHON__ | |
|
22 | get_ipython | |
|
22 | 23 | except NameError: |
|
23 | 24 | nested = 0 |
|
24 | args = [''] | |
|
25 | cfg = Config() | |
|
26 | shell_config = cfg.InteractiveShellEmbed | |
|
27 | shell_config.prompt_in1 = 'In <\\#>: ' | |
|
28 | shell_config.prompt_in2 = ' .\\D.: ' | |
|
29 | shell_config.prompt_out = 'Out<\\#>: ' | |
|
25 | 30 | else: |
|
26 | 31 | print "Running nested copies of IPython." |
|
27 | 32 | print "The prompts for the nested copy have been modified" |
|
33 | cfg = Config() | |
|
28 | 34 | nested = 1 |
|
29 | # what the embedded instance will see as sys.argv: | |
|
30 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', | |
|
31 | '-po','Out<\\#>: ','-nosep'] | |
|
32 | 35 | |
|
33 | 36 | # First import the embeddable shell class |
|
34 |
from IPython. |
|
|
37 | from IPython.frontend.terminal.embed import InteractiveShellEmbed | |
|
35 | 38 | |
|
36 | 39 | # Now create an instance of the embeddable shell. The first argument is a |
|
37 | 40 | # string with options exactly as you would type them if you were starting |
|
38 | 41 | # IPython at the system command line. Any parameters you want to define for |
|
39 | 42 | # configuration can thus be specified here. |
|
40 |
ipshell = I |
|
|
41 | banner = 'Dropping into IPython', | |
|
43 | ipshell = InteractiveShellEmbed(config=cfg, | |
|
44 | banner1 = 'Dropping into IPython', | |
|
42 | 45 | exit_msg = 'Leaving Interpreter, back to program.') |
|
43 | 46 | |
|
44 | 47 | # Make a second instance, you can have as many as you want. |
|
45 | if nested: | |
|
46 | args[1] = 'In2<\\#>' | |
|
47 | else: | |
|
48 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', | |
|
49 | '-po','Out<\\#>: ','-nosep'] | |
|
50 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') | |
|
48 | cfg2 = cfg.copy() | |
|
49 | shell_config = cfg2.InteractiveShellEmbed | |
|
50 | shell_config.prompt_in1 = 'In2<\\#>: ' | |
|
51 | if not nested: | |
|
52 | shell_config.prompt_in1 = 'In2<\\#>: ' | |
|
53 | shell_config.prompt_in2 = ' .\\D.: ' | |
|
54 | shell_config.prompt_out = 'Out<\\#>: ' | |
|
55 | ipshell2 = InteractiveShellEmbed(config=cfg, | |
|
56 | banner1 = 'Second IPython instance.') | |
|
51 | 57 | |
|
52 | 58 | print '\nHello. This is printed from the main controller program.\n' |
|
53 | 59 | |
@@ -63,11 +69,11 print '\nBack in caller program, moving along...\n' | |||
|
63 | 69 | #--------------------------------------------------------------------------- |
|
64 | 70 | # More details: |
|
65 | 71 | |
|
66 |
# I |
|
|
72 | # InteractiveShellEmbed instances don't print the standard system banner and | |
|
67 | 73 | # messages. The IPython banner (which actually may contain initialization |
|
68 |
# messages) is available as |
|
|
74 | # messages) is available as get_ipython().banner in case you want it. | |
|
69 | 75 | |
|
70 |
# I |
|
|
76 | # InteractiveShellEmbed instances print the following information everytime they | |
|
71 | 77 | # start: |
|
72 | 78 | |
|
73 | 79 | # - A global startup banner. |
@@ -79,7 +85,7 print '\nBack in caller program, moving along...\n' | |||
|
79 | 85 | |
|
80 | 86 | # Both the startup banner and the exit message default to None, and can be set |
|
81 | 87 | # either at the instance constructor or at any other time with the |
|
82 |
# |
|
|
88 | # by setting the banner and exit_msg attributes. | |
|
83 | 89 | |
|
84 | 90 | # The shell instance can be also put in 'dummy' mode globally or on a per-call |
|
85 | 91 | # basis. This gives you fine control for debugging without having to change |
@@ -89,17 +95,17 print '\nBack in caller program, moving along...\n' | |||
|
89 | 95 | |
|
90 | 96 | |
|
91 | 97 | # This is how the global banner and exit_msg can be reset at any point |
|
92 |
ipshell. |
|
|
93 |
ipshell. |
|
|
98 | ipshell.banner = 'Entering interpreter - New Banner' | |
|
99 | ipshell.exit_msg = 'Leaving interpreter - New exit_msg' | |
|
94 | 100 | |
|
95 | 101 | def foo(m): |
|
96 | 102 | s = 'spam' |
|
97 |
ipshell('***In foo(). Try |
|
|
103 | ipshell('***In foo(). Try %whos, or print s or m:') | |
|
98 | 104 | print 'foo says m = ',m |
|
99 | 105 | |
|
100 | 106 | def bar(n): |
|
101 | 107 | s = 'eggs' |
|
102 |
ipshell('***In bar(). Try |
|
|
108 | ipshell('***In bar(). Try %whos, or print s or n:') | |
|
103 | 109 | print 'bar says n = ',n |
|
104 | 110 | |
|
105 | 111 | # Some calls to the above functions which will trigger IPython: |
@@ -109,16 +115,16 foo('eggs') | |||
|
109 | 115 | # The shell can be put in 'dummy' mode where calls to it silently return. This |
|
110 | 116 | # allows you, for example, to globally turn off debugging for a program with a |
|
111 | 117 | # single call. |
|
112 |
ipshell. |
|
|
118 | ipshell.dummy_mode = True | |
|
113 | 119 | print '\nTrying to call IPython which is now "dummy":' |
|
114 | 120 | ipshell() |
|
115 | 121 | print 'Nothing happened...' |
|
116 | 122 | # The global 'dummy' mode can still be overridden for a single call |
|
117 | 123 | print '\nOverriding dummy mode manually:' |
|
118 |
ipshell(dummy= |
|
|
124 | ipshell(dummy=False) | |
|
119 | 125 | |
|
120 | 126 | # Reactivate the IPython shell |
|
121 |
ipshell. |
|
|
127 | ipshell.dummy_mode = False | |
|
122 | 128 | |
|
123 | 129 | print 'You can even have multiple embedded instances:' |
|
124 | 130 | ipshell2() |
@@ -6,7 +6,7 from IPython import embed | |||
|
6 | 6 | a = 10 |
|
7 | 7 | b = 20 |
|
8 | 8 | |
|
9 | embed('First time') | |
|
9 | embed(header='First time', banner1='') | |
|
10 | 10 | |
|
11 | 11 | c = 30 |
|
12 | 12 | d = 40 |
@@ -14,4 +14,4 d = 40 | |||
|
14 | 14 | try: |
|
15 | 15 | raise Exception('adsfasdf') |
|
16 | 16 | except: |
|
17 | embed('The second time') | |
|
17 | embed(header='The second time') |
@@ -166,9 +166,9 All options with a [no] prepended can be specified in negated form | |||
|
166 | 166 | logfile as a file to be executed with option -logplay (see |
|
167 | 167 | below). |
|
168 | 168 | |
|
169 |
|
|
|
169 | logfile=<name> specify the name of your logfile. | |
|
170 | 170 | |
|
171 |
|
|
|
171 | logplay=<name> | |
|
172 | 172 | |
|
173 | 173 | you can replay a previous log. For restoring a session as close as |
|
174 | 174 | possible to the state you left it in, use this option (don't just run |
@@ -201,10 +201,6 All options with a [no] prepended can be specified in negated form | |||
|
201 | 201 | IPython or in code called by it) which triggers an exception |
|
202 | 202 | which goes uncaught. |
|
203 | 203 | |
|
204 | --pydb | |
|
205 | Makes IPython use the third party "pydb" package as debugger, | |
|
206 | instead of pdb. Requires that pydb is installed. | |
|
207 | ||
|
208 | 204 | --[no-]pprint |
|
209 | 205 | ipython can optionally use the pprint (pretty printer) module |
|
210 | 206 | for displaying results. pprint tends to give a nicer display |
@@ -213,24 +209,24 All options with a [no] prepended can be specified in negated form | |||
|
213 | 209 | |
|
214 | 210 | profile=<name> |
|
215 | 211 | |
|
216 | assume that your config file is ipythonrc-<name> or | |
|
217 | ipy_profile_<name>.py (looks in current dir first, then in | |
|
218 |
|
|
|
212 | Select the IPython profile by name. | |
|
213 | ||
|
214 | This is a quick way to keep and load multiple | |
|
219 | 215 | config files for different tasks, especially if you use the |
|
220 | 216 | include option of config files. You can keep a basic |
|
221 | 217 | IPYTHON_DIR/ipythonrc file and then have other 'profiles' which |
|
222 | 218 | include this one and load extra things for particular |
|
223 | 219 | tasks. For example: |
|
224 | 220 | |
|
225 |
1. $IPYTHON_DIR/ |
|
|
226 |
2. $IPYTHON_DIR/ |
|
|
227 |
3. $IPYTHON_DIR/ |
|
|
221 | 1. $IPYTHON_DIR/profile_default : load basic things you always want. | |
|
222 | 2. $IPYTHON_DIR/profile_math : load (1) and basic math-related modules. | |
|
223 | 3. $IPYTHON_DIR/profile_numeric : load (1) and Numeric and plotting modules. | |
|
228 | 224 | |
|
229 | 225 | Since it is possible to create an endless loop by having |
|
230 | 226 | circular file inclusions, IPython will stop if it reaches 15 |
|
231 | 227 | recursive inclusions. |
|
232 | 228 | |
|
233 | pi1=<string> | |
|
229 | InteractiveShell.prompt_in1=<string> | |
|
234 | 230 | |
|
235 | 231 | Specify the string used for input prompts. Note that if you are using |
|
236 | 232 | numbered prompts, the number is represented with a '\#' in the |
@@ -239,7 +235,7 All options with a [no] prepended can be specified in negated form | |||
|
239 | 235 | discusses in detail all the available escapes to customize your |
|
240 | 236 | prompts. |
|
241 | 237 | |
|
242 | pi2=<string> | |
|
238 | InteractiveShell.prompt_in2=<string> | |
|
243 | 239 | Similar to the previous option, but used for the continuation |
|
244 | 240 | prompts. The special sequence '\D' is similar to '\#', but |
|
245 | 241 | with all digits replaced dots (so you can have your |
@@ -247,7 +243,7 All options with a [no] prepended can be specified in negated form | |||
|
247 | 243 | ' .\D.:' (note three spaces at the start for alignment with |
|
248 | 244 | 'In [\#]'). |
|
249 | 245 | |
|
250 | po=<string> | |
|
246 | InteractiveShell.prompt_out=<string> | |
|
251 | 247 | String used for output prompts, also uses numbers like |
|
252 | 248 | prompt_in1. Default: 'Out[\#]:' |
|
253 | 249 | |
@@ -272,7 +268,7 All options with a [no] prepended can be specified in negated form | |||
|
272 | 268 | IPython's readline and syntax coloring fine, only 'emacs' (M-x |
|
273 | 269 | shell and C-c !) buffers do not. |
|
274 | 270 | |
|
275 | sl=<n> | |
|
271 | TerminalInteractiveShell.screen_length=<n> | |
|
276 | 272 | number of lines of your screen. This is used to control |
|
277 | 273 | printing of very long strings. Strings longer than this number |
|
278 | 274 | of lines will be sent through a pager instead of directly |
@@ -285,34 +281,31 All options with a [no] prepended can be specified in negated form | |||
|
285 | 281 | reason this isn't working well (it needs curses support), specify |
|
286 | 282 | it yourself. Otherwise don't change the default. |
|
287 | 283 | |
|
288 | si=<string> | |
|
284 | TerminalInteractiveShell.separate_in=<string> | |
|
289 | 285 | |
|
290 | 286 | separator before input prompts. |
|
291 | 287 | Default: '\n' |
|
292 | 288 | |
|
293 | so=<string> | |
|
289 | TerminalInteractiveShell.separate_out=<string> | |
|
294 | 290 | separator before output prompts. |
|
295 | 291 | Default: nothing. |
|
296 | 292 | |
|
297 | so2=<string> | |
|
293 | TerminalInteractiveShell.separate_out2=<string> | |
|
298 | 294 | separator after output prompts. |
|
299 | 295 | Default: nothing. |
|
300 | 296 | For these three options, use the value 0 to specify no separator. |
|
301 | 297 | |
|
302 | 298 | --nosep |
|
303 | shorthand for '-SeparateIn 0 -SeparateOut 0 -SeparateOut2 | |
|
304 | 0'. Simply removes all input/output separators. | |
|
299 | shorthand for setting the above separators to empty strings. | |
|
300 | ||
|
301 | Simply removes all input/output separators. | |
|
305 | 302 | |
|
306 | 303 | --init |
|
307 |
allows you to initialize |
|
|
308 |
install a new version of IPython |
|
|
309 |
include new command line options or example |
|
|
310 | updated config files. However, it backs up (with a | |
|
311 | .old extension) all files which it overwrites so that you can | |
|
312 | merge back any customizations you might have in your personal | |
|
313 | files. Note that you should probably use %upgrade instead, | |
|
314 | it's a safer alternative. | |
|
315 | ||
|
304 | allows you to initialize a profile dir for configuration when you | |
|
305 | install a new version of IPython or want to use a new profile. | |
|
306 | Since new versions may include new command line options or example | |
|
307 | files, this copies updated config files. Note that you should probably | |
|
308 | use %upgrade instead,it's a safer alternative. | |
|
316 | 309 | |
|
317 | 310 | --version print version information and exit. |
|
318 | 311 | |
@@ -337,11 +330,7 All options with a [no] prepended can be specified in negated form | |||
|
337 | 330 | Interactive use |
|
338 | 331 | =============== |
|
339 | 332 | |
|
340 | Warning: IPython relies on the existence of a global variable called | |
|
341 | _ip which controls the shell itself. If you redefine _ip to anything, | |
|
342 | bizarre behavior will quickly occur. | |
|
343 | ||
|
344 | Other than the above warning, IPython is meant to work as a drop-in | |
|
333 | IPython is meant to work as a drop-in | |
|
345 | 334 | replacement for the standard interactive interpreter. As such, any code |
|
346 | 335 | which is valid python should execute normally under IPython (cases where |
|
347 | 336 | this is not true should be reported as bugs). It does, however, offer |
@@ -384,7 +373,9 an identifier with the same name as an existing magic function will | |||
|
384 | 373 | shadow it for automagic use. You can still access the shadowed magic |
|
385 | 374 | function by explicitly using the % character at the beginning of the line. |
|
386 | 375 | |
|
387 |
An example (with automagic on) should clarify all this: |
|
|
376 | An example (with automagic on) should clarify all this: | |
|
377 | ||
|
378 | .. sourcecode:: ipython | |
|
388 | 379 | |
|
389 | 380 | In [1]: cd ipython # %cd is called by automagic |
|
390 | 381 | |
@@ -415,11 +406,11 An example (with automagic on) should clarify all this:: | |||
|
415 | 406 | /home/fperez/ipython |
|
416 | 407 | |
|
417 | 408 | You can define your own magic functions to extend the system. The |
|
418 |
following example defines a new magic command, %impall: |
|
|
409 | following example defines a new magic command, %impall: | |
|
419 | 410 | |
|
420 | import IPython.ipapi | |
|
411 | .. sourcecode:: python | |
|
421 | 412 | |
|
422 |
ip = |
|
|
413 | ip = get_ipython() | |
|
423 | 414 | |
|
424 | 415 | def doimp(self, arg): |
|
425 | 416 | |
@@ -433,20 +424,13 following example defines a new magic command, %impall:: | |||
|
433 | 424 | |
|
434 | 425 | ip.expose_magic('impall', doimp) |
|
435 | 426 | |
|
436 | You can also define your own aliased names for magic functions. In your | |
|
437 | ipythonrc file, placing a line like:: | |
|
438 | ||
|
439 | execute __IP.magic_cl = __IP.magic_clear | |
|
440 | ||
|
441 | will define %cl as a new name for %clear. | |
|
442 | ||
|
443 | 427 | Type %magic for more information, including a list of all available |
|
444 | 428 | magic functions at any time and their docstrings. You can also type |
|
445 | 429 | %magic_function_name? (see sec. 6.4 <#sec:dyn-object-info> for |
|
446 | 430 | information on the '?' system) to get information about any particular |
|
447 | 431 | magic function you are interested in. |
|
448 | 432 | |
|
449 |
The API documentation for the :mod:`IPython. |
|
|
433 | The API documentation for the :mod:`IPython.core.magic` module contains the full | |
|
450 | 434 | docstrings of all currently available magic commands. |
|
451 | 435 | |
|
452 | 436 | |
@@ -543,8 +527,7 Persistent command history across sessions | |||
|
543 | 527 | |
|
544 | 528 | IPython will save your input history when it leaves and reload it next |
|
545 | 529 | time you restart it. By default, the history file is named |
|
546 | $IPYTHON_DIR/history, but if you've loaded a named profile, | |
|
547 | '-PROFILE_NAME' is appended to the name. This allows you to keep | |
|
530 | $IPYTHON_DIR/profile_<name>/history.sqlite. This allows you to keep | |
|
548 | 531 | separate histories related to various tasks: commands related to |
|
549 | 532 | numerical work will not be clobbered by a system shell history, for |
|
550 | 533 | example. |
@@ -568,11 +551,17 more convenient (M-i indents, M-u unindents):: | |||
|
568 | 551 | |
|
569 | 552 | Note that there are 4 spaces between the quote marks after "M-i" above. |
|
570 | 553 | |
|
571 | Warning: this feature is ON by default, but it can cause problems with | |
|
572 | the pasting of multi-line indented code (the pasted code gets | |
|
573 | re-indented on each line). A magic function %autoindent allows you to | |
|
574 | toggle it on/off at runtime. You can also disable it permanently on in | |
|
575 | your ipythonrc file (set autoindent 0). | |
|
554 | .. warning:: | |
|
555 | ||
|
556 | Setting the above indents will cause problems with unicode text entry in the terminal. | |
|
557 | ||
|
558 | .. warning:: | |
|
559 | ||
|
560 | This feature is ON by default, but it can cause problems with | |
|
561 | the pasting of multi-line indented code (the pasted code gets | |
|
562 | re-indented on each line). A magic function %autoindent allows you to | |
|
563 | toggle it on/off at runtime. You can also disable it permanently on in | |
|
564 | your :file:`ipython_config.py` file (set TerminalInteractiveShell.autoindent=False). | |
|
576 | 565 | |
|
577 | 566 | |
|
578 | 567 | Customizing readline behavior |
@@ -967,11 +956,12 Python honors the environment variable PYTHONSTARTUP and will execute at | |||
|
967 | 956 | startup the file referenced by this variable. If you put at the end of |
|
968 | 957 | this file the following two lines of code:: |
|
969 | 958 | |
|
970 | import IPython | |
|
971 | IPython.Shell.IPShell().mainloop(sys_exit=1) | |
|
959 | from IPython.frontend.terminal.ipapp import launch_new_instance | |
|
960 | launch_new_instance() | |
|
961 | raise SystemExit | |
|
972 | 962 | |
|
973 | 963 | then IPython will be your working environment anytime you start Python. |
|
974 |
The |
|
|
964 | The ``raise SystemExit`` is needed to exit Python when | |
|
975 | 965 | it finishes, otherwise you'll be back at the normal Python '>>>' |
|
976 | 966 | prompt. |
|
977 | 967 | |
@@ -1009,11 +999,9 needed). | |||
|
1009 | 999 | The following code snippet is the bare minimum you need to include in |
|
1010 | 1000 | your Python programs for this to work (detailed examples follow later):: |
|
1011 | 1001 | |
|
1012 |
from IPython |
|
|
1002 | from IPython import embed | |
|
1013 | 1003 | |
|
1014 | ipshell = IPShellEmbed() | |
|
1015 | ||
|
1016 | ipshell() # this call anywhere in your program will start IPython | |
|
1004 | embed() # this call anywhere in your program will start IPython | |
|
1017 | 1005 | |
|
1018 | 1006 | You can run embedded instances even in code which is itself being run at |
|
1019 | 1007 | the IPython interactive prompt with '%run <filename>'. Since it's easy |
@@ -1027,13 +1015,14 them separately, for example with different options for data | |||
|
1027 | 1015 | presentation. If you close and open the same instance multiple times, |
|
1028 | 1016 | its prompt counters simply continue from each execution to the next. |
|
1029 | 1017 | |
|
1030 | Please look at the docstrings in the Shell.py module for more details on | |
|
1031 | the use of this system. | |
|
1018 | Please look at the docstrings in the :mod:`~IPython.frontend.terminal.embed` | |
|
1019 | module for more details on the use of this system. | |
|
1032 | 1020 | |
|
1033 | 1021 | The following sample file illustrating how to use the embedding |
|
1034 | 1022 | functionality is provided in the examples directory as example-embed.py. |
|
1035 |
It should be fairly self-explanatory: |
|
|
1023 | It should be fairly self-explanatory: | |
|
1036 | 1024 | |
|
1025 | .. sourcecode:: python | |
|
1037 | 1026 | |
|
1038 | 1027 | #!/usr/bin/env python |
|
1039 | 1028 | |
@@ -1054,37 +1043,43 It should be fairly self-explanatory:: | |||
|
1054 | 1043 | |
|
1055 | 1044 | # Try running this code both at the command line and from inside IPython (with |
|
1056 | 1045 | # %run example-embed.py) |
|
1046 | from IPython.config.loader import Config | |
|
1057 | 1047 | try: |
|
1058 | __IPYTHON__ | |
|
1048 | get_ipython | |
|
1059 | 1049 | except NameError: |
|
1060 | 1050 | nested = 0 |
|
1061 | args = [''] | |
|
1051 | cfg = Config() | |
|
1052 | shell_config = cfg.InteractiveShellEmbed | |
|
1053 | shell_config.prompt_in1 = 'In <\\#>: ' | |
|
1054 | shell_config.prompt_in2 = ' .\\D.: ' | |
|
1055 | shell_config.prompt_out = 'Out<\\#>: ' | |
|
1062 | 1056 | else: |
|
1063 | 1057 | print "Running nested copies of IPython." |
|
1064 | 1058 | print "The prompts for the nested copy have been modified" |
|
1059 | cfg = Config() | |
|
1065 | 1060 | nested = 1 |
|
1066 | # what the embedded instance will see as sys.argv: | |
|
1067 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', | |
|
1068 | '-po','Out<\\#>: ','-nosep'] | |
|
1069 | 1061 | |
|
1070 | 1062 | # First import the embeddable shell class |
|
1071 |
from IPython. |
|
|
1063 | from IPython.frontend.terminal.embed import InteractiveShellEmbed | |
|
1072 | 1064 | |
|
1073 | 1065 | # Now create an instance of the embeddable shell. The first argument is a |
|
1074 | 1066 | # string with options exactly as you would type them if you were starting |
|
1075 | 1067 | # IPython at the system command line. Any parameters you want to define for |
|
1076 | 1068 | # configuration can thus be specified here. |
|
1077 |
ipshell = I |
|
|
1078 | banner = 'Dropping into IPython', | |
|
1069 | ipshell = InteractiveShellEmbed(config=cfg, | |
|
1070 | banner1 = 'Dropping into IPython', | |
|
1079 | 1071 | exit_msg = 'Leaving Interpreter, back to program.') |
|
1080 | 1072 | |
|
1081 | 1073 | # Make a second instance, you can have as many as you want. |
|
1082 | if nested: | |
|
1083 | args[1] = 'In2<\\#>' | |
|
1084 | else: | |
|
1085 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', | |
|
1086 | '-po','Out<\\#>: ','-nosep'] | |
|
1087 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') | |
|
1074 | cfg2 = cfg.copy() | |
|
1075 | shell_config = cfg2.InteractiveShellEmbed | |
|
1076 | shell_config.prompt_in1 = 'In2<\\#>: ' | |
|
1077 | if not nested: | |
|
1078 | shell_config.prompt_in1 = 'In2<\\#>: ' | |
|
1079 | shell_config.prompt_in2 = ' .\\D.: ' | |
|
1080 | shell_config.prompt_out = 'Out<\\#>: ' | |
|
1081 | ipshell2 = InteractiveShellEmbed(config=cfg, | |
|
1082 | banner1 = 'Second IPython instance.') | |
|
1088 | 1083 | |
|
1089 | 1084 | print '\nHello. This is printed from the main controller program.\n' |
|
1090 | 1085 | |
@@ -1100,11 +1095,11 It should be fairly self-explanatory:: | |||
|
1100 | 1095 | #--------------------------------------------------------------------------- |
|
1101 | 1096 | # More details: |
|
1102 | 1097 | |
|
1103 |
# I |
|
|
1098 | # InteractiveShellEmbed instances don't print the standard system banner and | |
|
1104 | 1099 | # messages. The IPython banner (which actually may contain initialization |
|
1105 |
# messages) is available as |
|
|
1100 | # messages) is available as get_ipython().banner in case you want it. | |
|
1106 | 1101 | |
|
1107 |
# I |
|
|
1102 | # InteractiveShellEmbed instances print the following information everytime they | |
|
1108 | 1103 | # start: |
|
1109 | 1104 | |
|
1110 | 1105 | # - A global startup banner. |
@@ -1116,7 +1111,7 It should be fairly self-explanatory:: | |||
|
1116 | 1111 | |
|
1117 | 1112 | # Both the startup banner and the exit message default to None, and can be set |
|
1118 | 1113 | # either at the instance constructor or at any other time with the |
|
1119 |
# |
|
|
1114 | # by setting the banner and exit_msg attributes. | |
|
1120 | 1115 | |
|
1121 | 1116 | # The shell instance can be also put in 'dummy' mode globally or on a per-call |
|
1122 | 1117 | # basis. This gives you fine control for debugging without having to change |
@@ -1126,17 +1121,17 It should be fairly self-explanatory:: | |||
|
1126 | 1121 | |
|
1127 | 1122 | |
|
1128 | 1123 | # This is how the global banner and exit_msg can be reset at any point |
|
1129 |
ipshell. |
|
|
1130 |
ipshell. |
|
|
1124 | ipshell.banner = 'Entering interpreter - New Banner' | |
|
1125 | ipshell.exit_msg = 'Leaving interpreter - New exit_msg' | |
|
1131 | 1126 | |
|
1132 | 1127 | def foo(m): |
|
1133 | 1128 | s = 'spam' |
|
1134 |
ipshell('***In foo(). Try |
|
|
1129 | ipshell('***In foo(). Try %whos, or print s or m:') | |
|
1135 | 1130 | print 'foo says m = ',m |
|
1136 | 1131 | |
|
1137 | 1132 | def bar(n): |
|
1138 | 1133 | s = 'eggs' |
|
1139 |
ipshell('***In bar(). Try |
|
|
1134 | ipshell('***In bar(). Try %whos, or print s or n:') | |
|
1140 | 1135 | print 'bar says n = ',n |
|
1141 | 1136 | |
|
1142 | 1137 | # Some calls to the above functions which will trigger IPython: |
@@ -1146,16 +1141,16 It should be fairly self-explanatory:: | |||
|
1146 | 1141 | # The shell can be put in 'dummy' mode where calls to it silently return. This |
|
1147 | 1142 | # allows you, for example, to globally turn off debugging for a program with a |
|
1148 | 1143 | # single call. |
|
1149 |
ipshell. |
|
|
1144 | ipshell.dummy_mode = True | |
|
1150 | 1145 | print '\nTrying to call IPython which is now "dummy":' |
|
1151 | 1146 | ipshell() |
|
1152 | 1147 | print 'Nothing happened...' |
|
1153 | 1148 | # The global 'dummy' mode can still be overridden for a single call |
|
1154 | 1149 | print '\nOverriding dummy mode manually:' |
|
1155 |
ipshell(dummy= |
|
|
1150 | ipshell(dummy=False) | |
|
1156 | 1151 | |
|
1157 | 1152 | # Reactivate the IPython shell |
|
1158 |
ipshell. |
|
|
1153 | ipshell.dummy_mode = False | |
|
1159 | 1154 | |
|
1160 | 1155 | print 'You can even have multiple embedded instances:' |
|
1161 | 1156 | ipshell2() |
@@ -1168,8 +1163,9 It should be fairly self-explanatory:: | |||
|
1168 | 1163 | #********************** End of file <example-embed.py> *********************** |
|
1169 | 1164 | |
|
1170 | 1165 | Once you understand how the system functions, you can use the following |
|
1171 |
code fragments in your programs which are ready for cut and paste: |
|
|
1166 | code fragments in your programs which are ready for cut and paste: | |
|
1172 | 1167 | |
|
1168 | .. sourcecode:: python | |
|
1173 | 1169 | |
|
1174 | 1170 | """Quick code snippets for embedding IPython into other programs. |
|
1175 | 1171 | |
@@ -1181,7 +1177,7 code fragments in your programs which are ready for cut and paste:: | |||
|
1181 | 1177 | # embedded in another IPython session (helps avoid confusion) |
|
1182 | 1178 | |
|
1183 | 1179 | try: |
|
1184 | __IPYTHON__ | |
|
1180 | get_ipython | |
|
1185 | 1181 | except NameError: |
|
1186 | 1182 | argv = [''] |
|
1187 | 1183 | banner = exit_msg = '' |
@@ -1201,9 +1197,8 code fragments in your programs which are ready for cut and paste:: | |||
|
1201 | 1197 | # This code will load an embeddable IPython shell always with no changes for |
|
1202 | 1198 | # nested embededings. |
|
1203 | 1199 | |
|
1204 |
from IPython |
|
|
1205 | ipshell = IPShellEmbed() | |
|
1206 | # Now ipshell() will open IPython anywhere in the code. | |
|
1200 | from IPython import embed | |
|
1201 | # Now embed() will open IPython anywhere in the code. | |
|
1207 | 1202 | |
|
1208 | 1203 | #--------------------------------------------------------------------------- |
|
1209 | 1204 | # This code loads an embeddable shell only if NOT running inside |
@@ -1211,15 +1206,14 code fragments in your programs which are ready for cut and paste:: | |||
|
1211 | 1206 | # dummy function. |
|
1212 | 1207 | |
|
1213 | 1208 | try: |
|
1214 | __IPYTHON__ | |
|
1209 | get_ipython | |
|
1215 | 1210 | except NameError: |
|
1216 |
from IPython. |
|
|
1217 | ipshell = IPShellEmbed() | |
|
1218 | # Now ipshell() will open IPython anywhere in the code | |
|
1211 | from IPython.frontend.terminal.embed import embed | |
|
1212 | # Now embed() will open IPython anywhere in the code | |
|
1219 | 1213 | else: |
|
1220 |
# Define a dummy |
|
|
1214 | # Define a dummy embed() so the same code doesn't crash inside an | |
|
1221 | 1215 | # interactive IPython |
|
1222 |
def |
|
|
1216 | def embed(): pass | |
|
1223 | 1217 | |
|
1224 | 1218 | #******************* End of file <example-embed-short.py> ******************** |
|
1225 | 1219 | |
@@ -1266,7 +1260,7 the origin of the problem. | |||
|
1266 | 1260 | Furthermore, you can use these debugging facilities both with the |
|
1267 | 1261 | embedded IPython mode and without IPython at all. For an embedded shell |
|
1268 | 1262 | (see sec. Embedding_), simply call the constructor with |
|
1269 | '-pdb' in the argument string and automatically pdb will be called if an | |
|
1263 | '--pdb' in the argument string and automatically pdb will be called if an | |
|
1270 | 1264 | uncaught exception is triggered by your code. |
|
1271 | 1265 | |
|
1272 | 1266 | For stand-alone use of the feature in your programs which do not use |
General Comments 0
You need to be logged in to leave comments.
Login now