-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add back literate and strip svg for all nbs (#269)
* add back literate and strip svg for all nbs * fix * also list jl files * fix * fix * fix * fix * fix * TOC
- Loading branch information
1 parent
630c68f
commit 4633e56
Showing
8 changed files
with
145 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Literate | ||
using JSON | ||
using Pkg | ||
using IJulia | ||
|
||
ENV["GKSwstype"] = "100" | ||
|
||
function main(; rmsvg=true) | ||
file = get(ENV, "NB", "test.ipynb") | ||
cachedir = get(ENV, "NBCACHE", ".cache") | ||
nb = if endswith(file, ".jl") | ||
run_literate(file; cachedir) | ||
elseif endswith(file, ".ipynb") | ||
IJulia.installkernel("Julia", "--project=@.") | ||
run_ipynb(file; cachedir) | ||
else | ||
error("$(file) is not a valid notebook file!") | ||
end | ||
rmsvg && strip_svg(nb) | ||
return nothing | ||
end | ||
|
||
# Strip SVG output from a Jupyter notebook | ||
function strip_svg(ipynb) | ||
@info "Stripping SVG in $(ipynb)" | ||
nb = open(JSON.parse, ipynb, "r") | ||
for cell in nb["cells"] | ||
!haskey(cell, "outputs") && continue | ||
for output in cell["outputs"] | ||
!haskey(output, "data") && continue | ||
datadict = output["data"] | ||
if haskey(datadict, "image/png") || haskey(datadict, "image/jpeg") | ||
delete!(datadict, "text/html") | ||
delete!(datadict, "image/svg+xml") | ||
end | ||
end | ||
end | ||
rm(ipynb) | ||
open(ipynb, "w") do io | ||
JSON.print(io, nb, 1) | ||
end | ||
return ipynb | ||
end | ||
|
||
function run_literate(file; cachedir = ".cache") | ||
outpath = joinpath(abspath(pwd()), cachedir, dirname(file)) | ||
mkpath(outpath) | ||
ipynb = Literate.notebook(file, outpath; mdstrings=true, execute=true) | ||
return ipynb | ||
end | ||
|
||
function run_ipynb(file; cachedir = ".cache") | ||
outpath = joinpath(abspath(pwd()), cachedir, file) | ||
mkpath(dirname(outpath)) | ||
kernelname = "--ExecutePreprocessor.kernel_name=julia-1.$(VERSION.minor)" | ||
execute = get(ENV, "ALLOWERRORS", " ") == "true" ? "--execute --allow-errors" : "--execute" | ||
timeout = "--ExecutePreprocessor.timeout=" * get(ENV, "TIMEOUT", "-1") | ||
cmd = `jupyter nbconvert --to notebook $(execute) $(timeout) $(kernelname) --output $(outpath) $(file)` | ||
run(cmd) | ||
return outpath | ||
end | ||
|
||
main() |
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 |
---|---|---|
|
@@ -5,4 +5,6 @@ format: jb-book | |
root: index | ||
chapters: | ||
- file: pyplot | ||
- file: pythonplot | ||
- file: sub/plots | ||
- file: sub/plots-lit |
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,21 @@ | ||
#=== | ||
# Plotting with PythonPlot.jl | ||
Using Literate.jl | ||
===# | ||
import PythonPlot as plt | ||
using Random | ||
Random.seed!(2022) | ||
|
||
#--- | ||
plt.figure() | ||
plt.plot(1:5, rand(1:6, 5)) | ||
plt.gcf() | ||
|
||
# ## Runtime information | ||
import Pkg | ||
Pkg.status() | ||
|
||
#--- | ||
import InteractiveUtils | ||
InteractiveUtils.versioninfo() |
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,19 @@ | ||
#=== | ||
# Plotting by Plots.jl | ||
Using Literate.jl | ||
===# | ||
using Plots | ||
using Random | ||
Random.seed!(2022) | ||
|
||
#--- | ||
plot(rand(1:6, 5)) | ||
|
||
# ## Runtime information | ||
import Pkg | ||
Pkg.status() | ||
|
||
#--- | ||
import InteractiveUtils | ||
InteractiveUtils.versioninfo() |
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