Makie doesn't show anything when run as a script

I am trying to plot something using Makie in the test.jl file. But it doesn’t show figure when using julia test.jl.

Here is my code:

using GLMakie
xs = cos.(1:0.5:20)
ys = sin.(1:0.5:20)
zs = LinRange(0, 3, length(xs))
a=meshscatter(xs, ys, zs, markersize = 0.1, color = zs)
display(a)

Thanks for any suggestions.

run it with

julia -i test.jl

basically you need to stop Julia from exiting

thank you, it works by adding “-i”. But I am curious what is the propose of “-i”, it looks like force julia go to the REPL, because after I ran above commend, it jumped into the REPL. I am wondering could I stay in my previous environment. Thanks

for the display window to stay open, the Julia process can’t exit. You can also put a readline() after the display() but you still won’t be able to use that terminal session.

-i puts you into interactive mode (REPL) after executing the script, which is a way to prevent exiting

I see, thank you. I am wondering if I add Scene() whether it could help me to stay on terminal. Thanks

You can also prevent exit by sleeping on the initial task:

using GLMakie

function main()
    fig = Figure()
    ax = Axis3(fig[1,1])
    xs = cos.(1:0.5:20)
    ys = sin.(1:0.5:20)
    zs = LinRange(0, 3, length(xs))
    a = meshscatter!(ax, xs, ys, zs, markersize = 0.1, color = zs)
    exit = Observable(false)
    exit_button = Button(fig[2,1]; label="Quit", tellwidth=false, tellheight=false)
    on(exit_button.clicks) do _
        exit[] = true
    end
    display(fig)
    while !exit[]
        sleep(0.01)
    end
end

main()

Edit In response to the comment below from @albheim, that is indeed a much simpler solution.

using GLMakie

function main()
    fig = Figure()
    ax = Axis3(fig[1,1])
    xs = cos.(1:0.5:20)
    ys = sin.(1:0.5:20)
    zs = LinRange(0, 3, length(xs))
    a = meshscatter!(ax, xs, ys, zs, markersize = 0.1, color = zs)
    wait(display(fig))
end

main()

On the phone so haven’t tried, but I believe you can also call wait on the return from display to wait until the window decides it is done. E.g. wait(display(fig))

3 Likes