Just re-assigning Base.stdout
or calling redirect
won’t accomplish this, because the default display backend saves a reference to the original stdout
. (Also, the whole point of display
is that the output can be specific to your enviroment, e.g. some objects might display graphically in Jupyter or Pluto or similar environments.)
The right way to capture display
output is to call pushdisplay
to push a new display backend to the top of the stack — this will get called first for any code that invokes display(x)
, allowing you to do whatever you want, displaying objects as text or other supported formats as desired, or capturing the output in a string or array. This is what IJulia does, for example. It’s also what Pluto.jl does.
However, I want to step back a bit. Why are you doing this? Are you creating a new interactive environment for Julia? If not, i.e. for non-interactive use, I wouldn’t recommend communicating with libjulia
via strings, and especially not by capturing stdout
— better to pass data back and forth directly, in native formats.