fredrikekre / Literate.jl

Simple package for literate programming in Julia
https://fredrikekre.github.io/Literate.jl
Other
543 stars 64 forks source link

`Literate.markdown` with `execute=true` does not truncate array output #234

Closed goerz closed 9 months ago

goerz commented 9 months ago

Consider the following mwe_document.jl:

# # Minimum Working Example
#
# Print a long array:

rand(1000)

# Print a long matrix:

rand(2, 1000)

Executing this with

using Literate
Literate.markdown("mwe_document.jl", "."; execute=true)

produces a .md file that shows all values of the array and matrix. This is both non-ideal to look at and produces a large file.

In contrast, running the code in a terminal will show the array/matrix truncated, e.g.,

2×1000 Matrix{Float64}:
 0.336248  0.399099  0.91087   0.988818  …  0.679812  0.481316  0.0602483
 0.358077  0.333078  0.928913  0.810229     0.810204  0.830849  0.985508

The number of printed elements depends on the size of the terminal (see displaysize in IOContext).

A Jupyter notebook also truncates the output, apparently using the standard 80x24 terminal size.

Literate should set the IOContext during execution to mimic a standard terminal. The 80x24 size is probably a good default.

I'll have a look to see if I can fix this.

fredrikekre commented 9 months ago

Duplicate of #181

goerz commented 9 months ago

Agreed, it's a duplicate, and I figured out how to use DisplayAs as a workaround:

# # Minimum Working Example
#
# Print a long array:

import DisplayAs  # md

a = rand(1000)
#md a |> DisplayAs.withcontext(:limit => true) #hide

# Print a long matrix:

A = rand(2, 1000)
#md A |> DisplayAs.withcontext(:limit => true) #hide

I might still look into whether this has a better solution, since I agree with https://github.com/fredrikekre/Literate.jl/issues/181#issuecomment-1101959946 that the default output should emulate the REPL.

But the need is less pressing than I thought initially.

Closing in favor of #181