# Livereload with Weave.jl

**URL:** <https://discourse.julialang.org/t/livereload-with-weave-jl/36750>\
**Category:** Tooling\
**Created:** [March 30, 2020, 4:55pm UTC](https://discourse.julialang.org/t/livereload-with-weave-jl/36750 "2020-03-30T16:55:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpf01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpf01/32/8221_2.png) [@mpf01](https://discourse.julialang.org/u/mpf01)\
**Post date:** [March 30, 2020, 4:55pm UTC](https://discourse.julialang.org/t/livereload-with-weave-jl/36750/1 "2020-03-30T16:55:18Z")

</div>

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](https://asprionj.github.io/LiveServer.jl/stable/man/ls+lit/), but for Literate.jl, rather than Weave.jl.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [March 30, 2020, 5:11pm UTC](https://discourse.julialang.org/t/livereload-with-weave-jl/36750/2 "2020-03-30T17:11:08Z")

</div>

You could add your own hook (callback) on LiveServer (see [here](https://asprionj.github.io/LiveServer.jl/stable/man/extending_ls/#Using-SimpleWatcher-with-a-custom-callback-1)); 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)

---

<div class="post-metadata">

**Author:** ![mpf01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpf01/32/8221_2.png) [@mpf01](https://discourse.julialang.org/u/mpf01)\
**Post date:** [March 31, 2020, 7:50pm UTC](https://discourse.julialang.org/t/livereload-with-weave-jl/36750/3 "2020-03-31T19:50:24Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [April 1, 2020, 7:24am UTC](https://discourse.julialang.org/t/livereload-with-weave-jl/36750/4 "2020-04-01T07:24:52Z")

</div>

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 😄

---

<div class="post-metadata">

**Author:** ![Markus\_Hauschel](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Markus\_Hauschel](https://discourse.julialang.org/u/Markus_Hauschel)\
**Post date:** [April 1, 2020, 7:39pm UTC](https://discourse.julialang.org/t/livereload-with-weave-jl/36750/5 "2020-04-01T19:39:14Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![mpf01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mpf01/32/8221_2.png) [@mpf01](https://discourse.julialang.org/u/mpf01)\
**Post date:** [April 2, 2020, 5:28am UTC](https://discourse.julialang.org/t/livereload-with-weave-jl/36750/6 "2020-04-02T05:28:09Z")

</div>

Please see this [PR](https://github.com/asprionj/LiveServer.jl/pull/97).
