Livereload with Weave.jl

Are there existing tools that, on saving in the editor (I use Spacemacs), would automatically run weave and reload the browser to view the latest output?

I see that LiveServer.jl has this functionality, but for Literate.jl, rather than Weave.jl.

You could add your own hook (callback) on LiveServer (see here); if you have a look at the docs, it shows what you can do upon file events; so upon one of those you could just re-trigger weave.

Note that you have to bear in mind what weave creates so that you don’t get an infinite loop: if you look at the source file source.jl and it generates output.md (or something of the sorts) then you should make sure that the generated output.md doesn’t itself re-trigger the full loop; you can have the watcher ignore certain files or folders which should do the trick

(Note that adapting the stuff from Literate to Weave should be reasonably straightforward, feel free to open an issue if you get stuck)

Thanks for the pointer, @tlienart. This seems to do the job, but please say if I’ve missed anything important:

using LiveServer
using Weave
LS = LiveServer

function weave_callback!(fp; kwargs...)
    weave(fp; kwargs...)
    LS.file_changed_callback(fp)
    return nothing
end

function serveweave(file::AbstractString;
                    verbose::Bool=false,
                    kwargs...)
    dw = LS.SimpleWatcher(fp->weave_callback!(fp; kwargs...))
    push!(dw.watchedfiles, LS.WatchedFile(file))
    serve(dw, verbose=verbose)
    return nothing
end
1 Like

looks good, a few notes:

  1. (nitpicking) use const LS = LiveServer
  2. make sure you check which files should trigger the callback upon modification and which shouldn’t (see my previous message)

Otherwise looks good, it could be a nice PR to LiveServer too once you’re happy with your setup :smile:

1 Like

This is nice! Need to check it out ASAP!!! Currently I set up a local server in R and view the output in a browser. Works perfectly fine but a pure Julia version is much better!
Edit: And I need to run the weave command manually… I failed upon setting up a shortcut for the weave button a couple of days ago…

Please see this PR.

1 Like