Watch folder and plot new data in Makie

Nevermind, I figured out a solution that works and it’s pretty cool.

Here’s my solution and below is a screenshot of it working!

I’ll write up a post on how to do it, since I haven’t seen anyone do anything similar yet.

function run(datadir::String, extension::String)

    fig = Figure()
    sc = display(fig)

    
    xdata = [rand(10)]
    ydata = [rand(10)]
    xs = Observable(xdata[1])
    ys = Observable(ydata[1])
    plotnames = Observable(["nothing"])
    plotname = Observable(plotnames[][1])

    menu = Menu(fig, options = plotnames)
    fig[1, 1] = vgrid!(
        Label(fig, "Data", width = nothing), menu;
        tellheight = false, width = 200
        )
    
    ax = Axis(fig[1, 2], title = plotname)
    line = lines!(ax, xs, ys)

    on(menu.selection) do s
        i = to_value(menu.i_selected)
        xs.val = xdata[i]
        ys[] = ydata[i]
        autolimits!(ax)
        ax.title = plotnames[][i]
    end

    while true
        (file, event) = watch_folder(datadir)

        if endswith(file, extension)

            x, y, name = loadfile(datadir, file, extension)

            if !(name in plotnames[])
                xs.val = x
                ys[] = y
                autolimits!(ax)
                ax.title = name

                push!(xdata, x)
                push!(ydata, y)
                plotnames[] = push!(plotnames[], name)
            end
        end
    end

end

3 Likes