plotly png generation using wkhtmltoimage

This commit is contained in:
Thomas Breloff 2016-04-20 17:34:55 -04:00
parent 1da8fdb588
commit c4683a754e
5 changed files with 76 additions and 28 deletions

View File

@ -1,21 +1,37 @@
# Documentation: http://docs.travis-ci.com/user/languages/julia/ # Documentation: http://docs.travis-ci.com/user/languages/julia/
language: julia language: julia
# os: os:
# - linux - linux
# - osx - osx
# julia: julia:
# - 0.4 - 0.4
# #- nightly #- nightly
# borrowed from Blink.jl's travis file
matrix: addons:
include: apt:
- os: linux packages:
julia: 0.4 - wkhtmltopdf
env: TESTCMD="xvfb-run julia"
- os: osx before install:
julia: 0.4 - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi
env: TESTCMD="julia" - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install wkhtmltopdf; fi
# sudo: required
# before_install:
# - sudo apt-get -qq update
# - sudo apt-get install wkhtmltopdf-dbg -y
# # borrowed from Blink.jl's travis file
# matrix:
# include:
# - os: linux
# julia: 0.4
# env: TESTCMD="xvfb-run julia"
# - os: osx
# julia: 0.4
# env: TESTCMD="julia"
notifications: notifications:
email: true email: true
@ -33,5 +49,7 @@ script:
- julia -e 'Pkg.clone("https://github.com/spencerlyon2/PlotlyJS.jl.git")' - julia -e 'Pkg.clone("https://github.com/spencerlyon2/PlotlyJS.jl.git")'
# - julia -e 'Pkg.add("Cairo"); Pkg.build("Cairo")' # - julia -e 'Pkg.add("Cairo"); Pkg.build("Cairo")'
- julia -e 'ENV["PYTHON"] = ""; Pkg.add("PyPlot"); Pkg.build("PyPlot")' - julia -e 'ENV["PYTHON"] = ""; Pkg.add("PyPlot"); Pkg.build("PyPlot")'
- $TESTCMD -e 'Pkg.test("Plots"; coverage=false)'
# - $TESTCMD -e 'Pkg.test("Plots"; coverage=false)'
- julia -e 'Pkg.test("Plots"; coverage=false)'
# - julia -e 'cd(Pkg.dir("Plots")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder()); Codecov.submit(process_folder())' # - julia -e 'cd(Pkg.dir("Plots")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(process_folder()); Codecov.submit(process_folder())'

View File

@ -215,6 +215,8 @@ end
function plotly_layout(d::KW) function plotly_layout(d::KW)
d_out = KW() d_out = KW()
d_out[:width], d_out[:height] = d[:size]
bgcolor = webcolor(d[:background_color]) bgcolor = webcolor(d[:background_color])
fgcolor = webcolor(d[:foreground_color]) fgcolor = webcolor(d[:foreground_color])
@ -458,7 +460,7 @@ function html_body(plt::Plot{PlotlyBackend}, style = nothing)
Plotly.plot(PLOT, $(get_series_json(plt)), $(get_plot_json(plt))); Plotly.plot(PLOT, $(get_series_json(plt)), $(get_plot_json(plt)));
</script> </script>
""" """
@show html # @show html
html html
end end
@ -498,7 +500,7 @@ end
# ---------------------------------------------------------------- # ----------------------------------------------------------------
function Base.writemime(io::IO, ::MIME"image/png", plt::AbstractPlot{PlotlyBackend}) function Base.writemime(io::IO, ::MIME"image/png", plt::AbstractPlot{PlotlyBackend})
warn("todo: png") writemime_png_from_html(io, plt)
end end
function Base.writemime(io::IO, ::MIME"text/html", plt::AbstractPlot{PlotlyBackend}) function Base.writemime(io::IO, ::MIME"text/html", plt::AbstractPlot{PlotlyBackend})

View File

@ -8,6 +8,7 @@ function _initialize_backend(::PlotlyJSBackend; kw...)
end end
for (mime, fmt) in PlotlyJS._mimeformats for (mime, fmt) in PlotlyJS._mimeformats
# mime == "image/png" && continue # don't use plotlyjs's writemime for png
@eval Base.writemime(io::IO, m::MIME{symbol($mime)}, p::Plot{PlotlyJSBackend}) = writemime(io, m, p.o) @eval Base.writemime(io::IO, m::MIME{symbol($mime)}, p::Plot{PlotlyJSBackend}) = writemime(io, m, p.o)
end end
@ -113,9 +114,14 @@ end
# ---------------------------------------------------------------- # ----------------------------------------------------------------
function Base.writemime(io::IO, m::MIME"text/html", plt::AbstractPlot{PlotlyJSBackend}) # function Base.writemime(io::IO, m::MIME"text/html", plt::AbstractPlot{PlotlyJSBackend})
Base.writemime(io, m, plt.o) # Base.writemime(io, m, plt.o)
end # end
# function Base.writemime(io::IO, ::MIME"image/png", plt::AbstractPlot{PlotlyJSBackend})
# println("here!")
# writemime_png_from_html(io, plt)
# end
function Base.display(::PlotsDisplay, plt::Plot{PlotlyJSBackend}) function Base.display(::PlotsDisplay, plt::Plot{PlotlyJSBackend})
display(plt.o) display(plt.o)

View File

@ -26,13 +26,35 @@ function open_browser_window(filename::AbstractString)
warn("Unknown OS... cannot open browser window.") warn("Unknown OS... cannot open browser window.")
end end
function standalone_html_window(plt::AbstractPlot; kw...) function write_temp_html(plt::AbstractPlot)
html = standalone_html(plt; kw...) html = standalone_html(plt; title = plt.plotargs[:title])
# println(html)
filename = string(tempname(), ".html") filename = string(tempname(), ".html")
output = open(filename, "w") output = open(filename, "w")
write(output, html) write(output, html)
close(output) close(output)
filename
end
function standalone_html_window(plt::AbstractPlot)
filename = write_temp_html(plt)
open_browser_window(filename) open_browser_window(filename)
end end
# uses wkhtmltopdf/wkhtmltoimage: http://wkhtmltopdf.org/downloads.html
function html_to_png(html_fn, png_fn, w, h)
run(`wkhtmltoimage --width $w --height $h --disable-smart-width $html_fn $png_fn`)
end
function writemime_png_from_html(io::IO, plt::AbstractPlot)
# write html to a temporary file
html_fn = write_temp_html(plt)
# convert that html file to a temporary png file using wkhtmltoimage
png_fn = tempname() * ".png"
w, h = plt.plotargs[:size]
html_to_png(html_fn, png_fn, w, h)
# now read that file data into io
pngdata = readall(png_fn)
write(io, pngdata)
end

View File

@ -33,11 +33,11 @@ facts("GR") do
# image_comparison_facts(:gr, only=[1], eps=img_eps) # image_comparison_facts(:gr, only=[1], eps=img_eps)
end end
facts("PlotlyJS") do facts("Plotly") do
@fact plotlyjs() --> Plots.PlotlyJSBackend() @fact plotly() --> Plots.PlotlyBackend()
@fact backend() --> Plots.PlotlyJSBackend() @fact backend() --> Plots.PlotlyBackend()
image_comparison_facts(:plotlyjs, only=[1,2,3,4,7,8,9,10,11,12,14,15,20,22,23,24,27], eps=img_eps) image_comparison_facts(:plotly, only=[1,3,4,7,8,9,10,11,12,14,15,20,22,23,27], eps=img_eps)
end end
FactCheck.exitstatus() FactCheck.exitstatus()