From c4683a754e30408fe19edd64d88727650333ce35 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Wed, 20 Apr 2016 17:34:55 -0400 Subject: [PATCH] plotly png generation using wkhtmltoimage --- .travis.yml | 50 +++++++++++++++++++++++++++------------- src/backends/plotly.jl | 6 +++-- src/backends/plotlyjs.jl | 12 +++++++--- src/backends/web.jl | 28 +++++++++++++++++++--- test/runtests.jl | 8 +++---- 5 files changed, 76 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3c5cc476..ac556004 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,21 +1,37 @@ # Documentation: http://docs.travis-ci.com/user/languages/julia/ language: julia -# os: -# - linux -# - osx -# julia: -# - 0.4 -# #- nightly +os: + - linux + - osx +julia: + - 0.4 + #- nightly -# 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" + +addons: + apt: + packages: + - wkhtmltopdf + +before install: + - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi + - 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: email: true @@ -33,5 +49,7 @@ script: - julia -e 'Pkg.clone("https://github.com/spencerlyon2/PlotlyJS.jl.git")' # - julia -e 'Pkg.add("Cairo"); Pkg.build("Cairo")' - 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())' diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 30d89f4d..ef6cda8c 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -215,6 +215,8 @@ end function plotly_layout(d::KW) d_out = KW() + d_out[:width], d_out[:height] = d[:size] + bgcolor = webcolor(d[:background_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))); """ - @show html + # @show html html end @@ -498,7 +500,7 @@ end # ---------------------------------------------------------------- function Base.writemime(io::IO, ::MIME"image/png", plt::AbstractPlot{PlotlyBackend}) - warn("todo: png") + writemime_png_from_html(io, plt) end function Base.writemime(io::IO, ::MIME"text/html", plt::AbstractPlot{PlotlyBackend}) diff --git a/src/backends/plotlyjs.jl b/src/backends/plotlyjs.jl index 9bb16d44..82287dc6 100644 --- a/src/backends/plotlyjs.jl +++ b/src/backends/plotlyjs.jl @@ -8,6 +8,7 @@ function _initialize_backend(::PlotlyJSBackend; kw...) end 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) end @@ -113,9 +114,14 @@ end # ---------------------------------------------------------------- -function Base.writemime(io::IO, m::MIME"text/html", plt::AbstractPlot{PlotlyJSBackend}) - Base.writemime(io, m, plt.o) -end +# function Base.writemime(io::IO, m::MIME"text/html", plt::AbstractPlot{PlotlyJSBackend}) +# Base.writemime(io, m, plt.o) +# 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}) display(plt.o) diff --git a/src/backends/web.jl b/src/backends/web.jl index 1be80c11..9500c76e 100644 --- a/src/backends/web.jl +++ b/src/backends/web.jl @@ -26,13 +26,35 @@ function open_browser_window(filename::AbstractString) warn("Unknown OS... cannot open browser window.") end -function standalone_html_window(plt::AbstractPlot; kw...) - html = standalone_html(plt; kw...) - # println(html) +function write_temp_html(plt::AbstractPlot) + html = standalone_html(plt; title = plt.plotargs[:title]) filename = string(tempname(), ".html") output = open(filename, "w") write(output, html) close(output) + filename +end + +function standalone_html_window(plt::AbstractPlot) + filename = write_temp_html(plt) open_browser_window(filename) 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 diff --git a/test/runtests.jl b/test/runtests.jl index a0fe86d7..137a8dd6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -33,11 +33,11 @@ facts("GR") do # image_comparison_facts(:gr, only=[1], eps=img_eps) end -facts("PlotlyJS") do - @fact plotlyjs() --> Plots.PlotlyJSBackend() - @fact backend() --> Plots.PlotlyJSBackend() +facts("Plotly") do + @fact plotly() --> Plots.PlotlyBackend() + @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 FactCheck.exitstatus()