echo_kernel.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. import logging
  4. from ipykernel.kernelapp import IPKernelApp
  5. from ipykernel.kernelbase import Kernel
  6. class EchoKernel(Kernel):
  7. implementation = "Echo"
  8. implementation_version = "1.0"
  9. language = "echo"
  10. language_version = "0.1"
  11. language_info = {
  12. "name": "echo",
  13. "mimetype": "text/plain",
  14. "file_extension": ".txt",
  15. }
  16. banner = "Echo kernel - as useful as a parrot"
  17. def do_execute(
  18. self, code, silent, store_history=True, user_expressions=None, allow_stdin=False
  19. ):
  20. if not silent:
  21. stream_content = {"name": "stdout", "text": code}
  22. self.send_response(self.iopub_socket, "stream", stream_content)
  23. # Send a input_request if code contains input command.
  24. if allow_stdin and code and code.find("input(") != -1:
  25. self._input_request(
  26. "Echo Prompt",
  27. self._parent_ident["shell"],
  28. self.get_parent(channel="shell"),
  29. password=False,
  30. )
  31. return {
  32. "status": "ok",
  33. # The base class increments the execution count
  34. "execution_count": self.execution_count,
  35. "payload": [],
  36. "user_expressions": {},
  37. }
  38. class EchoKernelApp(IPKernelApp):
  39. kernel_class = EchoKernel
  40. if __name__ == "__main__":
  41. logging.disable(logging.ERROR)
  42. EchoKernelApp.launch_instance()