JunoLab / Weave.jl

Scientific reports/literate programming for Julia
http://weavejl.mpastell.com
MIT License
823 stars 94 forks source link

Support GIF from Plots in markdown? with a scratch[FR] #408

Open HaoLi111 opened 3 years ago

HaoLi111 commented 3 years ago

If I save a gif in the folder or path the file goes


/```julia

p2 = Plots.plot!(xlim = (-2,2))
anm = @animate for i in 1:25
    Plots.plot!(x->x^2/i,key = false)
    Plots.title!("x-> (1/ $i) *(x^2)",key = false)
end

gif(anm,"xSqrDivByi.gif")
\```

\```
Plots.AnimatedGif("D:\\animated-adventure-in-mathematics\\ElementaryFnTrans
\\xSqrDivByi.gif")
\```

It seems possible to detect pwd(), which is

Plots.AnimatedGif("D:\\animated-adventure-in-mathematics\\ElementaryFnTrans
\

and just substitute it with

![](

and then check and match fig_path and finally get

![](xSqrDivByi.gif)

so that we can get support for gif link to this relev proj.

HaoLi111 commented 3 years ago

Just realized that the hard part is the line folds when it is too long. Maybe if we detect gif() usage it would be better

torfjelde commented 3 years ago

Btw, if you really need this, this hack ought to do (am currently using it myself):

using Plots

function Weave.WeavePlots.add_plots_figure(report::Weave.Report, plot::Plots.AnimatedGif, ext)
    chunk = report.cur_chunk
    full_name, rel_name = Weave.get_figname(report, chunk, ext = ext)

    # A `AnimatedGif` has been saved somewhere temporarily, so make a copy to `full_name`.
    cp(plot.filename, full_name; force = true)
    push!(report.figures, rel_name)
    report.fignum += 1
    return full_name
end

function Base.display(report::Weave.Report, m::MIME"text/plain", plot::Plots.AnimatedGif)
    Weave.WeavePlots.add_plots_figure(report, plot, ".gif")
end

or if you have a package which uses Weave.jl, in which you need the following:

using Requires, Plots
function __init__()
    @require Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" begin
        # HACK
        function Weave.WeavePlots.add_plots_figure(report::Weave.Report, plot::Plots.AnimatedGif, ext)
            chunk = report.cur_chunk
            full_name, rel_name = Weave.get_figname(report, chunk, ext = ext)

            # A `AnimatedGif` has been saved somewhere temporarily, so make a copy to `full_name`.
            cp(plot.filename, full_name; force = true)
            push!(report.figures, rel_name)
            report.fignum += 1
            return full_name
        end

        function Base.display(report::Weave.Report, m::MIME"text/plain", plot::Plots.AnimatedGif)
            Weave.WeavePlots.add_plots_figure(report, plot, ".gif")
        end
    end
end