Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #33, support include in notebooks #34

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
matrix:
version:
- "1.6"
- "1.10"
- "1.10.0"
os:
- ubuntu-latest
- macos-latest
Expand Down
25 changes: 25 additions & 0 deletions src/worker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ function worker_init(f::File)
const WORKSPACE = Ref(Module(:Notebook))
const OPTIONS = Ref(Dict{String,Any}())

# Written as an `eval` call to avoid restriction of the `baremodule`
# expression requiring being written as a top-level expression since this
# entire file is wrapped in a `quote` block.
Core.eval(
Main,
:(
baremodule NotebookInclude

import Base, Core, Main

# As defined by `MainInclude` to replicate the behaviour of
# the `Main` module in the REPL.
function include(fname::Base.AbstractString)
isa(fname, Base.String) ||
(fname = Base.convert(Base.String, fname)::Base.String)
Base._include(Base.identity, Main.WORKSPACE[], fname)
end
eval(x) = Core.eval(Main.WORKSPACE[], x)

end
),
)

# Interface:

function refresh!(options = OPTIONS[])
Expand Down Expand Up @@ -65,6 +88,8 @@ function worker_init(f::File)
# Ensure that `Pkg` is always available in the notebook so that users
# can immediately activate a project environment if they want to.
Core.eval(WORKSPACE[], :(import Main: Pkg, ojs_define))
# Custom `include` and `eval` implementation to match behaviour of the REPL.
Core.eval(WORKSPACE[], :(import Main.NotebookInclude: include, eval))

# Rerun the package loading hooks if the options have changed.
if OPTIONS[] != options
Expand Down
7 changes: 7 additions & 0 deletions test/examples/include.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: "`include`ing files"
---

```{julia}
include("script.jl")
```
14 changes: 14 additions & 0 deletions test/testsets/include.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include("../utilities/prelude.jl")

test_example(joinpath(@__DIR__, "../examples/include.qmd")) do json
cells = json["cells"]
@test length(cells) == 2

cell = cells[2]
@test cell["cell_type"] == "code"
@test cell["execution_count"] == 1
@test cell["outputs"][1]["output_type"] == "stream"
@test cell["outputs"][1]["name"] == "stdout"
@test cell["outputs"][2]["output_type"] == "execute_result"
@test contains(cell["outputs"][2]["data"]["text/plain"], "10×10")
end
Loading