Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Internal module for console introspection 

3""" 

4 

5from shutil import get_terminal_size 

6 

7 

8def get_console_size(): 

9 """ 

10 Return console size as tuple = (width, height). 

11 

12 Returns (None,None) in non-interactive session. 

13 """ 

14 from pandas import get_option 

15 

16 display_width = get_option("display.width") 

17 display_height = get_option("display.max_rows") 

18 

19 # Consider 

20 # interactive shell terminal, can detect term size 

21 # interactive non-shell terminal (ipnb/ipqtconsole), cannot detect term 

22 # size non-interactive script, should disregard term size 

23 

24 # in addition 

25 # width,height have default values, but setting to 'None' signals 

26 # should use Auto-Detection, But only in interactive shell-terminal. 

27 # Simple. yeah. 

28 

29 if in_interactive_session(): 

30 if in_ipython_frontend(): 

31 # sane defaults for interactive non-shell terminal 

32 # match default for width,height in config_init 

33 from pandas._config.config import get_default_val 

34 

35 terminal_width = get_default_val("display.width") 

36 terminal_height = get_default_val("display.max_rows") 

37 else: 

38 # pure terminal 

39 terminal_width, terminal_height = get_terminal_size() 

40 else: 

41 terminal_width, terminal_height = None, None 

42 

43 # Note if the User sets width/Height to None (auto-detection) 

44 # and we're in a script (non-inter), this will return (None,None) 

45 # caller needs to deal. 

46 return (display_width or terminal_width, display_height or terminal_height) 

47 

48 

49# ---------------------------------------------------------------------- 

50# Detect our environment 

51 

52 

53def in_interactive_session(): 

54 """ 

55 Check if we're running in an interactive shell. 

56 

57 Returns 

58 ------- 

59 bool 

60 True if running under python/ipython interactive shell. 

61 """ 

62 from pandas import get_option 

63 

64 def check_main(): 

65 try: 

66 import __main__ as main 

67 except ModuleNotFoundError: 

68 return get_option("mode.sim_interactive") 

69 return not hasattr(main, "__file__") or get_option("mode.sim_interactive") 

70 

71 try: 

72 return __IPYTHON__ or check_main() # noqa 

73 except NameError: 

74 return check_main() 

75 

76 

77def in_ipython_frontend(): 

78 """ 

79 Check if we're inside an an IPython zmq frontend. 

80 

81 Returns 

82 ------- 

83 bool 

84 """ 

85 try: 

86 ip = get_ipython() # noqa 

87 return "zmq" in str(type(ip)).lower() 

88 except NameError: 

89 pass 

90 

91 return False