PyCall.jl icon indicating copy to clipboard operation
PyCall.jl copied to clipboard

Misalignment of first line in multiline show of PyObject

Open olof3 opened this issue 3 years ago • 2 comments

PyObjects whose pystring method returns a multiline string have their first row misaligned when shown. For example

using PyCall
qiskit = pyimport("qiskit")
qc = qiskit.QuantumCircuit(1)
qc.x(0)
qc.draw()

gives

PyObject      ┌───┐
q_0: ┤ X ├
     └───┘

A fix would be to replace

function show(io::IO, o::PyObject)
    print(io, "PyObject $(pystring(o))")
end

with

function show(io::IO, o::PyObject)
    str = pystring(o)
    sep = contains(str, "\n") ? "\n" : " " # Avoid misaligning the first line for multiline prints
    print(io, "PyObject$sep$str")
end

which gives the following print instead.

PyObject
     ┌───┐
q_0: ┤ X ├
     └───┘

olof3 avatar Mar 24 '21 10:03 olof3

Can you give an example?

stevengj avatar Mar 24 '21 15:03 stevengj

There was some kind of example in the original post, but I've updated it to make it more clear.

olof3 avatar Mar 24 '21 19:03 olof3