Browse Source

Factor out code for Jupyter notebook output.

pull/122/head
Fredrik Ekre 5 years ago
parent
commit
3980541e87
  1. 18
      src/Literate.jl

18
src/Literate.jl

@ -529,6 +529,14 @@ function notebook(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:nb) preprocessor(inputfile, outputdir; user_config=config, user_kwargs=kwargs, type=:nb)
# create the notebook # create the notebook
nb = jupyter_notebook(chunks, config)
# write to file
outputfile = write_result(nb, config; print = (io, c)->JSON.print(io, c, 1))
return outputfile
end
function jupyter_notebook(chunks, config)
nb = Dict() nb = Dict()
nb["nbformat"] = JUPYTER_VERSION.major nb["nbformat"] = JUPYTER_VERSION.major
nb["nbformat_minor"] = JUPYTER_VERSION.minor nb["nbformat_minor"] = JUPYTER_VERSION.minor
@ -584,18 +592,16 @@ function notebook(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
if config["execute"]::Bool if config["execute"]::Bool
@info "executing notebook `$(config["name"] * ".ipynb")`" @info "executing notebook `$(config["name"] * ".ipynb")`"
try try
cd(outputdir) do cd(config["literate_outputdir"]) do
nb = execute_notebook(nb) nb = execute_notebook(nb)
end end
catch err catch err
@error "error when executing notebook based on input file: `$(Base.contractuser(inputfile))`" @error "error when executing notebook based on input file: " *
"`$(Base.contractuser(config["literate_inputfile"]))`"
rethrow(err) rethrow(err)
end end
end end
return nb
# write to file
outputfile = write_result(nb, config; print = (io, c)->JSON.print(io, c, 1))
return outputfile
end end
function execute_notebook(nb) function execute_notebook(nb)

Loading…
Cancel
Save