Skip to content

Commit

Permalink
Fix #143, pass :module to IOCapture.capture (#149)
Browse files Browse the repository at this point in the history
* Fix #143, pass `:module` to `IOCapture.capture`

* Compat bound for `IOCapture`
  • Loading branch information
MichaelHatherly authored Jun 13, 2024
1 parent f007917 commit 7eb713b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
Base64 = "1.6"
CommonMark = "0.8"
Compat = "4"
IOCapture = "0.2"
IOCapture = "0.2.5"
InteractiveUtils = "1.6"
IterTools = "1"
JSON3 = "1"
Expand Down
25 changes: 19 additions & 6 deletions src/QuartoNotebookWorker/src/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function render(
code;
file,
line,
cell_options,
)
end,
)
Expand Down Expand Up @@ -173,15 +174,25 @@ function _process_code(
return _parseall(code; filename, lineno)
end

function include_str(mod::Module, code::AbstractString; file::AbstractString, line::Integer)
function include_str(
mod::Module,
code::AbstractString;
file::AbstractString,
line::Integer,
cell_options::AbstractDict,
)
loc = LineNumberNode(line, Symbol(file))
try
ast = _process_code(mod, code; filename = file, lineno = line)
@assert Meta.isexpr(ast, :toplevel)
# Note: IO capturing combines stdout and stderr into a single
# `.output`, but Jupyter notebook spec appears to want them
# separate. Revisit this if it causes issues.
return Packages.IOCapture.capture(; rethrow = InterruptException, color = true) do
return Packages.IOCapture.capture(;
rethrow = InterruptException,
color = true,
io_context = _io_context(cell_options),
) do
result = nothing
line_and_ex = Expr(:toplevel, loc, nothing)
try
Expand Down Expand Up @@ -223,10 +234,12 @@ end

# passing our module removes Main.Notebook noise when printing types etc.
function with_context(io::IO, cell_options = Dict{String,Any}())
return IOContext(
io,
return IOContext(io, _io_context(cell_options)...)
end

function _io_context(cell_options = Dict{String,Any}())
return [
:module => NotebookState.notebook_module(),
:color => true,
:limit => true,
# This allows a `show` method implementation to check for
# metadata that may be of relevance to it's rendering. For
Expand All @@ -238,7 +251,7 @@ function with_context(io::IO, cell_options = Dict{String,Any}())
# TODO: perhaps preprocess the metadata provided here rather
# than just passing it through as-is.
:QuartoNotebookRunner => (; cell_options, options = NotebookState.OPTIONS[]),
)
]
end

function clean_bt_str(is_error::Bool, bt, err, prefix = "", mimetype = false)
Expand Down
11 changes: 11 additions & 0 deletions test/examples/io_context.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: io_context
---

```{julia}
struct M
a::Int
end
x = M(22)
print(x)
```
12 changes: 12 additions & 0 deletions test/testsets/io_context.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include("../utilities/prelude.jl")

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

cell = cells[2]
output = cell["outputs"][1]
@test output["output_type"] == "stream"
@test output["name"] == "stdout"
@test output["text"] == "M(22)"
end

0 comments on commit 7eb713b

Please sign in to comment.