Show More
@@ -1,29 +1,29 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # encoding: utf-8 |
|
3 | 3 | """ |
|
4 | 4 | A backwards compatibility layer for IPython.Shell. |
|
5 | 5 | |
|
6 | 6 | Previously, IPython had an IPython.Shell module. IPython.Shell has been moved |
|
7 | 7 | to IPython.core.shell and is being refactored. This new module is provided |
|
8 | 8 | for backwards compatability. We strongly encourage everyone to start using |
|
9 | 9 | the new code in IPython.core.shell. |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Copyright (C) 2008-2009 The IPython Development Team |
|
14 | 14 | # |
|
15 | 15 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | 16 | # the file COPYING, distributed as part of this software. |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | from warnings import warn |
|
20 | 20 | |
|
21 | 21 | msg = """ |
|
22 | 22 | This module (IPython.Shell) has been moved to a new location |
|
23 | 23 | (IPython.core.shell) and is being refactored. Please update your code |
|
24 | 24 | to use the new IPython.core.shell module""" |
|
25 | 25 | |
|
26 | 26 | warn(msg, category=DeprecationWarning, stacklevel=1) |
|
27 | 27 | |
|
28 |
from IPython.core.shell import |
|
|
28 | from IPython.core.shell import start, IPShell, IPShellEmbed | |
|
29 | 29 |
@@ -1,131 +1,131 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | |
|
3 | 3 | """An example of how to embed an IPython shell into a running program. |
|
4 | 4 | |
|
5 | 5 | Please see the documentation in the IPython.Shell module for more details. |
|
6 | 6 | |
|
7 | 7 | The accompanying file example-embed-short.py has quick code fragments for |
|
8 | 8 | embedding which you can cut and paste in your code once you understand how |
|
9 | 9 | things work. |
|
10 | 10 | |
|
11 | 11 | The code in this file is deliberately extra-verbose, meant for learning.""" |
|
12 | 12 | |
|
13 | 13 | # The basics to get you going: |
|
14 | 14 | |
|
15 | 15 | # IPython sets the __IPYTHON__ variable so you can know if you have nested |
|
16 | 16 | # copies running. |
|
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 | 20 | try: |
|
21 | 21 | __IPYTHON__ |
|
22 | 22 | except NameError: |
|
23 | 23 | nested = 0 |
|
24 | 24 | args = [''] |
|
25 | 25 | else: |
|
26 | 26 | print "Running nested copies of IPython." |
|
27 | 27 | print "The prompts for the nested copy have been modified" |
|
28 | 28 | nested = 1 |
|
29 | 29 | # what the embedded instance will see as sys.argv: |
|
30 | 30 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', |
|
31 | 31 | '-po','Out<\\#>: ','-nosep'] |
|
32 | 32 | |
|
33 | 33 | # First import the embeddable shell class |
|
34 |
from IPython. |
|
|
34 | from IPython.core.shell import IPShellEmbed | |
|
35 | 35 | |
|
36 | 36 | # Now create an instance of the embeddable shell. The first argument is a |
|
37 | 37 | # string with options exactly as you would type them if you were starting |
|
38 | 38 | # IPython at the system command line. Any parameters you want to define for |
|
39 | 39 | # configuration can thus be specified here. |
|
40 | 40 | ipshell = IPShellEmbed(args, |
|
41 | 41 | banner = 'Dropping into IPython', |
|
42 | 42 | exit_msg = 'Leaving Interpreter, back to program.') |
|
43 | 43 | |
|
44 | 44 | # Make a second instance, you can have as many as you want. |
|
45 | 45 | if nested: |
|
46 | 46 | args[1] = 'In2<\\#>' |
|
47 | 47 | else: |
|
48 | 48 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', |
|
49 | 49 | '-po','Out<\\#>: ','-nosep'] |
|
50 | 50 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') |
|
51 | 51 | |
|
52 | 52 | print '\nHello. This is printed from the main controller program.\n' |
|
53 | 53 | |
|
54 | 54 | # You can then call ipshell() anywhere you need it (with an optional |
|
55 | 55 | # message): |
|
56 | 56 | ipshell('***Called from top level. ' |
|
57 | 57 | 'Hit Ctrl-D to exit interpreter and continue program.\n' |
|
58 | 58 | 'Note that if you use %kill_embedded, you can fully deactivate\n' |
|
59 | 59 | 'This embedded instance so it will never turn on again') |
|
60 | 60 | |
|
61 | 61 | print '\nBack in caller program, moving along...\n' |
|
62 | 62 | |
|
63 | 63 | #--------------------------------------------------------------------------- |
|
64 | 64 | # More details: |
|
65 | 65 | |
|
66 | 66 | # IPShellEmbed instances don't print the standard system banner and |
|
67 | 67 | # messages. The IPython banner (which actually may contain initialization |
|
68 | 68 | # messages) is available as <instance>.IP.BANNER in case you want it. |
|
69 | 69 | |
|
70 | 70 | # IPShellEmbed instances print the following information everytime they |
|
71 | 71 | # start: |
|
72 | 72 | |
|
73 | 73 | # - A global startup banner. |
|
74 | 74 | |
|
75 | 75 | # - A call-specific header string, which you can use to indicate where in the |
|
76 | 76 | # execution flow the shell is starting. |
|
77 | 77 | |
|
78 | 78 | # They also print an exit message every time they exit. |
|
79 | 79 | |
|
80 | 80 | # Both the startup banner and the exit message default to None, and can be set |
|
81 | 81 | # either at the instance constructor or at any other time with the |
|
82 | 82 | # set_banner() and set_exit_msg() methods. |
|
83 | 83 | |
|
84 | 84 | # The shell instance can be also put in 'dummy' mode globally or on a per-call |
|
85 | 85 | # basis. This gives you fine control for debugging without having to change |
|
86 | 86 | # code all over the place. |
|
87 | 87 | |
|
88 | 88 | # The code below illustrates all this. |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | # This is how the global banner and exit_msg can be reset at any point |
|
92 | 92 | ipshell.set_banner('Entering interpreter - New Banner') |
|
93 | 93 | ipshell.set_exit_msg('Leaving interpreter - New exit_msg') |
|
94 | 94 | |
|
95 | 95 | def foo(m): |
|
96 | 96 | s = 'spam' |
|
97 | 97 | ipshell('***In foo(). Try @whos, or print s or m:') |
|
98 | 98 | print 'foo says m = ',m |
|
99 | 99 | |
|
100 | 100 | def bar(n): |
|
101 | 101 | s = 'eggs' |
|
102 | 102 | ipshell('***In bar(). Try @whos, or print s or n:') |
|
103 | 103 | print 'bar says n = ',n |
|
104 | 104 | |
|
105 | 105 | # Some calls to the above functions which will trigger IPython: |
|
106 | 106 | print 'Main program calling foo("eggs")\n' |
|
107 | 107 | foo('eggs') |
|
108 | 108 | |
|
109 | 109 | # The shell can be put in 'dummy' mode where calls to it silently return. This |
|
110 | 110 | # allows you, for example, to globally turn off debugging for a program with a |
|
111 | 111 | # single call. |
|
112 | 112 | ipshell.set_dummy_mode(1) |
|
113 | 113 | print '\nTrying to call IPython which is now "dummy":' |
|
114 | 114 | ipshell() |
|
115 | 115 | print 'Nothing happened...' |
|
116 | 116 | # The global 'dummy' mode can still be overridden for a single call |
|
117 | 117 | print '\nOverriding dummy mode manually:' |
|
118 | 118 | ipshell(dummy=0) |
|
119 | 119 | |
|
120 | 120 | # Reactivate the IPython shell |
|
121 | 121 | ipshell.set_dummy_mode(0) |
|
122 | 122 | |
|
123 | 123 | print 'You can even have multiple embedded instances:' |
|
124 | 124 | ipshell2() |
|
125 | 125 | |
|
126 | 126 | print '\nMain program calling bar("spam")\n' |
|
127 | 127 | bar('spam') |
|
128 | 128 | |
|
129 | 129 | print 'Main program finished. Bye!' |
|
130 | 130 | |
|
131 | 131 | #********************** End of file <example-embed.py> *********************** |
General Comments 0
You need to be logged in to leave comments.
Login now