Skip to content

Commit

Permalink
Better support for Bokeh (#60)
Browse files Browse the repository at this point in the history
* Better support plotting in Juno

* Overload write for BokehPlot

* Add docstring for BokehPlot

* Improve test coverage of bokeh

* Add missing begin

* Fix string check
  • Loading branch information
sethaxen authored Feb 26, 2020
1 parent 4a5434e commit 94cd5c0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ArviZ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using Conda; Conda.add_channel("conda-forge") # try to avoid mixing channels
using PyPlot
using Pandas

import Base: convert, propertynames, getproperty, hash, show, +
import Base: convert, propertynames, getproperty, hash, show, write, +
import Base.Docs: getdoc
import StatsBase
import StatsBase: summarystats
Expand Down
27 changes: 25 additions & 2 deletions src/bokeh.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
"""
BokehPlot(::PyObject)
Loose wrapper around a Bokeh figure, mostly used for dispatch.
In most cases, use one of the plotting functions with `backend=:bokeh` to create a
`BokehPlot` instead of using a constructor.
"""
struct BokehPlot
o
o::PyObject
end

@inline PyObject(plot::BokehPlot) = getfield(plot, :o)
Expand All @@ -25,10 +33,25 @@ Base.display(::REPL.REPLDisplay, plot::BokehPlot) = bokeh.plotting.show(plot)

Base.show(io::IO, ::MIME"text/html", plot::BokehPlot) = print(io, render_html(plot))

function Base.show(io::IO, ::MIME"application/prs.juno.plotpane+html", plot::BokehPlot)
function Base.show(
io::IO,
::Union{MIME"application/juno+plotpane",MIME"application/prs.juno.plotpane+html"},
plot::BokehPlot,
)
return print(io, render_html(plot))
end

function Base.show(io::IO, ::MIME"juliavscode/html", plot::BokehPlot)
return print(io, render_html(plot))
end

# We don't need to implement this `save` since FileIO defaults to calling the above
# `show` method.

"""
write(io::IO, plot::BokehPlot)
write(filename::AbstractString, plot::BokehPlot)
Write the HTML representation of the Bokeh plot to the I/O stream or file.
"""
Base.write(io::IO, plot::BokehPlot) = print(io, render_html(plot))
17 changes: 14 additions & 3 deletions test/test_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,21 @@ if !ispynull(ArviZ.bokeh) && "plot.backend" in keys(ArviZ.rc_params())
@test pyobj === PyObject(plot2)

@test propertynames(plot) == propertynames(PyObject(plot))
getproperty(plot, :__class__)
@test occursin("bokeh", "$(getproperty(plot, :__class__))")

@testset "show MIME::\"$(mime)\"" for mime in [
"text/html",
"juliavscode/html",
"application/juno+plotpane",
"application/prs.juno.plotpane+html",
]
text = sprint(show, MIME(mime), plot)
@test text isa String
@test occursin("<body", text)
end

@testset "MIME::\"$(mime)\"" for mime in ["text/html"]
text = repr(MIME(mime), plot)
@testset "write html" begin
text = sprint(write, plot)
@test text isa String
@test occursin("<body", text)
end
Expand Down

0 comments on commit 94cd5c0

Please sign in to comment.