You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9 KiB

4 years ago
  1. """Publishing native (typically pickled) objects.
  2. """
  3. import warnings
  4. warnings.warn("ipykernel.datapub is deprecated. It has moved to ipyparallel.datapub", DeprecationWarning)
  5. # Copyright (c) IPython Development Team.
  6. # Distributed under the terms of the Modified BSD License.
  7. from traitlets.config import Configurable
  8. from traitlets import Instance, Dict, CBytes, Any
  9. from ipykernel.jsonutil import json_clean
  10. from ipykernel.serialize import serialize_object
  11. from jupyter_client.session import Session, extract_header
  12. class ZMQDataPublisher(Configurable):
  13. topic = topic = CBytes(b'datapub')
  14. session = Instance(Session, allow_none=True)
  15. pub_socket = Any(allow_none=True)
  16. parent_header = Dict({})
  17. def set_parent(self, parent):
  18. """Set the parent for outbound messages."""
  19. self.parent_header = extract_header(parent)
  20. def publish_data(self, data):
  21. """publish a data_message on the IOPub channel
  22. Parameters
  23. ----------
  24. data : dict
  25. The data to be published. Think of it as a namespace.
  26. """
  27. session = self.session
  28. buffers = serialize_object(data,
  29. buffer_threshold=session.buffer_threshold,
  30. item_threshold=session.item_threshold,
  31. )
  32. content = json_clean(dict(keys=list(data.keys())))
  33. session.send(self.pub_socket, 'data_message', content=content,
  34. parent=self.parent_header,
  35. buffers=buffers,
  36. ident=self.topic,
  37. )
  38. def publish_data(data):
  39. """publish a data_message on the IOPub channel
  40. Parameters
  41. ----------
  42. data : dict
  43. The data to be published. Think of it as a namespace.
  44. """
  45. warnings.warn("ipykernel.datapub is deprecated. It has moved to ipyparallel.datapub", DeprecationWarning)
  46. from ipykernel.zmqshell import ZMQInteractiveShell
  47. ZMQInteractiveShell.instance().data_pub.publish_data(data)