JuliaDocs / DemoCards.jl

Let's focus on writing demos
MIT License
65 stars 14 forks source link

Make Pluto optional #138

Closed Dsantra92 closed 1 year ago

Dsantra92 commented 1 year ago

Introduce Requires and move Pluto dependency to optional. Close #130

johnnychen94 commented 1 year ago

Since DemoCards mainly runs on CI(where latency isn't a big deal), Requires is an okay solution already. But if you'd like to take a try, https://pkgdocs.julialang.org/dev/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions) is a good "official" solution. An extra benefit using Extensions is that we can manage Pluto*'s compatibility.

Dsantra92 commented 1 year ago

Faced an interesting problem while following the official example

if !isdefined(Base, :get_extension)
using Requires
# print("Require import block reached")
# end

# if !isdefined(Base, :get_extension)
# print("Require __init__ block reached")
function __init__()
    @require Pluto="c3e4b0f8-55cb-11ea-2926-15256bba5781" begin
      @require PlutoStaticHTML = "359b1769-a58e-495b-9770-312e911026ad" include("../ext/PlutoNotebook.jl")
    end
end
end

Results in an error ERROR: LoadError: UndefVarError: `@require` not defined

StackTrace ```julia Stacktrace: [1] top-level scope @ :0 [2] include @ ./Base.jl:456 [inlined] [3] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt128}}, source::Nothing) @ Base ./loading.jl:1952 [4] top-level scope @ stdin:2 in expression starting at /home/dsantra/DemoCards.jl/src/DemoCards.jl:60 in expression starting at /home/dsantra/DemoCards.jl/src/DemoCards.jl:1 in expression starting at stdin:2 ERROR: Failed to precompile DemoCards [311a05b2-6137-4a5a-b473-18580a3d38b5] to "/home/dsantra/.julia/compiled/v1.9/DemoCards/jl_j7FQ0Y". Stacktrace: [1] error(s::String) @ Base ./error.jl:35 [2] compilecache(pkg::Base.PkgId, path::String, internal_stderr::IO, internal_stdout::IO, keep_loaded_modules::Bool) @ Base ./loading.jl:2195 [3] compilecache @ ./loading.jl:2068 [inlined] [4] _require(pkg::Base.PkgId, env::String) @ Base ./loading.jl:1712 [5] _require_prelocked(uuidkey::Base.PkgId, env::String) @ Base ./loading.jl:1567 [6] macro expansion @ ./loading.jl:1555 [inlined] [7] macro expansion @ ./lock.jl:267 [inlined] [8] require(into::Module, mod::Symbol) @ Base ./loading.jl:1518 ```

Surprisingly works with comments removed, i.e. requires loaded as a separate if else blocks.

if !isdefined(Base, :get_extension)
using Requires
print("Require import block reached")
end

if !isdefined(Base, :get_extension)
print("Require __init__ block reached")
function __init__()
    @require Pluto="c3e4b0f8-55cb-11ea-2926-15256bba5781" begin
      @require PlutoStaticHTML = "359b1769-a58e-495b-9770-312e911026ad" include("../ext/PlutoNotebook.jl")
    end
end
end
julia> using DemoCards
[ Info: Precompiling DemoCards [311a05b2-6137-4a5a-b473-18580a3d38b5]
Democards dir
Require import block reached
Require __init__ block reached

Any idea why this is happening? Similar problems are happening for julia 1.9 beta3.

johnnychen94 commented 1 year ago

I'm not sure. It's perhaps because macros are expanded first?

Dsantra92 commented 1 year ago

I decided to leave PlutoDemoCard in the src folder. It might not make sense, now that we are loading Pluto Notebook as an extension. But I didn't want to change the original democard function: https://github.com/JuliaDocs/DemoCards.jl/blob/d89e89d107b7fa70a1bf5670c875e7066c8e862e/src/types/card.jl#L20-L34

t-bltg commented 1 year ago

Awesome, thanks !