PlutoUI Print only prints last line. How can I make it print normally?

I’m trying to print a multiline figure in Pluto. println only goes to the REPL, and although Print from PlutoUI prints in the notebook, it only prints the last item. That is, if I do

begin
	Print("GOK")
	Print("\n")
	Print("Souffle")
	Print("\n")
	Print("GARK")
end

The result is GAR, with Souffle and GOK being lost. Even printing \n doesn’t help, which is odd.

How can I make Print behave as one would expect, if possible?

1 Like

Pluto shows the return value of each cell in the notebook, not the output of stdout (which is shown in the REPL where Pluto has been started). Thus, it is best to re-arrange your code, e.g. (each line in a separate cell)

"GOK"

"Souffle"

"Gark"

If you really need to show REPL output in a notebook, you can do

PlutoUI.with_terminal() do
   println("foo")
end

In the future, a feature like Logging in notebook by fonsp · Pull Request #437 · fonsp/Pluto.jl · GitHub could be used to display REPL output in a notebook.

That wouldn’t work too well for what I’m doing. That was just a test for a function with a loop that prints out a figure but wasn’t working with Print, and I’d still get overwriting. I can’t write a loop in separate cells. Thanks for reminding me about with_terminal, although it’s awkward when you just need an advancing Print. What PlutoUI needs is a Println function to dupe println the way they now dupe print. Print is really stubborn. I even tried Print(“something\n”) and was sure that would work but it defeated me. I avoided with_terminal since it looked like a do loop and I couldn’t figure it out, but of course just found out it’s a more verbose mapping function. (I’ll stick with x → x^2 since I avoid typing.) :stuck_out_tongue_winking_eye:

You could use an IOBuffer for something like that, works both in a single block and between different cells (not sure if that was what you wanted):

# ╔═╡ 6bb96b04-1adf-43c9-bd2a-2165cb6b50c5
asd = IOBuffer()

# ╔═╡ 5630b586-ff96-4de4-bc31-fdddc56099f7
println(asd,"TEST1")

# ╔═╡ e724a2d8-f0fc-43d6-9df6-43a45236a656
println(asd,"TEST2")

# ╔═╡ 206ea655-5a74-414f-b0af-f353c0b690ec
String(take!(asd)) |> Text

# ╔═╡ f8fac3f5-1dc0-493a-8c74-910675f17d07
begin
	lol = IOBuffer()
	println(lol,"TEXT1")
	println(lol,"TEXT2")
	take!(lol) |> String |> Text
end

Thanks. also works. with_terminal works but for some odd reason is presenting the result in a big, ugly gray box, when I just want a clean pic rendered. Now, I just have to get the mess into a loop, but it’s all fun. IOBuffer just gives me a clean result. Hmm, just tried IOBuffer in a loop and it doesn’t print. Back to square one. I’ll do something else entirely like saving the whole print-image to a matrix, to print in one go, since Print needs some work (like a Println function) Ah, finally figured it out. Just construct a multiline quote:
Print(""“The rain in Spain
falls gently
on the plain.”"")

The rain in Spain
falls gently
on the plain.

That works, and it’s really simple. Now I just have to be sure I can construct a multiline quote with carriage returns, in a loop. Not having any luck with that, but I’ll stubborn it out.

Later: finally got a technique that works, just have to simplify it into a function I put in config

begin
	pho = "The rain\n"; qup = "In Spain\n";blar="Falls gently on the plain.\n"
	dummy = """"""
	for word in [pho,qup,blar]
		dummy *= word
	end
	Print(dummy)
end

The rain
In Spain
Falls gently on the plain.

use Jupyter?

Nice that you found out the solution that suits you, but for posterity’s sake, what was the use case that caused the IOBuffer to not work in your case?

I tried putting it in a for loop and didn’t see any issue:

I do, but I like some of the features of Pluto, esp dynamic HTML output. It has a few oddities I’m getting used to, but Print() needs work. Anyway, here is my final hack to fix Print()

“”“Make PlutoUI’s Print(), print any number of arguments as a vertical list, without disappearing all but the last as it normally does, including numbers and basic vectors. It doesn’t like mixed-type vectors (bad idea anyway), anonymous functions, or complex stuff, but that’s a flaw in string(), not JPrint.”""

function JPrint(args...)
	dummy = """"""
	for arg in args
		dummy *= string(arg)*"\n"
	end
	Print(dummy)
end

JPrint(“bim”,“baw”,“fallifaw”,“fartsy”,“artsy”,7,[3,4,14],[“all”,“good”,“dogs”,“eat”,“shoes”])

bim
baw
fallifaw
fartsy
artsy
7
[3, 4, 14]
[“all”, “good”, “dogs”, “eat”, “shoes”]

Simplified version:

“”“Make PlutoUI’s Print(), print any number of arguments as a vertical list, without disappearing all but the last as it normally does, including numbers and basic vectors. It doesn’t like mixed-type vectors (bad idea anyway), anonymous functions, or complex stuff, but that’s a flaw in string(), not JPrint.”""

function JPrint(args...)
	dummy = """"""
	for arg in args
		dummy *= string(arg)*"\n"
	end
	Print(dummy)
end

JPrint(“bim”,“baw”,“fallifaw”,“fartsy”,“artsy”,7,[3,4,14],[“all”,“good”,“dogs”,“eat”,“shoes”])

bim
baw
fallifaw
fartsy
artsy
7
[3, 4, 14]
[“all”, “good”, “dogs”, “eat”, “shoes”]

Okay, simplest way yet to make Print do the right thing:

“All
good
dogs
eat
shoes” |> Print

All
good
dogs
eat
shoes

Not sure why single quotes are acting like double quotes, but it works.
I’m done fooling with this. Back to learning Julia :smiley: