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