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