inprocess.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """ Defines an in-process KernelManager with signals and slots.
  2. """
  3. from qtpy import QtCore
  4. from ipykernel.inprocess import (
  5. InProcessHBChannel, InProcessKernelClient, InProcessKernelManager,
  6. )
  7. from ipykernel.inprocess.channels import InProcessChannel
  8. from traitlets import Type
  9. from .util import SuperQObject
  10. from .kernel_mixins import (
  11. QtKernelClientMixin, QtKernelManagerMixin,
  12. )
  13. from .rich_jupyter_widget import RichJupyterWidget
  14. class QtInProcessChannel(SuperQObject, InProcessChannel):
  15. # Emitted when the channel is started.
  16. started = QtCore.Signal()
  17. # Emitted when the channel is stopped.
  18. stopped = QtCore.Signal()
  19. # Emitted when any message is received.
  20. message_received = QtCore.Signal(object)
  21. def start(self):
  22. """ Reimplemented to emit signal.
  23. """
  24. super(QtInProcessChannel, self).start()
  25. self.started.emit()
  26. def stop(self):
  27. """ Reimplemented to emit signal.
  28. """
  29. super(QtInProcessChannel, self).stop()
  30. self.stopped.emit()
  31. def call_handlers_later(self, *args, **kwds):
  32. """ Call the message handlers later.
  33. """
  34. do_later = lambda: self.call_handlers(*args, **kwds)
  35. QtCore.QTimer.singleShot(0, do_later)
  36. def call_handlers(self, msg):
  37. self.message_received.emit(msg)
  38. def process_events(self):
  39. """ Process any pending GUI events.
  40. """
  41. QtCore.QCoreApplication.instance().processEvents()
  42. def flush(self, timeout=1.0):
  43. """ Reimplemented to ensure that signals are dispatched immediately.
  44. """
  45. super(QtInProcessChannel, self).flush()
  46. self.process_events()
  47. class QtInProcessHBChannel(SuperQObject, InProcessHBChannel):
  48. # This signal will never be fired, but it needs to exist
  49. kernel_died = QtCore.Signal()
  50. class QtInProcessKernelClient(QtKernelClientMixin, InProcessKernelClient):
  51. """ An in-process KernelManager with signals and slots.
  52. """
  53. iopub_channel_class = Type(QtInProcessChannel)
  54. shell_channel_class = Type(QtInProcessChannel)
  55. stdin_channel_class = Type(QtInProcessChannel)
  56. hb_channel_class = Type(QtInProcessHBChannel)
  57. class QtInProcessKernelManager(QtKernelManagerMixin, InProcessKernelManager):
  58. client_class = __module__ + '.QtInProcessKernelClient'
  59. class QtInProcessRichJupyterWidget(RichJupyterWidget):
  60. """ An in-process Jupyter Widget that enables multiline editing
  61. """
  62. def _is_complete(self, source, interactive=True):
  63. shell = self.kernel_manager.kernel.shell
  64. status, indent_spaces = \
  65. shell.input_transformer_manager.check_complete(source)
  66. if indent_spaces is None:
  67. indent = ''
  68. else:
  69. indent = ' ' * indent_spaces
  70. return status != 'incomplete', indent