console.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. Internal module for console introspection
  3. """
  4. from shutil import get_terminal_size
  5. def get_console_size():
  6. """
  7. Return console size as tuple = (width, height).
  8. Returns (None,None) in non-interactive session.
  9. """
  10. from pandas import get_option
  11. display_width = get_option("display.width")
  12. display_height = get_option("display.max_rows")
  13. # Consider
  14. # interactive shell terminal, can detect term size
  15. # interactive non-shell terminal (ipnb/ipqtconsole), cannot detect term
  16. # size non-interactive script, should disregard term size
  17. # in addition
  18. # width,height have default values, but setting to 'None' signals
  19. # should use Auto-Detection, But only in interactive shell-terminal.
  20. # Simple. yeah.
  21. if in_interactive_session():
  22. if in_ipython_frontend():
  23. # sane defaults for interactive non-shell terminal
  24. # match default for width,height in config_init
  25. from pandas._config.config import get_default_val
  26. terminal_width = get_default_val("display.width")
  27. terminal_height = get_default_val("display.max_rows")
  28. else:
  29. # pure terminal
  30. terminal_width, terminal_height = get_terminal_size()
  31. else:
  32. terminal_width, terminal_height = None, None
  33. # Note if the User sets width/Height to None (auto-detection)
  34. # and we're in a script (non-inter), this will return (None,None)
  35. # caller needs to deal.
  36. return (display_width or terminal_width, display_height or terminal_height)
  37. # ----------------------------------------------------------------------
  38. # Detect our environment
  39. def in_interactive_session():
  40. """
  41. Check if we're running in an interactive shell.
  42. Returns
  43. -------
  44. bool
  45. True if running under python/ipython interactive shell.
  46. """
  47. from pandas import get_option
  48. def check_main():
  49. try:
  50. import __main__ as main
  51. except ModuleNotFoundError:
  52. return get_option("mode.sim_interactive")
  53. return not hasattr(main, "__file__") or get_option("mode.sim_interactive")
  54. try:
  55. return __IPYTHON__ or check_main() # noqa
  56. except NameError:
  57. return check_main()
  58. def in_ipython_frontend():
  59. """
  60. Check if we're inside an an IPython zmq frontend.
  61. Returns
  62. -------
  63. bool
  64. """
  65. try:
  66. ip = get_ipython() # noqa
  67. return "zmq" in str(type(ip)).lower()
  68. except NameError:
  69. pass
  70. return False