simonschoelly/KittyTerminalImages.jl

How to force display in the middle of a script?

Closed this issue · 2 comments

Thanks for this package, I really love it! I am using it extensively for my daily work, and it has been a great time-saver!

There is one issue I have not been able to solve yet: suppose I have a script that performs calculations and creates plots, like this one:

using Plots
using KittyTerminalImages

f(x) = 3exp(x)

x = 0.0:0.1:1.0
plot(x, f.(x))

println("Minimum value: ", f(0.0))
println("Maximum value: ", f(1.0))

# The script goes on...

Since the plot command is not the last in the script, the plot is not produced and the output is just

julia> include("test.jl")
Minimum value: 3.0
Maximum value: 8.154845485377136

I am quite sure that the problem is easy to solve, yet I have not been able to understand why!

I am glad you like it, that motivates me to continue working on it!

When you call plot in the REPL, that function will return an object and the REPL will then call display on it. So in your script you have to explicitly call display:

f(x) = 3exp(x)

x = 0.0:0.1:1.0
plot(x, f.(x))
display(plot(x, f.(x)))
println() # note the additional println statement

println("Minimum value: ", f(0.0))
println("Maximum value: ", f(1.0))

I also added a println() as otherwise the newline character that is produced when pressing return in the repl is missing and that leads to some weird formatting.

As a side note, you can also call plot(f, x) instead of plot(x, f.(x))

Thank you @simonschoelly , it worked like a charm! If you agree, I can create a PR to explain this trick in the README, in the section named «Usage». I believe it would be useful for other people as well.