Skip to content

Commit

Permalink
Fix #139, add API for accessing notebook metadata programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHatherly committed Jul 22, 2024
1 parent c9550af commit b233ca7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/QuartoNotebookWorker/src/NotebookState.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ..QuartoNotebookWorker

const PROJECT = Ref("")
const OPTIONS = Ref(Dict{String,Any}())
const CELL_OPTIONS = Ref(Dict{String,Any}())

function __init__()
if ccall(:jl_generating_output, Cint, ()) == 0
Expand Down
4 changes: 4 additions & 0 deletions src/QuartoNotebookWorker/src/QuartoNotebookWorker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module QuartoNotebookWorker
export Cell
export expand

export cell_options
export notebook_options


walk(x, _, outer) = outer(x)
walk(x::Expr, inner, outer) = outer(Expr(x.head, map(inner, x.args)...))
Expand Down Expand Up @@ -94,5 +97,6 @@ include("cell_expansion.jl")
include("render.jl")
include("utilities.jl")
include("ojs_define.jl")
include("notebook_metadata.jl")

end
18 changes: 18 additions & 0 deletions src/QuartoNotebookWorker/src/notebook_metadata.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
notebook_options() -> Dict{String,Any}
All Quarto options that are either set by the user within their notebook
frontmatter, or implicitly set by Quarto itself at runtime. Note that this is a
copy of the options used internally and mutation will not affect any other parts
of notebook evaluation.
"""
notebook_options() = deepcopy(NotebookState.OPTIONS[])

Check warning on line 9 in src/QuartoNotebookWorker/src/notebook_metadata.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/notebook_metadata.jl#L9

Added line #L9 was not covered by tests

"""
cell_options() -> Dict{String,Any}
The options for the current cell being evaluated. This is a copy of the options
used internally and mutation will not affect any other parts of notebook
evaluation.
"""
cell_options() = deepcopy(NotebookState.CELL_OPTIONS[])

Check warning on line 18 in src/QuartoNotebookWorker/src/notebook_metadata.jl

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/notebook_metadata.jl#L18

Added line #L18 was not covered by tests
1 change: 1 addition & 0 deletions src/QuartoNotebookWorker/src/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function _render_thunk(
is_expansion_ref::Ref{Bool} = Ref(false);
inline::Bool,
)
NotebookState.CELL_OPTIONS[] = cell_options

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

View check run for this annotation

Codecov / codecov/patch

src/QuartoNotebookWorker/src/render.jl#L42

Added line #L42 was not covered by tests
captured, display_results = with_inline_display(thunk, cell_options)

# Attempt to expand the cell. This requires the cell result to have a method
Expand Down
53 changes: 53 additions & 0 deletions test/examples/notebook_metadata.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: Notebook Metadata
julia:
env: ["KEY=value"]
---

```{julia}
import QuartoNotebookWorker
```

```{julia}
let options = QuartoNotebookWorker.notebook_options()
options["format"]["metadata"]["julia"]["env"] == ["KEY=value"]
end
```

```{julia}
#| key: value
let options = QuartoNotebookWorker.cell_options()
options["key"] == "value"
end
```

```{julia}
struct CustomStruct
content::Any
function CustomStruct(content)
new([
QuartoNotebookWorker.Cell(
n.thunk;
code = get(n, :code, nothing),
options = get(n, :options, Dict()),
) for n in content
])
end
end
QuartoNotebookWorker.expand(cs::CustomStruct) = cs.content
```

```{julia}
#| key: value_1
static_options = QuartoNotebookWorker.cell_options()
CustomStruct([
(;
thunk = function ()
options = QuartoNotebookWorker.cell_options()
static_options["key"] == "value_1" && options["key"] == "value_2"
end,
options = Dict{String,Any}("key" => "value_2"),
),
])
```
11 changes: 11 additions & 0 deletions test/testsets/notebook_metadata.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include("../utilities/prelude.jl")

test_example(joinpath(@__DIR__, "../examples/notebook_metadata.qmd")) do json
cells = json["cells"]

cell = cells[4]
@test cell["outputs"][1]["data"]["text/plain"] == "true"

cell = cells[11]
@test cell["outputs"][1]["data"]["text/plain"] == "true"
end

0 comments on commit b233ca7

Please sign in to comment.