Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python -- How do you view output that doesn't fit the screen?

Tags:

python

I should say I'm looking for a solution to the problem of viewing output that does not fit on your screen. For example, range(100) will show the last 30ish lines in your terminal of 30 height.

I am only looking to be nudged in the right direction and am curious how you guys have approached this problem.

What have you done when you run into a situation where you wish you could conveniently scroll through some large output?

Best Answer

Use the scrollback buffer on your terminal.

If you're using GNU Screen, it can be set with defscrollback 1000 or any other number in HOME/.screenrc.

Use Ctrl-a, [ to enter copy mode

j -    Move the cursor down by one line
k -    Move the cursor up by one line
C-u -  Scrolls a half page up.
C-b -  Scrolls a full page up.
C-d -  Scrolls a half page down.
C-f -  Scrolls the full page down.
G -    Moves to the specified line 

The best part is ? for reverse search, / for forward search while in copy mode.

Super convenient.

Thank you!


Original question:

What is the python equivalent of the bash less command?

LongString | less 

Is there something like that for python? I find myself thinking I could use it fairly often but just move on and find some other solution.

By "long things" I mean anything that generates more output lines than my screen. 1000 print messages, a dictionary, a large string, range(1000), etc.

My googlefu has failed.

like image 416
Yuji 'Tomita' Tomita Avatar asked Jul 22 '10 02:07

Yuji 'Tomita' Tomita


People also ask

How do you display output in Python?

We can use print() function in Python to display some message or output on Python shell. Here EXPRESSION is the variable of any data type or string value that we want to display.

How do I clear the output screen in Python?

system('cls') to clear the screen. The output window will print the given text first, then the program will sleep for 4 seconds and then screen will be cleared and program execution will be stopped.

Why is Python not showing output?

One of the reasons of why your Python script does not show any output is because it buffers stdout and stderr steams instead of printing them. This also can happen if you execute the Python script from a CI/CD pipeline (e.g. using Jenkins, Gitlab-CI, TeamCity, etc.) or if you run it using a Dockerfile .

How do you get output to the console in Python?

Print to Console in Python. To print strings to console or echo some data to console output, use Python inbuilt print() function. print() function can take different type of values as argument(s), like string, integer, float, etc., or object of a class type.


4 Answers

just for fun :o)

Original version for Python2:

class Less(object):
    def __init__(self, num_lines):
        self.num_lines = num_lines
    def __ror__(self, other):
        s = str(other).split("\n")
        for i in range(0, len(s), self.num_lines):
            print "\n".join(s[i: i + self.num_lines])
            raw_input("Press <Enter> for more")

less = Less(num_lines=30)  
"\n".join(map(str, range(100))) | less

a Python3 version:

class Less(object):
    def __init__(self, num_lines):
        self.num_lines = num_lines
    def __ror__(self, other):
        s = str(other).split("\n")
        for i in range(0, len(s), self.num_lines):
            print(*s[i: i + self.num_lines], sep="\n")
            input("Press <Enter> for more")

less = Less(num_lines=30)  
"\n".join(map(str, range(100))) | less
like image 72
John La Rooy Avatar answered Sep 28 '22 14:09

John La Rooy


If you want to do this for an interactive python session, you should use a terminal emulation that allows you to scroll back up. I believe most of them do.

If you're using an actual terminal, or if you don't have a choice of terminal emulators, perhaps you can use GNU screen.

(If you're using Windows, you can change the screen buffer size to allow scrolling back up to 9999 lines).

And if you need this for the output of your program, you can try to use the curses module to implement scrolling yourself.

like image 30
itsadok Avatar answered Sep 28 '22 16:09

itsadok


Yes, there is a way and it is quite trivial, that's why not specifically described. Say you have long list and want to see just the beginning:

>>> lst = range(1000)   # let's make list of thousand elements
>>> lst[:100]           # i want to see just the first 100
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> lst[100:201]        # ok, now the 2nd hundred 
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200]
>>> lst[-100:]          # and now just the last 100, pretty please?
[900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999]

The same [] slicing works the same for strings. For dictionary you can cheat somehow, like using str() to convert it to string and then display with the same slicing :-)

like image 24
Nas Banov Avatar answered Sep 28 '22 15:09

Nas Banov


Say you're writing a program in Python and all it does is pretty print some stuff. The output is in prettiest_print_ever. You already do weird tricks importing fcntl, termios, struct and friends to get the terminal size so that you can use the full width of the terminal (if any); that also gives you the screen height, so it makes sense to use it. (That also means you've long given up any pretenses of cross-platform compatibility, too.)

Sure, you can reinvent the wheel, or you can rely on less like other programs do (e.g. git diff). This should be the outline of a solution:

def smart_print(prettiest_print_ever, terminal_height = 80):
  if len(prettiest_print_ever.splitlines()) <= terminal_height:
    #Python 3: make sure you write bytes!
    sys.stdout.buffer.write(prettiest_print_ever.encode("utf-8"))
  else
    less = subprocess.Popen("less", stdin=subprocess.PIPE)
    less.stdin.write(prettiest_print_ever.encode("utf-8"))
    less.stdin.close()
    less.wait()
like image 26
badp Avatar answered Sep 28 '22 15:09

badp