payloadpage.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """A payload based version of page."""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import warnings
  5. from IPython.core.getipython import get_ipython
  6. # see https://github.com/ipython/ipykernel/issues/1304
  7. # this should be moved to ipykernel and removed in the long run.
  8. def page(strng, start=0, screen_lines=0, pager_cmd=None):
  9. """Print a string, piping through a pager.
  10. This version ignores the screen_lines and pager_cmd arguments and uses
  11. IPython's payload system instead.
  12. Parameters
  13. ----------
  14. strng : str or mime-dict
  15. Text to page, or a mime-type keyed dict of already formatted data.
  16. start : int
  17. Starting line at which to place the display.
  18. """
  19. # Some routines may auto-compute start offsets incorrectly and pass a
  20. # negative value. Offset to 0 for robustness.
  21. start = max(0, start)
  22. shell = get_ipython()
  23. if isinstance(strng, dict):
  24. data = strng
  25. else:
  26. data = {"text/plain": strng}
  27. payload = dict(
  28. source="page",
  29. data=data,
  30. start=start,
  31. )
  32. shell.payload_manager.write_payload(payload)