Debian Codegraph @

jupyter_console/app.py

raw
1 """ A minimal application using the ZMQ-based terminal IPython frontend.
2
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.
5 """
6
7 # Copyright (c) IPython Development Team.
8 # Distributed under the terms of the Modified BSD License.
9
10 from __future__ import print_function
11
12 import signal
13 import sys
14
15 from traitlets import (
16 Dict, Any
17 )
18 from traitlets.config import catch_config_error, boolean_flag
19
20 from jupyter_core.application import JupyterApp, base_aliases, base_flags
21 from jupyter_client.consoleapp import (
22 JupyterConsoleApp, app_aliases, app_flags,
23 )
24
25 from jupyter_console.ptshell import ZMQTerminalInteractiveShell
26 from jupyter_console import __version__
27
28 #-----------------------------------------------------------------------------
29 # Globals
30 #-----------------------------------------------------------------------------
31
32 _examples = """
33 jupyter console # start the ZMQ-based console
34 jupyter console --existing # connect to an existing ipython session
35 """
36
37 #-----------------------------------------------------------------------------
38 # Flags and Aliases
39 #-----------------------------------------------------------------------------
40
41 # copy flags from mixin:
42 flags = dict(base_flags)
43 # start with mixin frontend flags:
44 # update full dict with frontend flags:
45 flags.update(app_flags)
46 flags.update(boolean_flag(
47 'simple-prompt', 'ZMQTerminalInteractiveShell.simple_prompt',
48 "Force simple minimal prompt using `raw_input`",
49 "Use a rich interactive prompt with prompt_toolkit"
50 ))
51
52 # copy flags from mixin
53 aliases = dict(base_aliases)
54
55 aliases.update(app_aliases)
56
57 frontend_aliases = set(app_aliases.keys())
58 frontend_flags = set(app_flags.keys())
59
60
61 #-----------------------------------------------------------------------------
62 # Classes
63 #-----------------------------------------------------------------------------
64
65
66 class ZMQTerminalIPythonApp(JupyterApp, JupyterConsoleApp): # type:ignore[misc]
67 name = "jupyter-console"
68 version = __version__
69 """Start a terminal frontend to the IPython zmq kernel."""
70
71 description = """
72 The Jupyter terminal-based Console.
73
74 This launches a Console application inside a terminal.
75
76 The Console supports various extra features beyond the traditional
77 single-process Terminal IPython shell, such as connecting to an
78 existing ipython session, via:
79
80 jupyter console --existing
81
82 where the previous session could have been created by another ipython
83 console, an ipython qtconsole, or by opening an ipython notebook.
84
85 """
86 examples = _examples
87
88 classes = [ZMQTerminalInteractiveShell] + JupyterConsoleApp.classes # type:ignore[operator]
89 flags = Dict(flags) # type:ignore[assignment]
90 aliases = Dict(aliases) # type:ignore[assignment]
91 frontend_aliases = Any(frontend_aliases)
92 frontend_flags = Any(frontend_flags)
93
94 subcommands = Dict()
95
96 force_interact = True
97
98 def parse_command_line(self, argv=None):
99 super(ZMQTerminalIPythonApp, self).parse_command_line(argv)
100 self.build_kernel_argv(self.extra_args)
101
102 def init_shell(self):
103 JupyterConsoleApp.initialize(self)
104 # relay sigint to kernel
105 signal.signal(signal.SIGINT, self.handle_sigint)
106 self.shell = ZMQTerminalInteractiveShell.instance(parent=self,
107 manager=self.kernel_manager,
108 client=self.kernel_client,
109 confirm_exit=self.confirm_exit,
110 )
111 self.shell.own_kernel = not self.existing
112
113 def init_gui_pylab(self):
114 # no-op, because we don't want to import matplotlib in the frontend.
115 pass
116
117 def handle_sigint(self, *args):
118 if self.shell._executing:
119 if self.kernel_manager:
120 self.kernel_manager.interrupt_kernel()
121 else:
122 print("ERROR: Cannot interrupt kernels we didn't start.",
123 file = sys.stderr)
124 else:
125 # raise the KeyboardInterrupt if we aren't waiting for execution,
126 # so that the interact loop advances, and prompt is redrawn, etc.
127 raise KeyboardInterrupt
128
129 @catch_config_error
130 def initialize(self, argv=None):
131 """Do actions after construct, but before starting the app."""
132 super(ZMQTerminalIPythonApp, self).initialize(argv)
133 if self._dispatching:
134 return
135 # create the shell
136 self.init_shell()
137 # and draw the banner
138 self.init_banner()
139
140 def init_banner(self):
141 """optionally display the banner"""
142 self.shell.show_banner()
143
144 def start(self):
145 # JupyterApp.start dispatches on NoStart
146 super(ZMQTerminalIPythonApp, self).start()
147 self.log.debug("Starting the jupyter console mainloop...")
148 self.shell.mainloop()
149
150
151 main = launch_new_instance = ZMQTerminalIPythonApp.launch_instance
152
153
154 if __name__ == '__main__':
155 main()
156