-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #139, add API for accessing notebook metadata programmatically
- Loading branch information
1 parent
c9550af
commit b233ca7
Showing
6 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]) | ||
|
||
""" | ||
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[]) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
]) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |