Show More
@@ -1,137 +1,135 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | """An example of how to embed an IPython shell into a running program. |
|
3 | 3 | |
|
4 | 4 | Please see the documentation in the IPython.Shell module for more details. |
|
5 | 5 | |
|
6 |
The accompanying file e |
|
|
6 | The accompanying file embed_class_short.py has quick code fragments for | |
|
7 | 7 | embedding which you can cut and paste in your code once you understand how |
|
8 | 8 | things work. |
|
9 | 9 | |
|
10 | 10 | The code in this file is deliberately extra-verbose, meant for learning.""" |
|
11 | 11 | from __future__ import print_function |
|
12 | 12 | |
|
13 | 13 | # The basics to get you going: |
|
14 | 14 | |
|
15 | 15 | # IPython injects get_ipython into builtins, 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 | from IPython.config.loader import Config |
|
21 | 21 | try: |
|
22 | 22 | get_ipython |
|
23 | 23 | except NameError: |
|
24 | 24 | nested = 0 |
|
25 | 25 | cfg = Config() |
|
26 | 26 | prompt_config = cfg.PromptManager |
|
27 | 27 | prompt_config.in_template = 'In <\\#>: ' |
|
28 | 28 | prompt_config.in2_template = ' .\\D.: ' |
|
29 | 29 | prompt_config.out_template = 'Out<\\#>: ' |
|
30 | 30 | else: |
|
31 | 31 | print("Running nested copies of IPython.") |
|
32 | 32 | print("The prompts for the nested copy have been modified") |
|
33 | 33 | cfg = Config() |
|
34 | 34 | nested = 1 |
|
35 | 35 | |
|
36 | 36 | # First import the embeddable shell class |
|
37 | 37 | from IPython.terminal.embed import InteractiveShellEmbed |
|
38 | 38 | |
|
39 | 39 | # Now create an instance of the embeddable shell. The first argument is a |
|
40 | 40 | # string with options exactly as you would type them if you were starting |
|
41 | 41 | # IPython at the system command line. Any parameters you want to define for |
|
42 | 42 | # configuration can thus be specified here. |
|
43 | 43 | ipshell = InteractiveShellEmbed(config=cfg, |
|
44 | 44 | banner1 = 'Dropping into IPython', |
|
45 | 45 | exit_msg = 'Leaving Interpreter, back to program.') |
|
46 | 46 | |
|
47 | 47 | # Make a second instance, you can have as many as you want. |
|
48 | 48 | cfg2 = cfg.copy() |
|
49 | 49 | prompt_config = cfg2.PromptManager |
|
50 | 50 | prompt_config.in_template = 'In2<\\#>: ' |
|
51 | 51 | if not nested: |
|
52 | 52 | prompt_config.in_template = 'In2<\\#>: ' |
|
53 | 53 | prompt_config.in2_template = ' .\\D.: ' |
|
54 | 54 | prompt_config.out_template = 'Out<\\#>: ' |
|
55 | 55 | ipshell2 = InteractiveShellEmbed(config=cfg, |
|
56 | 56 | banner1 = 'Second IPython instance.') |
|
57 | 57 | |
|
58 | 58 | print('\nHello. This is printed from the main controller program.\n') |
|
59 | 59 | |
|
60 | 60 | # You can then call ipshell() anywhere you need it (with an optional |
|
61 | 61 | # message): |
|
62 | 62 | ipshell('***Called from top level. ' |
|
63 | 63 | 'Hit Ctrl-D to exit interpreter and continue program.\n' |
|
64 | 64 | 'Note that if you use %kill_embedded, you can fully deactivate\n' |
|
65 | 65 | 'This embedded instance so it will never turn on again') |
|
66 | 66 | |
|
67 | 67 | print('\nBack in caller program, moving along...\n') |
|
68 | 68 | |
|
69 | 69 | #--------------------------------------------------------------------------- |
|
70 | 70 | # More details: |
|
71 | 71 | |
|
72 | 72 | # InteractiveShellEmbed instances don't print the standard system banner and |
|
73 | 73 | # messages. The IPython banner (which actually may contain initialization |
|
74 | 74 | # messages) is available as get_ipython().banner in case you want it. |
|
75 | 75 | |
|
76 | 76 | # InteractiveShellEmbed instances print the following information everytime they |
|
77 | 77 | # start: |
|
78 | 78 | |
|
79 | 79 | # - A global startup banner. |
|
80 | 80 | |
|
81 | 81 | # - A call-specific header string, which you can use to indicate where in the |
|
82 | 82 | # execution flow the shell is starting. |
|
83 | 83 | |
|
84 | 84 | # They also print an exit message every time they exit. |
|
85 | 85 | |
|
86 | 86 | # Both the startup banner and the exit message default to None, and can be set |
|
87 | 87 | # either at the instance constructor or at any other time with the |
|
88 | 88 | # by setting the banner and exit_msg attributes. |
|
89 | 89 | |
|
90 | 90 | # The shell instance can be also put in 'dummy' mode globally or on a per-call |
|
91 | 91 | # basis. This gives you fine control for debugging without having to change |
|
92 | 92 | # code all over the place. |
|
93 | 93 | |
|
94 | 94 | # The code below illustrates all this. |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | # This is how the global banner and exit_msg can be reset at any point |
|
98 | 98 | ipshell.banner = 'Entering interpreter - New Banner' |
|
99 | 99 | ipshell.exit_msg = 'Leaving interpreter - New exit_msg' |
|
100 | 100 | |
|
101 | 101 | def foo(m): |
|
102 | 102 | s = 'spam' |
|
103 | 103 | ipshell('***In foo(). Try %whos, or print s or m:') |
|
104 | 104 | print('foo says m = ',m) |
|
105 | 105 | |
|
106 | 106 | def bar(n): |
|
107 | 107 | s = 'eggs' |
|
108 | 108 | ipshell('***In bar(). Try %whos, or print s or n:') |
|
109 | 109 | print('bar says n = ',n) |
|
110 | 110 | |
|
111 | 111 | # Some calls to the above functions which will trigger IPython: |
|
112 | 112 | print('Main program calling foo("eggs")\n') |
|
113 | 113 | foo('eggs') |
|
114 | 114 | |
|
115 | 115 | # The shell can be put in 'dummy' mode where calls to it silently return. This |
|
116 | 116 | # allows you, for example, to globally turn off debugging for a program with a |
|
117 | 117 | # single call. |
|
118 | 118 | ipshell.dummy_mode = True |
|
119 | 119 | print('\nTrying to call IPython which is now "dummy":') |
|
120 | 120 | ipshell() |
|
121 | 121 | print('Nothing happened...') |
|
122 | 122 | # The global 'dummy' mode can still be overridden for a single call |
|
123 | 123 | print('\nOverriding dummy mode manually:') |
|
124 | 124 | ipshell(dummy=False) |
|
125 | 125 | |
|
126 | 126 | # Reactivate the IPython shell |
|
127 | 127 | ipshell.dummy_mode = False |
|
128 | 128 | |
|
129 | 129 | print('You can even have multiple embedded instances:') |
|
130 | 130 | ipshell2() |
|
131 | 131 | |
|
132 | 132 | print('\nMain program calling bar("spam")\n') |
|
133 | 133 | bar('spam') |
|
134 | 134 | |
|
135 | 135 | print('Main program finished. Bye!') |
|
136 | ||
|
137 | #********************** End of file <example-embed.py> *********************** |
@@ -1,47 +1,45 b'' | |||
|
1 | 1 | """Quick code snippets for embedding IPython into other programs. |
|
2 | 2 | |
|
3 |
See e |
|
|
3 | See embed_class_long.py for full details, this file has the bare minimum code for | |
|
4 | 4 | cut and paste use once you understand how to use the system.""" |
|
5 | 5 | |
|
6 | 6 | #--------------------------------------------------------------------------- |
|
7 | 7 | # This code loads IPython but modifies a few things if it detects it's running |
|
8 | 8 | # embedded in another IPython session (helps avoid confusion) |
|
9 | 9 | |
|
10 | 10 | try: |
|
11 | 11 | get_ipython |
|
12 | 12 | except NameError: |
|
13 | 13 | banner=exit_msg='' |
|
14 | 14 | else: |
|
15 | 15 | banner = '*** Nested interpreter ***' |
|
16 | 16 | exit_msg = '*** Back in main IPython ***' |
|
17 | 17 | |
|
18 | 18 | # First import the embed function |
|
19 | 19 | from IPython.terminal.embed import InteractiveShellEmbed |
|
20 | 20 | # Now create the IPython shell instance. Put ipshell() anywhere in your code |
|
21 | 21 | # where you want it to open. |
|
22 | 22 | ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg) |
|
23 | 23 | |
|
24 | 24 | #--------------------------------------------------------------------------- |
|
25 | 25 | # This code will load an embeddable IPython shell always with no changes for |
|
26 | 26 | # nested embededings. |
|
27 | 27 | |
|
28 | 28 | from IPython import embed |
|
29 | 29 | # Now embed() will open IPython anywhere in the code. |
|
30 | 30 | |
|
31 | 31 | #--------------------------------------------------------------------------- |
|
32 | 32 | # This code loads an embeddable shell only if NOT running inside |
|
33 | 33 | # IPython. Inside IPython, the embeddable shell variable ipshell is just a |
|
34 | 34 | # dummy function. |
|
35 | 35 | |
|
36 | 36 | try: |
|
37 | 37 | get_ipython |
|
38 | 38 | except NameError: |
|
39 | 39 | from IPython.terminal.embed import InteractiveShellEmbed |
|
40 | 40 | ipshell = InteractiveShellEmbed() |
|
41 | 41 | # Now ipshell() will open IPython anywhere in the code |
|
42 | 42 | else: |
|
43 | 43 | # Define a dummy ipshell() so the same code doesn't crash inside an |
|
44 | 44 | # interactive IPython |
|
45 | 45 | def ipshell(): pass |
|
46 | ||
|
47 | #******************* End of file <example-embed-short.py> ******************** |
@@ -1,17 +1,16 b'' | |||
|
1 | # This shows how to use the new top-level embed function. It is a simpler | |
|
2 | # API that manages the creation of the embedded shell. | |
|
1 | """Embed IPython using the simple embed function rather than the class API.""" | |
|
3 | 2 | |
|
4 | 3 | from IPython import embed |
|
5 | 4 | |
|
6 | 5 | a = 10 |
|
7 | 6 | b = 20 |
|
8 | 7 | |
|
9 | 8 | embed(header='First time', banner1='') |
|
10 | 9 | |
|
11 | 10 | c = 30 |
|
12 | 11 | d = 40 |
|
13 | 12 | |
|
14 | 13 | try: |
|
15 | 14 | raise Exception('adsfasdf') |
|
16 | 15 | except: |
|
17 | 16 | embed(header='The second time') |
General Comments 0
You need to be logged in to leave comments.
Login now