##// END OF EJS Templates
add kernel banner to terminal and qt frontends
MinRK -
Show More
@@ -32,6 +32,7 b' from IPython.core.error import UsageError'
32 from IPython.core.magics import MacroToEdit, CodeMagics
32 from IPython.core.magics import MacroToEdit, CodeMagics
33 from IPython.core.magic import magics_class, line_magic, Magics
33 from IPython.core.magic import magics_class, line_magic, Magics
34 from IPython.core.payloadpage import install_payload_page
34 from IPython.core.payloadpage import install_payload_page
35 from IPython.core.usage import default_gui_banner
35 from IPython.display import display, Javascript
36 from IPython.display import display, Javascript
36 from IPython.kernel.inprocess.socket import SocketABC
37 from IPython.kernel.inprocess.socket import SocketABC
37 from IPython.kernel import (
38 from IPython.kernel import (
@@ -422,6 +423,9 b' class ZMQInteractiveShell(InteractiveShell):'
422 data_pub_class = Type(ZMQDataPublisher)
423 data_pub_class = Type(ZMQDataPublisher)
423 kernel = Any()
424 kernel = Any()
424 parent_header = Any()
425 parent_header = Any()
426
427 def _banner1_default(self):
428 return default_gui_banner
425
429
426 # Override the traitlet in the parent class, because there's no point using
430 # Override the traitlet in the parent class, because there's no point using
427 # readline for the kernel. Can be removed when the readline code is moved
431 # readline for the kernel. Can be removed when the readline code is moved
@@ -82,6 +82,7 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
82
82
83 # The text to show when the kernel is (re)started.
83 # The text to show when the kernel is (re)started.
84 banner = Unicode(config=True)
84 banner = Unicode(config=True)
85 kernel_banner = Unicode()
85
86
86 # An option and corresponding signal for overriding the default kernel
87 # An option and corresponding signal for overriding the default kernel
87 # interrupt behavior.
88 # interrupt behavior.
@@ -639,6 +640,9 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
639 if clear:
640 if clear:
640 self._control.clear()
641 self._control.clear()
641 self._append_plain_text(self.banner)
642 self._append_plain_text(self.banner)
643 if self.kernel_banner:
644 self._append_plain_text(self.kernel_banner)
645
642 # update output marker for stdout/stderr, so that startup
646 # update output marker for stdout/stderr, so that startup
643 # messages appear after banner:
647 # messages appear after banner:
644 self._append_before_prompt_pos = self._get_cursor().position()
648 self._append_before_prompt_pos = self._get_cursor().position()
@@ -17,6 +17,7 b' from textwrap import dedent'
17 from IPython.external.qt import QtCore, QtGui
17 from IPython.external.qt import QtCore, QtGui
18
18
19 from IPython.core.inputsplitter import IPythonInputSplitter
19 from IPython.core.inputsplitter import IPythonInputSplitter
20 from IPython.core.release import version
20 from IPython.core.inputtransformer import ipy_prompt
21 from IPython.core.inputtransformer import ipy_prompt
21 from IPython.utils.traitlets import Bool, Unicode
22 from IPython.utils.traitlets import Bool, Unicode
22 from .frontend_widget import FrontendWidget
23 from .frontend_widget import FrontendWidget
@@ -107,6 +108,7 b' class IPythonWidget(FrontendWidget):'
107 _payload_source_next_input = 'set_next_input'
108 _payload_source_next_input = 'set_next_input'
108 _payload_source_page = 'page'
109 _payload_source_page = 'page'
109 _retrying_history_request = False
110 _retrying_history_request = False
111 _starting = False
110
112
111 #---------------------------------------------------------------------------
113 #---------------------------------------------------------------------------
112 # 'object' interface
114 # 'object' interface
@@ -253,31 +255,28 b' class IPythonWidget(FrontendWidget):'
253 self._append_plain_text(u'\n', True)
255 self._append_plain_text(u'\n', True)
254
256
255 def _handle_kernel_info_reply(self, rep):
257 def _handle_kernel_info_reply(self, rep):
256 """ Handle kernel info replies.
258 """Handle kernel info replies."""
257 """
259 content = rep['content']
258 if not self._guiref_loaded:
260 if not self._guiref_loaded:
259 if rep['content'].get('language') == 'python':
261 if content.get('language') == 'python':
260 self._load_guiref_magic()
262 self._load_guiref_magic()
261 self._guiref_loaded = True
263 self._guiref_loaded = True
264
265 self.kernel_banner = content.get('banner', '')
266 if self._starting:
267 # finish handling started channels
268 self._starting = False
269 super(IPythonWidget, self)._started_channels()
262
270
263 def _started_channels(self):
271 def _started_channels(self):
264 """Reimplemented to make a history request and load %guiref."""
272 """Reimplemented to make a history request and load %guiref."""
265 super(IPythonWidget, self)._started_channels()
273 self._starting = True
266
267 # The reply will trigger %guiref load provided language=='python'
274 # The reply will trigger %guiref load provided language=='python'
268 self.kernel_client.kernel_info()
275 self.kernel_client.kernel_info()
269
276
270 self.kernel_client.shell_channel.history(hist_access_type='tail',
277 self.kernel_client.shell_channel.history(hist_access_type='tail',
271 n=1000)
278 n=1000)
272
279
273 def _started_kernel(self):
274 """Load %guiref when the kernel starts (if channels are also started).
275
276 Principally triggered by kernel restart.
277 """
278 if self.kernel_client.shell_channel is not None:
279 self._load_guiref_magic()
280
281 def _load_guiref_magic(self):
280 def _load_guiref_magic(self):
282 """Load %guiref magic."""
281 """Load %guiref magic."""
283 self.kernel_client.shell_channel.execute('\n'.join([
282 self.kernel_client.shell_channel.execute('\n'.join([
@@ -568,5 +567,4 b' class IPythonWidget(FrontendWidget):'
568 #------ Trait default initializers -----------------------------------------
567 #------ Trait default initializers -----------------------------------------
569
568
570 def _banner_default(self):
569 def _banner_default(self):
571 from IPython.core.usage import default_gui_banner
570 return "IPython QtConsole {version}\n".format(version=version)
572 return default_gui_banner
@@ -22,6 +22,7 b' except ImportError:'
22 from Queue import Empty # Py 2
22 from Queue import Empty # Py 2
23
23
24 from IPython.core import page
24 from IPython.core import page
25 from IPython.core import release
25 from IPython.utils.warn import warn, error
26 from IPython.utils.warn import warn, error
26 from IPython.utils import io
27 from IPython.utils import io
27 from IPython.utils.py3compat import string_types, input
28 from IPython.utils.py3compat import string_types, input
@@ -37,6 +38,7 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
37 _executing = False
38 _executing = False
38 _execution_state = Unicode('')
39 _execution_state = Unicode('')
39 _pending_clearoutput = False
40 _pending_clearoutput = False
41 kernel_banner = Unicode('')
40 kernel_timeout = Float(60, config=True,
42 kernel_timeout = Float(60, config=True,
41 help="""Timeout for giving up on a kernel (in seconds).
43 help="""Timeout for giving up on a kernel (in seconds).
42
44
@@ -366,7 +368,7 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
366 while True:
368 while True:
367 try:
369 try:
368 self.interact(display_banner=display_banner)
370 self.interact(display_banner=display_banner)
369 #self.interact_with_readline()
371 #self.interact_with_readline()
370 # XXX for testing of a readline-decoupled repl loop, call
372 # XXX for testing of a readline-decoupled repl loop, call
371 # interact_with_readline above
373 # interact_with_readline above
372 break
374 break
@@ -375,6 +377,24 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
375 # handling seems rather unpredictable...
377 # handling seems rather unpredictable...
376 self.write("\nKeyboardInterrupt in interact()\n")
378 self.write("\nKeyboardInterrupt in interact()\n")
377
379
380 def _banner1_default(self):
381 return "IPython Console {version}\n".format(version=release.version)
382
383 def compute_banner(self):
384 super(ZMQTerminalInteractiveShell, self).compute_banner()
385 if self.client and not self.kernel_banner:
386 msg_id = self.client.kernel_info()
387 while True:
388 try:
389 reply = self.client.get_shell_msg(timeout=1)
390 except Empty:
391 break
392 else:
393 if reply['parent_header'].get('msg_id') == msg_id:
394 self.kernel_banner = reply['content'].get('banner', '')
395 break
396 self.banner += self.kernel_banner
397
378 def wait_for_kernel(self, timeout=None):
398 def wait_for_kernel(self, timeout=None):
379 """method to wait for a kernel to be ready"""
399 """method to wait for a kernel to be ready"""
380 tic = time.time()
400 tic = time.time()
General Comments 0
You need to be logged in to leave comments. Login now