Unabridged printing of long strings, either in a Vector or in a DataFrame?

Sometimes when I’m working with long strings, usually in the form of a Vector{String}, I’d like to be able to see them in their entirety in the REPL, even if each one gets wrapped to multiple lines. Is there an easy way to override the default behavior and do this?

julia> mystrings = [repeat("abc", 64); repeat("def", 64); repeat("ghi", 64)]
3-element Vector{String}:
 "abcabcabcabcabcabcabcabcabcabca" ⋯ 131 bytes ⋯ "abcabcabcabcabcabcabcabcabcabc"
 "defdefdefdefdefdefdefdefdefdefd" ⋯ 131 bytes ⋯ "defdefdefdefdefdefdefdefdefdef"
 "ghighighighighighighighighighig" ⋯ 131 bytes ⋯ "ghighighighighighighighighighi"

Also, sometimes I have a column in a DataFrame that contains a long string, and I might wish for a print method that wraps the text in that column.

julia> using DataFrames
julia> mytest = DataFrame(col1 = 1:3, col2 = mystrings)
3×2 DataFrame
 Row │ col1   col2                              
     │ Int64  String                            
─────┼──────────────────────────────────────────
   1 │     1  abcabcabcabcabcabcabcabcabcabcab…
   2 │     2  defdefdefdefdefdefdefdefdefdefde…
   3 │     3  ghighighighighighighighighighigh…

Thanks in advance if anyone has advice on achieving one or both of these things.

I think the answer to the first scenario is probably something like:


function Base.show(io::IO, ::MIME"text/plain", v::Vector{String})
    for i in eachindex(v)
        println(v[i])
    end
end

You can use PrettyTables.jl:

using DataFrames, PrettyTables
mytest = DataFrame(col1=1:3, col2=mystrings)
pretty_table(mytest,columns_width=[8,60], autowrap=true, crop=:none)

For vectors of long strings, simply do: println.(mystrings);

1 Like

PrettyTables.jl is re-exported by DataFrames.jl, so one could just call PrettyTables.pretty_table. Alternatively you also can just call show as DataFrames.jl uses PrettyTables.jl internally:

julia> show(mytest,columns_width=[8,60], autowrap=true)
3×2 DataFrame
 Row │ col1      col2
     │ Int64     String
─────┼────────────────────────────────────────────────────────────────────────
   1 │        1  abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc
     │           abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc
     │           abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc
     │           abcabcabcabc
   2 │        2  defdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdef
     │           defdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdef
     │           defdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdefdef
     │           defdefdefdef
   3 │        3  ghighighighighighighighighighighighighighighighighighighighi
     │           ghighighighighighighighighighighighighighighighighighighighi
     │           ghighighighighighighighighighighighighighighighighighighighi
     │           ghighighighi

@Ronis_BR - I think something along the lines of this example would be nice to include in the manual section on printing, as it is a quite common need.

4 Likes

I think we also need to pass: crop=:none, otherwise if the table is long or the REPL height is short, it will not print all rows.

1 Like

Thanks to you both, I wasn’t aware of PrettyTables.

Yes! I really need to find time to write that documentation. I am sorry for the delay. I entered a rabbit hole of splitting SatelliteToolbox.jl into smaller packages that took way more time than anticipated. Everything is almost finished and I will be able to take care of other packages really soon :slight_smile: