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/
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())'

View File

@ -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)));
</script>
"""
@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})

View File

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

View File

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

View File

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