Julia: which environment variable/setting controls the number of elements printed for an array in the repl?
Asked Answered
D

2

7

When I print

rand(1_000_000)

It prints the first N lines and prints the last N lines. How is this N determined and how do I control this N?

enter image description here

Detailed answered 12/9, 2019 at 0:24 Comment(4)
I don't know if there is an API for setting these values, but this is determined by show which uses displaysize on the IO object. You can get your terminal display size with: displaysize(stdout) for example.Hamhung
@SalchiPapa I would accept this as the answer.Detailed
You can use IOContext to control displaysize.Spurlock
Possible duplicate of Julia: limited printing of large arraysHamhung
E
5

The size comes is calculated by Base.displaysize(::IO), which you can see should report the size of your terminal for stdout, and reports the "standard" size for IOBuffers:

julia> Base.displaysize(stdout)
(19, 81)


julia> Base.displaysize(IOBuffer())
(24, 80)

julia> Base.displaysize()
(24, 80)

This is called in the full show() method for showing arrays at the REPL: show(io::IO, ::MIME"text/plain", X::AbstractArray), inside print_matrix, here:

    if !get(io, :limit, false)
        screenheight = screenwidth = typemax(Int)
    else
        sz = displaysize(io)
        screenheight, screenwidth = sz[1] - 4, sz[2]
    end

https://github.com/NHDaly/julia/blob/879fef402835c1727aac52bafae686b5913aec2d/base/arrayshow.jl#L159-L164

Note though that in that function, io is actually an IOContext, so as @Fengyang Wang describes in this answer: https://mcmap.net/q/798774/-julia-limited-printing-of-large-arrays, you can also manually set the displaysize on the IOContext if you want to control it yourself (updated for julia 1.0):

julia> show(IOContext(stdout, :limit=>true, :displaysize=>(10,10)), MIME("text/plain"), rand(1_000_000))
1000000-element Array{Float64,1}:
 0.5684598962187111
 0.2779754727011845
 0.22165656934386813
 ⋮
 0.3574516963850929
 0.914975294703998

Finally, to close the loop, displaying a value at the REPL turns into show(io, MIME("text/plain"), v), via display: https://github.com/NHDaly/julia/blob/879fef402835c1727aac52bafae686b5913aec2d/base/multimedia.jl#L319

Elise answered 18/9, 2019 at 7:6 Comment(0)
P
1

If you want the setting to stick around in your REPL session:

Base.active_repl.options.iocontext[:displaysize] = (100, 80)

That will tell Julia to use 100 lines and 80 columns. You can go back to the default with:

Base.active_repl.options.iocontext[:displaysize] = displaysize(stdout)

To change the display size for a single call to show, you can do as others have suggested:

show(IOContext(stdout, :limit=>true, :displaysize=>(100,80)),
     MIME("text/plain"),
     thing)
Preliminaries answered 23/3, 2020 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.