Merge branch 'detect_plotlib'

pull/44/head
= 2016-05-02 23:32:18 +03:00
commit 95cfdaf04d
4 changed files with 66 additions and 8 deletions

View File

@ -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.
"""
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,
cache_path = "cache", cache=:off)
@ -101,6 +101,18 @@ function weave(doc::AbstractString, doctype::AbstractString)
weave(doc, doctype=doctype)
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,
set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults

View File

@ -2,7 +2,8 @@
#Default options
const defaultParams =
@compat Dict{Symbol,Any}(:plotlib => "Gadfly",
@compat Dict{Symbol,Any}(:plotlib => nothing,
:plotlib_set => false,
:storeresults => false,
:doc_number => 0,
:chunk_defaults =>

11
src/plots.jl Normal file
View File

@ -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)

View File

@ -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.
"""
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,
cache_path = "cache", cache = :off)
#cache :all, :user, :off, :refresh
@ -43,8 +43,10 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",
mimetypes = default_mime_types
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)
pushdisplay(report)
@ -64,6 +66,8 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",
if typeof(chunk) == CodeChunk
options = merge(rcParams[:chunk_defaults], chunk.options)
merge!(chunk.options, options)
end
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)
result_chunks = restore_chunk(chunk, cached)
else
result_chunks = run_chunk(chunk, report, SandBox)
result_chunks = run_chunk(chunk, report, SandBox)
end
executed = [executed; result_chunks]
end
@ -127,6 +133,7 @@ function run_code(chunk::CodeChunk, report::Report, SandBox::Module)
for (str_expr, expr) = expressions
reset_report(report)
lastline = (result_no == N)
rcParams[:plotlib_set] || detect_plotlib(chunk) #Try to autodetect plotting library
(obj, out) = capture_output(expr, SandBox, chunk.options[:term],
chunk.options[:display], rcParams[:plotlib], lastline)
figures = report.figures #Captured figures
@ -199,6 +206,11 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
return chunk
end
#Run preexecute_hooks
for hook in preexecute_hooks
chunk = hook(chunk)
end
report.fignum = 1
report.cur_chunk = chunk
@ -207,6 +219,12 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
end
chunk.result = run_code(chunk, report, SandBox)
#Run post_execute chunks
for hook in postexecute_hooks
chunk = hook(chunk)
end
if chunk.options[:term]
chunks = collect_results(chunk, TermResult())
elseif chunk.options[:hold]
@ -215,6 +233,8 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
chunks = collect_results(chunk, ScriptResult())
end
#else
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
#end
@ -252,25 +272,27 @@ end
function init_plotting(plotlib)
rcParams[:plotlib_set] = true
if plotlib == nothing
#rcParams[:chunk_defaults][:fig] = false
rcParams[:plotlib] = nothing
else
l_plotlib = lowercase(plotlib)
rcParams[:chunk_defaults][:fig] = true
if l_plotlib == "winston"
eval(parse("""include(Pkg.dir("Weave","src","winston.jl"))"""))
rcParams[:plotlib] = "Winston"
elseif l_plotlib == "pyplot"
eval(parse("""include(Pkg.dir("Weave","src","pyplot.jl"))"""))
rcParams[:plotlib] = "PyPlot"
elseif l_plotlib == "plots"
eval(parse("""include(Pkg.dir("Weave","src","plots.jl"))"""))
rcParams[:plotlib] = "Plots"
elseif l_plotlib == "gadfly"
eval(parse("""include(Pkg.dir("Weave","src","gadfly.jl"))"""))
rcParams[:plotlib] = "Gadfly"
end
end
return nothing
return true
end
function get_cwd(doc::WeaveDoc, out_path)
@ -387,3 +409,15 @@ function collect_results(chunk::CodeChunk, fmt::CollectResult)
end
return [chunk]
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