mirror of https://github.com/mpastell/Weave.jl
Merge branch 'detect_plotlib'
commit
95cfdaf04d
14
src/Weave.jl
14
src/Weave.jl
|
@ -64,7 +64,7 @@ Weave an input document to output file.
|
||||||
|
|
||||||
**Note:** Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
|
**Note:** Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
|
||||||
"""
|
"""
|
||||||
function weave(source ; doctype = :auto, plotlib="Gadfly",
|
function weave(source ; doctype = :auto, plotlib=:auto,
|
||||||
informat=:auto, out_path=:doc, fig_path = "figures", fig_ext = nothing,
|
informat=:auto, out_path=:doc, fig_path = "figures", fig_ext = nothing,
|
||||||
cache_path = "cache", cache=:off)
|
cache_path = "cache", cache=:off)
|
||||||
|
|
||||||
|
@ -101,6 +101,18 @@ function weave(doc::AbstractString, doctype::AbstractString)
|
||||||
weave(doc, doctype=doctype)
|
weave(doc, doctype=doctype)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#Hooks to run before and after chunks, this is form IJulia,
|
||||||
|
#but note that Weave hooks take the chunk as input
|
||||||
|
const preexecute_hooks = Function[]
|
||||||
|
push_preexecute_hook(f::Function) = push!(preexecute_hooks, f)
|
||||||
|
pop_preexecute_hook(f::Function) = splice!(preexecute_hooks, findfirst(pretexecute_hooks, f))
|
||||||
|
|
||||||
|
const postexecute_hooks = Function[]
|
||||||
|
push_postexecute_hook(f::Function) = push!(postexecute_hooks, f)
|
||||||
|
pop_postexecute_hook(f::Function) = splice!(postexecute_hooks, findfirst(postexecute_hooks, f))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export weave, list_out_formats, tangle,
|
export weave, list_out_formats, tangle,
|
||||||
set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults
|
set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
#Default options
|
#Default options
|
||||||
const defaultParams =
|
const defaultParams =
|
||||||
@compat Dict{Symbol,Any}(:plotlib => "Gadfly",
|
@compat Dict{Symbol,Any}(:plotlib => nothing,
|
||||||
|
:plotlib_set => false,
|
||||||
:storeresults => false,
|
:storeresults => false,
|
||||||
:doc_number => 0,
|
:doc_number => 0,
|
||||||
:chunk_defaults =>
|
:chunk_defaults =>
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
import Plots
|
||||||
|
|
||||||
|
"""Pre-execute hooks to set the plot size for the chunk """
|
||||||
|
function plots_set_size(chunk)
|
||||||
|
w = chunk.options[:fig_width] * chunk.options[:dpi]
|
||||||
|
h = chunk.options[:fig_height] * chunk.options[:dpi]
|
||||||
|
Plots.default(size = (w,h))
|
||||||
|
return chunk
|
||||||
|
end
|
||||||
|
|
||||||
|
push_preexecute_hook(plots_set_size)
|
46
src/run.jl
46
src/run.jl
|
@ -18,7 +18,7 @@ Run code chunks and capture output from parsed document.
|
||||||
|
|
||||||
**Note:** Run command from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
|
**Note:** Run command from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
|
||||||
"""
|
"""
|
||||||
function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",
|
function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
|
||||||
out_path=:doc, fig_path = "figures", fig_ext = nothing,
|
out_path=:doc, fig_path = "figures", fig_ext = nothing,
|
||||||
cache_path = "cache", cache = :off)
|
cache_path = "cache", cache = :off)
|
||||||
#cache :all, :user, :off, :refresh
|
#cache :all, :user, :off, :refresh
|
||||||
|
@ -43,8 +43,10 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",
|
||||||
mimetypes = default_mime_types
|
mimetypes = default_mime_types
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#Reset plotting
|
||||||
|
rcParams[:plotlib_set] = false
|
||||||
|
plotlib == :auto || init_plotting(plotlib)
|
||||||
|
|
||||||
init_plotting(plotlib)
|
|
||||||
report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes)
|
report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes)
|
||||||
pushdisplay(report)
|
pushdisplay(report)
|
||||||
|
|
||||||
|
@ -64,6 +66,8 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",
|
||||||
if typeof(chunk) == CodeChunk
|
if typeof(chunk) == CodeChunk
|
||||||
options = merge(rcParams[:chunk_defaults], chunk.options)
|
options = merge(rcParams[:chunk_defaults], chunk.options)
|
||||||
merge!(chunk.options, options)
|
merge!(chunk.options, options)
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache])
|
restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache])
|
||||||
|
@ -71,8 +75,10 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",
|
||||||
if cached != nothing && (cache == :all || restore)
|
if cached != nothing && (cache == :all || restore)
|
||||||
result_chunks = restore_chunk(chunk, cached)
|
result_chunks = restore_chunk(chunk, cached)
|
||||||
else
|
else
|
||||||
result_chunks = run_chunk(chunk, report, SandBox)
|
|
||||||
|
result_chunks = run_chunk(chunk, report, SandBox)
|
||||||
end
|
end
|
||||||
|
|
||||||
executed = [executed; result_chunks]
|
executed = [executed; result_chunks]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -127,6 +133,7 @@ function run_code(chunk::CodeChunk, report::Report, SandBox::Module)
|
||||||
for (str_expr, expr) = expressions
|
for (str_expr, expr) = expressions
|
||||||
reset_report(report)
|
reset_report(report)
|
||||||
lastline = (result_no == N)
|
lastline = (result_no == N)
|
||||||
|
rcParams[:plotlib_set] || detect_plotlib(chunk) #Try to autodetect plotting library
|
||||||
(obj, out) = capture_output(expr, SandBox, chunk.options[:term],
|
(obj, out) = capture_output(expr, SandBox, chunk.options[:term],
|
||||||
chunk.options[:display], rcParams[:plotlib], lastline)
|
chunk.options[:display], rcParams[:plotlib], lastline)
|
||||||
figures = report.figures #Captured figures
|
figures = report.figures #Captured figures
|
||||||
|
@ -199,6 +206,11 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
|
||||||
return chunk
|
return chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#Run preexecute_hooks
|
||||||
|
for hook in preexecute_hooks
|
||||||
|
chunk = hook(chunk)
|
||||||
|
end
|
||||||
|
|
||||||
report.fignum = 1
|
report.fignum = 1
|
||||||
report.cur_chunk = chunk
|
report.cur_chunk = chunk
|
||||||
|
|
||||||
|
@ -207,6 +219,12 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
|
||||||
end
|
end
|
||||||
|
|
||||||
chunk.result = run_code(chunk, report, SandBox)
|
chunk.result = run_code(chunk, report, SandBox)
|
||||||
|
|
||||||
|
#Run post_execute chunks
|
||||||
|
for hook in postexecute_hooks
|
||||||
|
chunk = hook(chunk)
|
||||||
|
end
|
||||||
|
|
||||||
if chunk.options[:term]
|
if chunk.options[:term]
|
||||||
chunks = collect_results(chunk, TermResult())
|
chunks = collect_results(chunk, TermResult())
|
||||||
elseif chunk.options[:hold]
|
elseif chunk.options[:hold]
|
||||||
|
@ -215,6 +233,8 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
|
||||||
chunks = collect_results(chunk, ScriptResult())
|
chunks = collect_results(chunk, ScriptResult())
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
|
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
|
||||||
#end
|
#end
|
||||||
|
@ -252,25 +272,27 @@ end
|
||||||
|
|
||||||
|
|
||||||
function init_plotting(plotlib)
|
function init_plotting(plotlib)
|
||||||
|
rcParams[:plotlib_set] = true
|
||||||
if plotlib == nothing
|
if plotlib == nothing
|
||||||
#rcParams[:chunk_defaults][:fig] = false
|
|
||||||
rcParams[:plotlib] = nothing
|
rcParams[:plotlib] = nothing
|
||||||
else
|
else
|
||||||
l_plotlib = lowercase(plotlib)
|
l_plotlib = lowercase(plotlib)
|
||||||
rcParams[:chunk_defaults][:fig] = true
|
rcParams[:chunk_defaults][:fig] = true
|
||||||
|
|
||||||
if l_plotlib == "winston"
|
if l_plotlib == "winston"
|
||||||
eval(parse("""include(Pkg.dir("Weave","src","winston.jl"))"""))
|
eval(parse("""include(Pkg.dir("Weave","src","winston.jl"))"""))
|
||||||
rcParams[:plotlib] = "Winston"
|
rcParams[:plotlib] = "Winston"
|
||||||
elseif l_plotlib == "pyplot"
|
elseif l_plotlib == "pyplot"
|
||||||
eval(parse("""include(Pkg.dir("Weave","src","pyplot.jl"))"""))
|
eval(parse("""include(Pkg.dir("Weave","src","pyplot.jl"))"""))
|
||||||
rcParams[:plotlib] = "PyPlot"
|
rcParams[:plotlib] = "PyPlot"
|
||||||
|
elseif l_plotlib == "plots"
|
||||||
|
eval(parse("""include(Pkg.dir("Weave","src","plots.jl"))"""))
|
||||||
|
rcParams[:plotlib] = "Plots"
|
||||||
elseif l_plotlib == "gadfly"
|
elseif l_plotlib == "gadfly"
|
||||||
eval(parse("""include(Pkg.dir("Weave","src","gadfly.jl"))"""))
|
eval(parse("""include(Pkg.dir("Weave","src","gadfly.jl"))"""))
|
||||||
rcParams[:plotlib] = "Gadfly"
|
rcParams[:plotlib] = "Gadfly"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return nothing
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_cwd(doc::WeaveDoc, out_path)
|
function get_cwd(doc::WeaveDoc, out_path)
|
||||||
|
@ -387,3 +409,15 @@ function collect_results(chunk::CodeChunk, fmt::CollectResult)
|
||||||
end
|
end
|
||||||
return [chunk]
|
return [chunk]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function detect_plotlib(chunk::CodeChunk)
|
||||||
|
if isdefined(:Plots)
|
||||||
|
init_plotting("Plots")
|
||||||
|
#Need to set size before plots are created
|
||||||
|
plots_set_size(chunk)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
isdefined(:PyPlot) && init_plotting("PyPlot") && return
|
||||||
|
isdefined(:Gadfly) && init_plotting("Gadfly") && return
|
||||||
|
isdefined(:Winston) && init_plotting("Winston") && return
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue