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