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

Error converting examples/extra/heisenberg_animation.jl into a Jupyter notebook #347

Closed
quantumsteve opened this issue Dec 19, 2024 · 3 comments

Comments

@quantumsteve
Copy link

quantumsteve commented Dec 19, 2024

I tried converting the example file examples/extra/heisenberg_animation.jl to a Jupyter notebook using the following script.

using Literate
using Sunny

function pp_vcheck(str)
  check = "@assert pkgversion(Sunny) >= " * repr(pkgversion(Sunny))
  return replace(str, r"^(using.*Sunny.*)$"m => SubstitutionString("\\1\n"*check))
end

function pp_wglmakie(str)
  return replace(str, r"^using(.*?)GLMakie"m => s"using\1WGLMakie")
  return str
end

Literate.notebook("examples/extra/heisenberg_animation.jl", "examples/extra", preprocess=pp_wglmakie∘pp_vcheck, execute=true, credit=false)

and get the following error from WGLMakie

[ Info: generating notebook from `~/Documents/gravitas/Sunny.jl/examples/extra/heisenberg_animation.jl`
[ Info: executing notebook `heisenberg_animation.ipynb`
┌ Error: error when executing notebook based on input file: `~/Documents/gravitas/Sunny.jl/examples/extra/heisenberg_animation.jl`
└ @ Literate ~/.julia/packages/Literate/TujCy/src/Literate.jl:811
ERROR: LoadError: LoadError: Can't get three: Screen Session uninitialized. Not yet displayed? Session status: CLOSED, id: 68c4b806-73b1-4779-8ce9-07d4f362d976
Waiting for plot to be initialized in display
in expression starting at /home/svh/Documents/gravitas/Sunny.jl/examples/extra/heisenberg_animation.ipynb:2
when executing the following code block from inputfile `~/Documents/gravitas/Sunny.jl/examples/extra/heisenberg_animation.jl`

julia
randomize_spins!(sys)
record(fig, "animation.mp4", 1:500; framerate=30) do _
    for _ in 1:5
        step!(sys, integrator)
    end
    notify(fig)
end


Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] execute_block(sb::Module, block::String; inputfile::String, fake_source::String, softscope::Bool, continue_on_error::Bool)
   @ Literate ~/.julia/packages/Literate/TujCy/src/Literate.jl:948
 [3] execute_block
   @ ~/.julia/packages/Literate/TujCy/src/Literate.jl:912 [inlined]
 [4] execute_notebook(nb::Dict{Any, Any}; inputfile::String, fake_source::String, softscope::Bool, continue_on_error::Bool)
   @ Literate ~/.julia/packages/Literate/TujCy/src/Literate.jl:828
 [5] (::Literate.var"#40#42"{Dict{String, Any}})()
   @ Literate ~/.julia/packages/Literate/TujCy/src/Literate.jl:804
 [6] cd(f::Literate.var"#40#42"{Dict{String, Any}}, dir::String)
   @ Base.Filesystem ./file.jl:112
 [7] jupyter_notebook(chunks::Vector{Literate.Chunk}, config::Dict{String, Any})
   @ Literate ~/.julia/packages/Literate/TujCy/src/Literate.jl:803
 [8] notebook(inputfile::String, outputdir::String; config::Dict{Any, Any}, kwargs::@Kwargs{preprocess::ComposedFunction{typeof(pp_wglmakie), typeof(pp_vcheck)}, execute::Bool, credit::Bool})
   @ Literate ~/.julia/packages/Literate/TujCy/src/Literate.jl:740
 [9] top-level scope
   @ ~/Documents/gravitas/Sunny.jl/meow.jl:14
in expression starting at /home/svh/Documents/gravitas/Sunny.jl/meow.jl:14

If I remove pp_wglmakie the script finishes without errors. Also, I can run examples/extra/heisenberg_animation.jl with WGLMakie with the animation being displayed in a Chrome tab.

@kbarros
Copy link
Member

kbarros commented Dec 19, 2024

To summarize, I think the concrete issue is that the Julia code

using WGLMakie
[...]
record(fig, "animation.mp4", 1:500; framerate=30) do _
    for _ in 1:5
        step!(sys, integrator)
    end
    notify(fig)
end

doesn't correctly execute in the Literate notebook generator? In the current CI, we are generating notebooks with execute=false to avoid issues like this. Steve, what is the context that motivates enabling execute=true? (For example, we might not necessarily need to generate animation.mp4 on the Github CI...)

@quantumsteve
Copy link
Author

I walked through the examples directory and generated notebooks for every .jl file.

using Literate
using Sunny

function pp_vcheck(str)
  check = "@assert pkgversion(Sunny) >= " * repr(pkgversion(Sunny))
  return replace(str, r"^(using.*Sunny.*)$"m => SubstitutionString("\\1\n"*check))
end

function pp_wglmakie(str)
  return replace(str, r"^using(.*?)GLMakie"m => s"using\1WGLMakie")
  return str
end

for (root, dirs, files) in walkdir("examples")
  println("Files in $root")
  for file in files
    if(splitext(file)[2] == ".jl")
      println(joinpath(root, file)) # path to files
      Literate.notebook(joinpath(root, file), root, preprocess=pp_wglmakiepp_vcheck, execute=true, credit=false)
    end
  end
end

The simplest fix will be excluding the examples/extra directory in the above script.

Not sure if this is of interest to Makie or Literate. Some way of skipping execution of a single cell would be sufficient.

@kbarros
Copy link
Member

kbarros commented Dec 20, 2024

OK, this seems like an issue with Makie or Literate, so I'm closing it here.

@kbarros kbarros closed this as completed Dec 20, 2024
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

No branches or pull requests

2 participants