Skip to content

Commit

Permalink
Fix unpopulated notebook options during worker package init
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHatherly committed Jul 25, 2024
1 parent 796035d commit 82841dc
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ function configure()
kwargs = Dict{Symbol,Any}()
end
if fm.fig_format == "pdf"
CairoMakie.activate!(; type = "png", kwargs...)
kwargs[:type] = "png"

Check warning on line 17 in src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl#L17

Added line #L17 was not covered by tests
else
CairoMakie.activate!(; type = fm.fig_format, kwargs...)
if isa(fm.fig_format, AbstractString)
kwargs[:type] = fm.fig_format

Check warning on line 20 in src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl#L19-L20

Added lines #L19 - L20 were not covered by tests
end
end
CairoMakie.activate!(; kwargs...)

Check warning on line 23 in src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl#L23

Added line #L23 was not covered by tests

return nothing

Check warning on line 25 in src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/ext/QuartoNotebookWorkerCairoMakieExt.jl#L25

Added line #L25 was not covered by tests
end

function __init__()
Expand Down
1 change: 1 addition & 0 deletions src/QuartoNotebookWorker/src/NotebookState.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const CELL_OPTIONS = Ref(Dict{String,Any}())
function __init__()
if ccall(:jl_generating_output, Cint, ()) == 0
PROJECT[] = Base.active_project()
OPTIONS[] = task_local_storage(:QUARTO_NOTEBOOK_WORKER_OPTIONS, Dict{String,Any}())

Check warning on line 14 in src/QuartoNotebookWorker/src/NotebookState.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/NotebookState.jl#L14

Added line #L14 was not covered by tests
define_notebook_module!()
end
end
Expand Down
20 changes: 16 additions & 4 deletions src/QuartoNotebookWorker/src/refresh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@ function refresh!(path, original_options, options = original_options)
return nothing
end

function rget(dict, keys, default)
value = dict
for key in keys
if haskey(value, key)
value = value[key]

Check warning on line 31 in src/QuartoNotebookWorker/src/refresh.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/refresh.jl#L27-L31

Added lines #L27 - L31 were not covered by tests
else
return default

Check warning on line 33 in src/QuartoNotebookWorker/src/refresh.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/refresh.jl#L33

Added line #L33 was not covered by tests
end
end
return value

Check warning on line 36 in src/QuartoNotebookWorker/src/refresh.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/refresh.jl#L35-L36

Added lines #L35 - L36 were not covered by tests
end

function _figure_metadata()
options = NotebookState.OPTIONS[]

fig_width_inch = options["format"]["execute"]["fig-width"]
fig_height_inch = options["format"]["execute"]["fig-height"]
fig_format = options["format"]["execute"]["fig-format"]
fig_dpi = options["format"]["execute"]["fig-dpi"]
fig_width_inch = rget(options, ("format", "execute", "fig-width"), nothing)
fig_height_inch = rget(options, ("format", "execute", "fig-height"), nothing)
fig_format = rget(options, ("format", "execute", "fig-format"), nothing)
fig_dpi = rget(options, ("format", "execute", "fig-dpi"), nothing)

Check warning on line 45 in src/QuartoNotebookWorker/src/refresh.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/refresh.jl#L42-L45

Added lines #L42 - L45 were not covered by tests

if fig_format == "retina"
fig_format = "svg"
Expand Down
3 changes: 2 additions & 1 deletion src/QuartoNotebookWorker/src/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ function render_mimetypes(value, cell_options; inline::Bool = false)
# what the package defines itself.
value = _mimetype_wrapper(value)

to_format = NotebookState.OPTIONS[]["format"]["pandoc"]["to"]
options = NotebookState.OPTIONS[]
to_format = rget(options, ("format", "pandoc", "to"), nothing)

Check warning on line 330 in src/QuartoNotebookWorker/src/render.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L329-L330

Added lines #L329 - L330 were not covered by tests

result = Dict{String,@NamedTuple{error::Bool, data::Vector{UInt8}}}()
# Some output formats that we want to write to need different
Expand Down
6 changes: 6 additions & 0 deletions src/QuartoNotebookWorker/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ using Test, QuartoNotebookWorker
# Just a dummy test for now. We can start adding real tests in follow-up PRs
# that make changes to the worker code.
@test QuartoNotebookWorker.Packages.is_precompiling() == false
@test QuartoNotebookWorker._figure_metadata() == (
fig_width_inch = nothing,
fig_height_inch = nothing,
fig_format = nothing,
fig_dpi = nothing,
)
end
6 changes: 4 additions & 2 deletions src/WorkerSetup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ function test(; exeflags = String[])
write(
file,
"""
pushfirst!(LOAD_PATH, "@stdlib")
import Pkg
popfirst!(LOAD_PATH)
cd($(repr(QNW)))
pushfirst!(LOAD_PATH, $(repr(project)))
import Pkg
Pkg.test("QuartoNotebookWorker")
""",
)
Expand Down
14 changes: 8 additions & 6 deletions src/worker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ function worker_init(f::File, options::Dict)
return quote
push!(LOAD_PATH, $(project))

let QNW = Base.require(
Base.PkgId(
Base.UUID("38328d9c-a911-4051-bc06-3f7f556ffeda"),
"QuartoNotebookWorker",
),
)
let QNW = task_local_storage(:QUARTO_NOTEBOOK_WORKER_OPTIONS, $(options)) do
Base.require(
Base.PkgId(
Base.UUID("38328d9c-a911-4051-bc06-3f7f556ffeda"),
"QuartoNotebookWorker",
),
)
end
global refresh!(args...) = QNW.refresh!($(f.path), $(options), args...)
global render(args...; kwargs...) = QNW.render(args...; kwargs...)
end
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include("utilities/project_precompile.jl")
include("utilities/cleanup.jl")

@testset "QuartoNotebookRunner" begin
include("worker_tests.jl")
for (root, dirs, files) in walkdir(joinpath(@__DIR__, "testsets"))
for each in files
_, ext = splitext(each)
Expand Down
6 changes: 6 additions & 0 deletions test/worker_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Test
import QuartoNotebookRunner

@testset "QuartoNotebookWorker" begin
@test success(QuartoNotebookRunner.WorkerSetup.test())
end

0 comments on commit 82841dc

Please sign in to comment.