Weave.jl icon indicating copy to clipboard operation
Weave.jl copied to clipboard

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

Open HaoLi111 opened this issue 4 years ago • 2 comments

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 avatar Nov 26 '20 12:11 HaoLi111

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

HaoLi111 avatar Nov 26 '20 13:11 HaoLi111

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

torfjelde avatar Mar 06 '21 18:03 torfjelde