Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/releasenotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes

## Unreleased
* Showing `Py` now respects the `compact` option - output is limited to a single line of
at most the display width.
* Bug fixes.

## 0.9.28 (2025-09-17)
* Added `NumpyDates`: NumPy-compatible DateTime64/TimeDelta64 types and units.
* Added `pyconvert` rules for NumpyDates types.
Expand Down
33 changes: 29 additions & 4 deletions src/Core/Py.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,23 @@ function Base.show(io::IO, ::MIME"text/plain", o::Py)
end
hasprefix = (get(io, :typeinfo, Any) != Py)::Bool
compact = get(io, :compact, false)::Bool
limit = get(io, :limit, true)::Bool
if compact
# compact should output a single line, which we force by replacing newline
# characters with spaces
str = replace(str, "\n" => " ")
end
multiline = '\n' in str
prefix =
hasprefix ?
compact ? "Py:$(multiline ? '\n' : ' ')" : "Python:$(multiline ? '\n' : ' ')" : ""
prefix = !hasprefix ? "" : compact ? "Py: " : multiline ? "Python:\n" : "Python: "
print(io, prefix)
h, w = displaysize(io)
if get(io, :limit, true)
if limit
# limit: fit the printed text into the display size
h, w = displaysize(io)
h = max(h - 3, 5) # use 3 fewer lines to allow for the prompt, but always allow at least 5 lines
if multiline
# multiline: we truncate each line to the width of the screen, and skip
# middle lines if there are too many for the height
h -= 1 # for the prefix
lines = split(str, '\n')
function printlines(io, lines, w)
Expand Down Expand Up @@ -218,7 +225,25 @@ function Base.show(io::IO, ::MIME"text/plain", o::Py)
println(io)
printlines(io, lines[end-h1+1:end], w)
end
elseif compact
# compact: we print up to one screen width, skipping characters in the
# middle if the string is too long for the width
maxlen = w - length(prefix)
if length(str) ≤ maxlen
print(io, str)
else
gap = " ... "
gaplen = length(gap)
w0 = cld(maxlen - gaplen, 2)
i0 = nextind(str, 1, w0 - 1)
i1 = prevind(str, ncodeunits(str), maxlen - gaplen - w0 - 1)
print(io, str[begin:i0])
printstyled(io, gap, color = :light_black)
print(io, str[i1:end])
end
else
# single-line: we print up to one screenfull, skipping characters in the
# middle if the string is too long. We skip a whole line of characters.
maxlen = h * w - length(prefix)
if length(str) ≤ maxlen
print(io, str)
Expand Down
3 changes: 3 additions & 0 deletions test/Core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,9 @@ end
@test sprint(show, MIME("text/plain"), Py(12)) == "Python: 12"
# https://github.com/JuliaPy/PythonCall.jl/issues/522
@test sprint(show, MIME("text/plain"), PythonCall.pynew()) == "Python: NULL"
# test compact printing
@test sprint(show, MIME("text/plain"), Py(String('A':'Z')), context=(:compact=>true, :displaysize=>(50, 20))) == "Py: 'ABCDE ... WXYZ'"
@test sprint(show, MIME("text/plain"), Py(String('A':'Z')), context=(:compact=>true, :limit=>false, :displaysize=>(50, 20))) == "Py: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"
@test_throws MethodError sprint(show, MIME("text/html"), PythonCall.pynew())
end
end
Expand Down
Loading