Strip SVG outputs from a Literate-generated Jupyter notebook
By default, ipynb notebooks generated by Literate.jl contains all available formats in output cells. To save space, one can remove the output formats (e.g., SVG).
- Use
JSON.jlto read theipynbfile and remove the output data. - Use
Base.format_bytes()to show human-readable file size.
using JSON
function strip_svg(nbpath)
oldfilesize = filesize(nbpath)
nb = open(JSON.parse, nbpath, "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(nbpath; force=true)
write(nbpath, JSON.json(nb, 1))
@info "Stripped SVG in $(nbpath). The original size is $(Base.format_bytes(oldfilesize)). The new size is $(Base.format_bytes(filesize(nbpath)))."
return nbpath
endLast updated on