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

Call showable in correct world age #187

Merged
merged 1 commit into from Feb 10, 2022

Conversation

devmotion
Copy link
Contributor

It took me multiple days to figure out why executing some Markdown blocks that return CairoMakie figures errored even though everything worked when copy-pasted to the REPL. A much much simpler example that demonstrates the problem (requires installation of Literate and CairoMakie):

julia> ] add Literate CairoMakie

julia> using Literate

julia> write("literate_script.jl",
       """
       using CairoMakie
       CairoMakie.activate!(; type="svg")
       fig = lines(rand(3))
       showable(MIME("text/html"), fig) && error("HTML output is not supported")
       fig
       """);

julia> withenv("JULIA_DEBUG" => "Literate") do
           Literate.markdown("literate_script.jl"; execute=true)
       end
[ Info: generating markdown page from ~/literate_script.jl
┌ Debug: execute_block(Main.##291, block)
│
│   using CairoMakie
│   CairoMakie.activate!(; type="svg")
│   fig = lines(rand(3))
│   showable(MIME("text/html"), fig) && error("HTML output is not supported")
│   fig
└ @ Literate /home/david/.julia/packages/Literate/A6l2j/src/Literate.jl:802
ERROR: MethodError: no method matching backend_show(::CairoMakie.CairoBackend, ::IOContext{IOBuffer}, ::MIME{Symbol("text/html")}, ::Makie.Scene)
Closest candidates are:
  backend_show(::Any, ::IO, ::MIME{Symbol("text/plain")}, ::Makie.Scene) at ~/.julia/packages/Makie/14tKQ/src/display.jl:85
  backend_show(::CairoMakie.CairoBackend, ::IO, ::MIME{Symbol("image/png")}, ::Makie.Scene) at ~/.julia/packages/CairoMakie/ozkuo/src/infrastructure.jl:328
  backend_show(::CairoMakie.CairoBackend, ::IO, ::MIME{Symbol("application/postscript")}, ::Makie.Scene) at ~/.julia/packages/CairoMakie/ozkuo/src/infrastructure.jl:317
  ...
Stacktrace:
  [1] show(io::IOBuffer, m::MIME{Symbol("text/html")}, scene::Makie.Scene)
    @ Makie ~/.julia/packages/Makie/14tKQ/src/display.jl:108
  [2] show(io::IOBuffer, m::MIME{Symbol("text/html")}, fig::Makie.Figure; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Makie ~/.julia/packages/Makie/14tKQ/src/display.jl:102
  [3] show(io::IOBuffer, m::MIME{Symbol("text/html")}, fig::Makie.Figure)
    @ Makie ~/.julia/packages/Makie/14tKQ/src/display.jl:102
  [4] show(io::IOBuffer, m::MIME{Symbol("text/html")}, fap::Makie.FigureAxisPlot; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Makie ~/.julia/packages/Makie/14tKQ/src/display.jl:101
  [5] show(io::IOBuffer, m::MIME{Symbol("text/html")}, fap::Makie.FigureAxisPlot)
    @ Makie ~/.julia/packages/Makie/14tKQ/src/display.jl:101
  [6] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
  [7] invokelatest
    @ ./essentials.jl:714 [inlined]
  [8] execute_markdown!(io::IOBuffer, sb::Module, block::String, outputdir::String; inputfile::String, flavor::Literate.DocumenterFlavor, image_formats::Vector{Tuple{MIME, String}})
    @ Literate ~/.julia/packages/Literate/A6l2j/src/Literate.jl:575
  [9] markdown(inputfile::String, outputdir::String; config::Dict{Any, Any}, kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:execute,), Tuple{Bool}}})
    @ Literate ~/.julia/packages/Literate/A6l2j/src/Literate.jl:545
 [10] #3
    @ ./REPL[6]:2 [inlined]
 [11] withenv(f::var"#3#4", keyvals::Pair{String, String})
    @ Base ./env.jl:172
 [12] top-level scope
    @ REPL[6]:1

julia> fig = include("literate_script.jl");

julia> typeof(fig)
Makie.FigureAxisPlot

julia> showable(MIME("text/html"), fig)
false

As I realized after a long time, the problem is that the showable checks in src/Literate.jl run in a different world age than the actual show invocations. Calling showable also with invokelatest fixes the issue.

@fredrikekre fredrikekre merged commit efe9d0a into fredrikekre:master Feb 10, 2022
@fredrikekre
Copy link
Owner

Thanks

@devmotion devmotion deleted the dw/worldage branch February 10, 2022 19:00
@devmotion devmotion restored the dw/worldage branch February 10, 2022 19:01
@fredrikekre
Copy link
Owner

@devmotion
Copy link
Contributor Author

Thanks! I was about to push the following regression test when I noticed that you had already merged and released the PR 🙂 Maybe it could still be useful?

diff --git a/test/runtests.jl b/test/runtests.jl
index 9d34427..1b10962 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -790,6 +790,13 @@ end end
                 Base.show(io::IO, mime::MIME"image/svg+xml", ::SVG) = print(io, "SVG")
                 SVG()
                 #-
+                struct SVGOnly end
+                Base.showable(::MIME, ::SVGOnly) = false
+                Base.showable(::MIME"image/svg+xml", ::SVGOnly) = true
+                Base.show(::IO, ::MIME, ::SVGOnly) = error("only SVG output supported")
+                Base.show(io::IO, ::MIME"image/svg+xml", ::SVGOnly) = print(io, "SVGOnly")
+                SVGOnly()
+                #-
                 struct MD end
                 Base.show(io::IO, mime::MIME"text/markdown", ::MD) = print(io, "# " * "MD")
                 Base.show(io::IO, mime::MIME"text/html", ::MD) =

@fredrikekre
Copy link
Owner

Yea, that would be great.

@devmotion devmotion deleted the dw/worldage branch February 10, 2022 19:21
devmotion added a commit to devmotion/Literate.jl that referenced this pull request Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants