Show More
@@ -1,152 +1,156 b'' | |||||
1 | """ A minimal application using the ZMQ-based terminal IPython frontend. |
|
1 | """ A minimal application using the ZMQ-based terminal IPython frontend. | |
2 |
|
2 | |||
3 | This is not a complete console app, as subprocess will not be able to receive |
|
3 | This is not a complete console app, as subprocess will not be able to receive | |
4 | input, there is no real readline support, among other limitations. |
|
4 | input, there is no real readline support, among other limitations. | |
5 |
|
5 | |||
6 | Authors: |
|
6 | Authors: | |
7 |
|
7 | |||
8 | * Min RK |
|
8 | * Min RK | |
9 | * Paul Ivanov |
|
9 | * Paul Ivanov | |
10 |
|
10 | |||
11 | """ |
|
11 | """ | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | import signal |
|
16 | import signal | |
17 | import sys |
|
17 | import sys | |
18 | import time |
|
18 | import time | |
19 |
|
19 | |||
20 | from IPython.frontend.terminal.ipapp import TerminalIPythonApp, frontend_flags as term_flags |
|
20 | from IPython.frontend.terminal.ipapp import TerminalIPythonApp, frontend_flags as term_flags | |
21 |
|
21 | |||
22 | from IPython.utils.traitlets import ( |
|
22 | from IPython.utils.traitlets import ( | |
23 | Dict, List, Unicode, Int, CaselessStrEnum, CBool, Any |
|
23 | Dict, List, Unicode, Int, CaselessStrEnum, CBool, Any | |
24 | ) |
|
24 | ) | |
25 | from IPython.utils.warn import warn,error |
|
25 | from IPython.utils.warn import warn,error | |
26 |
|
26 | |||
27 | from IPython.zmq.ipkernel import IPKernelApp |
|
27 | from IPython.zmq.ipkernel import IPKernelApp | |
28 | from IPython.zmq.session import Session, default_secure |
|
28 | from IPython.zmq.session import Session, default_secure | |
29 | from IPython.zmq.zmqshell import ZMQInteractiveShell |
|
29 | from IPython.zmq.zmqshell import ZMQInteractiveShell | |
30 | from IPython.frontend.consoleapp import ( |
|
30 | from IPython.frontend.consoleapp import ( | |
31 | IPythonConsoleApp, app_aliases, app_flags, aliases, app_aliases, flags |
|
31 | IPythonConsoleApp, app_aliases, app_flags, aliases, app_aliases, flags | |
32 | ) |
|
32 | ) | |
33 |
|
33 | |||
34 | from IPython.frontend.terminal.console.interactiveshell import ZMQTerminalInteractiveShell |
|
34 | from IPython.frontend.terminal.console.interactiveshell import ZMQTerminalInteractiveShell | |
35 |
|
35 | |||
36 | #----------------------------------------------------------------------------- |
|
36 | #----------------------------------------------------------------------------- | |
37 | # Globals |
|
37 | # Globals | |
38 | #----------------------------------------------------------------------------- |
|
38 | #----------------------------------------------------------------------------- | |
39 |
|
39 | |||
40 | _examples = """ |
|
40 | _examples = """ | |
41 | ipython console # start the ZMQ-based console |
|
41 | ipython console # start the ZMQ-based console | |
42 | ipython console --existing # connect to an existing ipython session |
|
42 | ipython console --existing # connect to an existing ipython session | |
43 | """ |
|
43 | """ | |
44 |
|
44 | |||
45 | #----------------------------------------------------------------------------- |
|
45 | #----------------------------------------------------------------------------- | |
46 | # Flags and Aliases |
|
46 | # Flags and Aliases | |
47 | #----------------------------------------------------------------------------- |
|
47 | #----------------------------------------------------------------------------- | |
48 |
|
48 | |||
49 | # copy flags from mixin: |
|
49 | # copy flags from mixin: | |
50 | flags = dict(flags) |
|
50 | flags = dict(flags) | |
51 | # start with mixin frontend flags: |
|
51 | # start with mixin frontend flags: | |
52 | frontend_flags = dict(app_flags) |
|
52 | frontend_flags = dict(app_flags) | |
53 | # add TerminalIPApp flags: |
|
53 | # add TerminalIPApp flags: | |
54 | frontend_flags.update(term_flags) |
|
54 | frontend_flags.update(term_flags) | |
55 | # pylab is not frontend-specific in two-process IPython |
|
55 | # pylab is not frontend-specific in two-process IPython | |
56 | frontend_flags.pop('pylab') |
|
56 | frontend_flags.pop('pylab') | |
57 | # disable quick startup, as it won't propagate to the kernel anyway |
|
57 | # disable quick startup, as it won't propagate to the kernel anyway | |
58 | frontend_flags.pop('quick') |
|
58 | frontend_flags.pop('quick') | |
59 | # update full dict with frontend flags: |
|
59 | # update full dict with frontend flags: | |
60 | flags.update(frontend_flags) |
|
60 | flags.update(frontend_flags) | |
61 |
|
61 | |||
62 | # copy flags from mixin |
|
62 | # copy flags from mixin | |
63 | aliases = dict(aliases) |
|
63 | aliases = dict(aliases) | |
64 | # start with mixin frontend flags |
|
64 | # start with mixin frontend flags | |
65 | frontend_aliases = dict(app_aliases) |
|
65 | frontend_aliases = dict(app_aliases) | |
66 | # load updated frontend flags into full dict |
|
66 | # load updated frontend flags into full dict | |
67 | aliases.update(frontend_aliases) |
|
67 | aliases.update(frontend_aliases) | |
68 |
|
68 | |||
69 | # get flags&aliases into sets, and remove a couple that |
|
69 | # get flags&aliases into sets, and remove a couple that | |
70 | # shouldn't be scrubbed from backend flags: |
|
70 | # shouldn't be scrubbed from backend flags: | |
71 | frontend_aliases = set(frontend_aliases.keys()) |
|
71 | frontend_aliases = set(frontend_aliases.keys()) | |
72 | frontend_flags = set(frontend_flags.keys()) |
|
72 | frontend_flags = set(frontend_flags.keys()) | |
73 |
|
73 | |||
74 |
|
74 | |||
75 | #----------------------------------------------------------------------------- |
|
75 | #----------------------------------------------------------------------------- | |
76 | # Classes |
|
76 | # Classes | |
77 | #----------------------------------------------------------------------------- |
|
77 | #----------------------------------------------------------------------------- | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | class ZMQTerminalIPythonApp(TerminalIPythonApp, IPythonConsoleApp): |
|
80 | class ZMQTerminalIPythonApp(TerminalIPythonApp, IPythonConsoleApp): | |
81 | name = "ipython-console" |
|
81 | name = "ipython-console" | |
82 | """Start a terminal frontend to the IPython zmq kernel.""" |
|
82 | """Start a terminal frontend to the IPython zmq kernel.""" | |
83 |
|
83 | |||
84 | description = """ |
|
84 | description = """ | |
85 | The IPython terminal-based Console. |
|
85 | The IPython terminal-based Console. | |
86 |
|
86 | |||
87 | This launches a Console application inside a terminal. |
|
87 | This launches a Console application inside a terminal. | |
88 |
|
88 | |||
89 | The Console supports various extra features beyond the traditional |
|
89 | The Console supports various extra features beyond the traditional | |
90 | single-process Terminal IPython shell, such as connecting to an |
|
90 | single-process Terminal IPython shell, such as connecting to an | |
91 | existing ipython session, via: |
|
91 | existing ipython session, via: | |
92 |
|
92 | |||
93 | ipython console --existing |
|
93 | ipython console --existing | |
94 |
|
94 | |||
95 | where the previous session could have been created by another ipython |
|
95 | where the previous session could have been created by another ipython | |
96 | console, an ipython qtconsole, or by opening an ipython notebook. |
|
96 | console, an ipython qtconsole, or by opening an ipython notebook. | |
97 |
|
97 | |||
98 | """ |
|
98 | """ | |
99 | examples = _examples |
|
99 | examples = _examples | |
100 |
|
100 | |||
101 | classes = List([IPKernelApp, ZMQTerminalInteractiveShell, Session]) |
|
101 | classes = List([IPKernelApp, ZMQTerminalInteractiveShell, Session]) | |
102 | flags = Dict(flags) |
|
102 | flags = Dict(flags) | |
103 | aliases = Dict(aliases) |
|
103 | aliases = Dict(aliases) | |
104 | frontend_aliases = Any(frontend_aliases) |
|
104 | frontend_aliases = Any(frontend_aliases) | |
105 | frontend_flags = Any(frontend_flags) |
|
105 | frontend_flags = Any(frontend_flags) | |
106 |
|
106 | |||
107 | subcommands = Dict() |
|
107 | subcommands = Dict() | |
108 |
|
108 | |||
109 | def parse_command_line(self, argv=None): |
|
109 | def parse_command_line(self, argv=None): | |
110 | super(ZMQTerminalIPythonApp, self).parse_command_line(argv) |
|
110 | super(ZMQTerminalIPythonApp, self).parse_command_line(argv) | |
111 | self.build_kernel_argv(argv) |
|
111 | self.build_kernel_argv(argv) | |
112 |
|
112 | |||
113 | def init_shell(self): |
|
113 | def init_shell(self): | |
114 | IPythonConsoleApp.initialize(self) |
|
114 | IPythonConsoleApp.initialize(self) | |
115 | # relay sigint to kernel |
|
115 | # relay sigint to kernel | |
116 | signal.signal(signal.SIGINT, self.handle_sigint) |
|
116 | signal.signal(signal.SIGINT, self.handle_sigint) | |
117 | self.shell = ZMQTerminalInteractiveShell.instance(config=self.config, |
|
117 | self.shell = ZMQTerminalInteractiveShell.instance(config=self.config, | |
118 | display_banner=False, profile_dir=self.profile_dir, |
|
118 | display_banner=False, profile_dir=self.profile_dir, | |
119 | ipython_dir=self.ipython_dir, kernel_manager=self.kernel_manager) |
|
119 | ipython_dir=self.ipython_dir, kernel_manager=self.kernel_manager) | |
120 |
|
120 | |||
|
121 | def init_gui_pylab(self): | |||
|
122 | # no-op, because we don't want to import matplotlib in the frontend. | |||
|
123 | pass | |||
|
124 | ||||
121 | def handle_sigint(self, *args): |
|
125 | def handle_sigint(self, *args): | |
122 | if self.shell._executing: |
|
126 | if self.shell._executing: | |
123 | if self.kernel_manager.has_kernel: |
|
127 | if self.kernel_manager.has_kernel: | |
124 | # interrupt already gets passed to subprocess by signal handler. |
|
128 | # interrupt already gets passed to subprocess by signal handler. | |
125 | # Only if we prevent that should we need to explicitly call |
|
129 | # Only if we prevent that should we need to explicitly call | |
126 | # interrupt_kernel, until which time, this would result in a |
|
130 | # interrupt_kernel, until which time, this would result in a | |
127 | # double-interrupt: |
|
131 | # double-interrupt: | |
128 | # self.kernel_manager.interrupt_kernel() |
|
132 | # self.kernel_manager.interrupt_kernel() | |
129 | pass |
|
133 | pass | |
130 | else: |
|
134 | else: | |
131 | self.shell.write_err('\n') |
|
135 | self.shell.write_err('\n') | |
132 | error("Cannot interrupt kernels we didn't start.\n") |
|
136 | error("Cannot interrupt kernels we didn't start.\n") | |
133 | else: |
|
137 | else: | |
134 | # raise the KeyboardInterrupt if we aren't waiting for execution, |
|
138 | # raise the KeyboardInterrupt if we aren't waiting for execution, | |
135 | # so that the interact loop advances, and prompt is redrawn, etc. |
|
139 | # so that the interact loop advances, and prompt is redrawn, etc. | |
136 | raise KeyboardInterrupt |
|
140 | raise KeyboardInterrupt | |
137 |
|
141 | |||
138 |
|
142 | |||
139 | def init_code(self): |
|
143 | def init_code(self): | |
140 | # no-op in the frontend, code gets run in the backend |
|
144 | # no-op in the frontend, code gets run in the backend | |
141 | pass |
|
145 | pass | |
142 |
|
146 | |||
143 | def launch_new_instance(): |
|
147 | def launch_new_instance(): | |
144 | """Create and run a full blown IPython instance""" |
|
148 | """Create and run a full blown IPython instance""" | |
145 | app = ZMQTerminalIPythonApp.instance() |
|
149 | app = ZMQTerminalIPythonApp.instance() | |
146 | app.initialize() |
|
150 | app.initialize() | |
147 | app.start() |
|
151 | app.start() | |
148 |
|
152 | |||
149 |
|
153 | |||
150 | if __name__ == '__main__': |
|
154 | if __name__ == '__main__': | |
151 | launch_new_instance() |
|
155 | launch_new_instance() | |
152 |
|
156 |
General Comments 0
You need to be logged in to leave comments.
Login now