More, less, head, tail, to display arrays in REPL

Is there any functionality which mimics the head, tail, more and less command in bash, but to visualize the contents of arrays?

That would be quite useful, I think, for rapid inspection of data. Currently, the Base.display() function for arrays fills up the screen (something I find sometimes annoying when I forget to put the ;). It would be neat if

  1. one could set an environment variable to set the maximum number of lines printed by Base.display() of any array, and
  2. be able to navigate the content of the array using more and less and equivalents.

Did someone write something like that already?

Thank you.

8 Likes

One issue could be how does it work for multidimensional objects? 1D vs 2D vs 3D, etc, arrays. What does head(A,10) mean when A has 3 (or more) dimensions?

2 Likes

That is true. Maybe someone can figure out something that returns meaningful and useful data in those cases. I was thinking about 2D arrays only, in reality (files are bidimensional).

Maybe a first(x, n) method and a last(x, n) method could be added to first and last, respectively. There used to be something like that in DataFrames.jl, but then they decided to improve the default printing of DataFrames instead.

1 Like

That actually does work for DataFrames.

1 Like

Usual ordering by columns?

Right but do you take 10 from each dimension? Or 10 from only the first dimension and don’t limit the others (this is basically what OP had in mind in 2D)? Or perhaps it should just take a tuple instead of an integer?

Actually that is already taken care of in the usual display of multi-dimensional arrays. I do not know if it is the best solution of all, but what I am imagining here is just an extension of that display.

more / less would require interactivity. Specific packages exist eg for the Tables.jl ecosystem.

head / tail is useful for compact printing, but generally that is taken care of if show methods respect the compact property from the IOContext. When they don’t, you should open an issue.

1 Like

Yes. I was taking a look and this package https://github.com/timholy/HeaderREPLs.jl/blob/master/src/HeaderREPLs.jl appears to provide the building blocks to start with. I will play with it when I have some time.

True, I would like to have more control on the amount of data being printed though.

1 Like

In what way? Base.print_matrix tries to adapt to screen size, but if you know what you are looking for, why not just print a slice using :?

That’s already implemented in the upcoming Julia 1.6.

4 Likes

It would be nice if there were an environment variable to customize this. Often if I have the REPL full screen, I don’t want the printing of a matrix or a table to take up the entire screen.

For example, if I’m full screen, it might be nice to customize printing so that the number of rows displayed is never more than 20 (10 from the head and 10 from the tail).

Even better, the logic could be this:

  • If x fits on the screen, then print all of x.
  • Otherwise, print as many lines of x as will fit on the screen, subject to the restriction that num_lines ≤ MAX_SHOW_LINES.
5 Likes

Exactly, this is one of the things I would like to have.

2 Likes

Yes, this would be nice. Perhaps check if there is an open issue about this and open one if there isn’t, so that eventually when someone redesigns printing it is considered.

That said, printing arrays is a bit tricky because the elements could be arbitrary objects with a large printed representation (even with :compact and :limit set), so it is easy to come up with corner cases.

It seems that the number of lines is simply read from the output of displaysize(). Modifying one of the “show” functions to display the minimum between that and a environment variable seems easy (it doesn’t seem to require a major redesign). For an outsider the problem is to be sure that the modification has been made in every of the many show functions that are available, consistently.

1 Like

I find numpy’s default behaviour for printing arrays (3 rows/cols from the beginning/end of the array) to be more convenient/pretty than Julia’s Base.display, which fills up the entire display. What’s the reason behind the default preference in Julia?

It all boils down to an aesthetic choice and weighting of importance between showing more data vs. preserving screen space. Preferences may and do vary — note that the original poster simultaneously wanted both more data shown and less data shown in different contexts.

1 Like

side gripe: numpy doesn’t show you the eltype though :frowning:

Thanks for weighing in. Is it possible that this is going to be revisited in future releases (if there’s enough interest by majority of the community)?