Show More
@@ -1,137 +1,138 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 | from __future__ import print_function | |
|
12 | 13 | |
|
13 | 14 | # The basics to get you going: |
|
14 | 15 | |
|
15 | 16 | # IPython sets the __IPYTHON__ variable so you can know if you have nested |
|
16 | 17 | # copies running. |
|
17 | 18 | |
|
18 | 19 | # Try running this code both at the command line and from inside IPython (with |
|
19 | 20 | # %run example-embed.py) |
|
20 | 21 | from IPython.config.loader import Config |
|
21 | 22 | try: |
|
22 | 23 | get_ipython |
|
23 | 24 | except NameError: |
|
24 | 25 | nested = 0 |
|
25 | 26 | cfg = Config() |
|
26 | 27 | prompt_config = cfg.PromptManager |
|
27 | 28 | prompt_config.in_template = 'In <\\#>: ' |
|
28 | 29 | prompt_config.in2_template = ' .\\D.: ' |
|
29 | 30 | prompt_config.out_template = 'Out<\\#>: ' |
|
30 | 31 | else: |
|
31 |
print |
|
|
32 |
print |
|
|
32 | print("Running nested copies of IPython.") | |
|
33 | print("The prompts for the nested copy have been modified") | |
|
33 | 34 | cfg = Config() |
|
34 | 35 | nested = 1 |
|
35 | 36 | |
|
36 | 37 | # First import the embeddable shell class |
|
37 | 38 | from IPython.frontend.terminal.embed import InteractiveShellEmbed |
|
38 | 39 | |
|
39 | 40 | # Now create an instance of the embeddable shell. The first argument is a |
|
40 | 41 | # string with options exactly as you would type them if you were starting |
|
41 | 42 | # IPython at the system command line. Any parameters you want to define for |
|
42 | 43 | # configuration can thus be specified here. |
|
43 | 44 | ipshell = InteractiveShellEmbed(config=cfg, |
|
44 | 45 | banner1 = 'Dropping into IPython', |
|
45 | 46 | exit_msg = 'Leaving Interpreter, back to program.') |
|
46 | 47 | |
|
47 | 48 | # Make a second instance, you can have as many as you want. |
|
48 | 49 | cfg2 = cfg.copy() |
|
49 | 50 | prompt_config = cfg2.PromptManager |
|
50 | 51 | prompt_config.in_template = 'In2<\\#>: ' |
|
51 | 52 | if not nested: |
|
52 | 53 | prompt_config.in_template = 'In2<\\#>: ' |
|
53 | 54 | prompt_config.in2_template = ' .\\D.: ' |
|
54 | 55 | prompt_config.out_template = 'Out<\\#>: ' |
|
55 | 56 | ipshell2 = InteractiveShellEmbed(config=cfg, |
|
56 | 57 | banner1 = 'Second IPython instance.') |
|
57 | 58 | |
|
58 |
print |
|
|
59 | print('\nHello. This is printed from the main controller program.\n') | |
|
59 | 60 | |
|
60 | 61 | # You can then call ipshell() anywhere you need it (with an optional |
|
61 | 62 | # message): |
|
62 | 63 | ipshell('***Called from top level. ' |
|
63 | 64 | 'Hit Ctrl-D to exit interpreter and continue program.\n' |
|
64 | 65 | 'Note that if you use %kill_embedded, you can fully deactivate\n' |
|
65 | 66 | 'This embedded instance so it will never turn on again') |
|
66 | 67 | |
|
67 |
print |
|
|
68 | print('\nBack in caller program, moving along...\n') | |
|
68 | 69 | |
|
69 | 70 | #--------------------------------------------------------------------------- |
|
70 | 71 | # More details: |
|
71 | 72 | |
|
72 | 73 | # InteractiveShellEmbed instances don't print the standard system banner and |
|
73 | 74 | # messages. The IPython banner (which actually may contain initialization |
|
74 | 75 | # messages) is available as get_ipython().banner in case you want it. |
|
75 | 76 | |
|
76 | 77 | # InteractiveShellEmbed instances print the following information everytime they |
|
77 | 78 | # start: |
|
78 | 79 | |
|
79 | 80 | # - A global startup banner. |
|
80 | 81 | |
|
81 | 82 | # - A call-specific header string, which you can use to indicate where in the |
|
82 | 83 | # execution flow the shell is starting. |
|
83 | 84 | |
|
84 | 85 | # They also print an exit message every time they exit. |
|
85 | 86 | |
|
86 | 87 | # Both the startup banner and the exit message default to None, and can be set |
|
87 | 88 | # either at the instance constructor or at any other time with the |
|
88 | 89 | # by setting the banner and exit_msg attributes. |
|
89 | 90 | |
|
90 | 91 | # The shell instance can be also put in 'dummy' mode globally or on a per-call |
|
91 | 92 | # basis. This gives you fine control for debugging without having to change |
|
92 | 93 | # code all over the place. |
|
93 | 94 | |
|
94 | 95 | # The code below illustrates all this. |
|
95 | 96 | |
|
96 | 97 | |
|
97 | 98 | # This is how the global banner and exit_msg can be reset at any point |
|
98 | 99 | ipshell.banner = 'Entering interpreter - New Banner' |
|
99 | 100 | ipshell.exit_msg = 'Leaving interpreter - New exit_msg' |
|
100 | 101 | |
|
101 | 102 | def foo(m): |
|
102 | 103 | s = 'spam' |
|
103 | 104 | ipshell('***In foo(). Try %whos, or print s or m:') |
|
104 |
print |
|
|
105 | print('foo says m = ',m) | |
|
105 | 106 | |
|
106 | 107 | def bar(n): |
|
107 | 108 | s = 'eggs' |
|
108 | 109 | ipshell('***In bar(). Try %whos, or print s or n:') |
|
109 |
print |
|
|
110 | print('bar says n = ',n) | |
|
110 | 111 | |
|
111 | 112 | # Some calls to the above functions which will trigger IPython: |
|
112 |
print |
|
|
113 | print('Main program calling foo("eggs")\n') | |
|
113 | 114 | foo('eggs') |
|
114 | 115 | |
|
115 | 116 | # The shell can be put in 'dummy' mode where calls to it silently return. This |
|
116 | 117 | # allows you, for example, to globally turn off debugging for a program with a |
|
117 | 118 | # single call. |
|
118 | 119 | ipshell.dummy_mode = True |
|
119 |
print |
|
|
120 | print('\nTrying to call IPython which is now "dummy":') | |
|
120 | 121 | ipshell() |
|
121 |
print |
|
|
122 | print('Nothing happened...') | |
|
122 | 123 | # The global 'dummy' mode can still be overridden for a single call |
|
123 |
print |
|
|
124 | print('\nOverriding dummy mode manually:') | |
|
124 | 125 | ipshell(dummy=False) |
|
125 | 126 | |
|
126 | 127 | # Reactivate the IPython shell |
|
127 | 128 | ipshell.dummy_mode = False |
|
128 | 129 | |
|
129 |
print |
|
|
130 | print('You can even have multiple embedded instances:') | |
|
130 | 131 | ipshell2() |
|
131 | 132 | |
|
132 |
print |
|
|
133 | print('\nMain program calling bar("spam")\n') | |
|
133 | 134 | bar('spam') |
|
134 | 135 | |
|
135 |
print |
|
|
136 | print('Main program finished. Bye!') | |
|
136 | 137 | |
|
137 | 138 | #********************** End of file <example-embed.py> *********************** |
General Comments 0
You need to be logged in to leave comments.
Login now