arviz-devs / ArviZ.jl
1
const has_bokeh_png_deps = false
2

3
function initialize_bokeh()
4 20
    ispynull(bokeh) || return nothing
5 20
    try
6 20
        copy!(bokeh, _import_dependency("bokeh", "bokeh"; channel="conda-forge"))
7
    catch err
8 0
        copy!(bokeh, PyNULL())
9 0
        throw(err)
10
    end
11 20
    return nothing
12
end
13

14
# install dependencies for saving PNGs if using conda
15
function initialize_bokeh_png_deps()
16 20
    has_bokeh_png_deps && return nothing
17 20
    try
18 20
        _import_dependency("selenium", "selenium"; channel="conda-forge")
19 20
        has_bokeh_png_deps = true
20
    catch err
21 0
        has_bokeh_png_deps = false
22 0
        throw(err)
23
    end
24 20
    return nothing
25
end
26

27 20
load_backend(::Val{:bokeh}) = initialize_bokeh()
28

29 20
convert_result(f, axis, ::Val{:bokeh}) = BokehPlot(axis)
30
function convert_result(f, axes::AbstractArray, ::Val{:bokeh})
31 20
    return BokehPlot(arviz.plots.backends.create_layout(axes))
32
end
33

34
"""
35
    BokehPlot(::PyObject)
36

37
Loose wrapper around a Bokeh figure, mostly used for dispatch.
38

39
In most cases, use one of the plotting functions with `backend=:bokeh` to create a
40
`BokehPlot` instead of using a constructor.
41
"""
42
struct BokehPlot
43 20
    o::PyObject
44
end
45

46 20
BokehPlot(plot::BokehPlot) = plot
47

48 20
@inline PyObject(plot::BokehPlot) = getfield(plot, :o)
49

50 20
Base.convert(::Type{BokehPlot}, o::PyObject) = BokehPlot(o)
51

52 20
Base.hash(plot::BokehPlot) = hash(PyObject(plot))
53

54 20
Base.propertynames(plot::BokehPlot) = propertynames(PyObject(plot))
55

56
function Base.getproperty(plot::BokehPlot, name::Symbol)
57 20
    o = PyObject(plot)
58 20
    name === :o && return o
59 20
    return getproperty(o, name)
60
end
61

62
function render_html(plot::BokehPlot, name=nothing)
63 20
    obj = PyObject(plot)
64 20
    return bokeh.embed.file_html(obj, bokeh.resources.CDN, name)
65
end
66

67 0
Base.display(::REPL.REPLDisplay, plot::BokehPlot) = bokeh.plotting.show(plot)
68

69 20
Base.show(io::IO, ::MIME"text/html", plot::BokehPlot) = print(io, render_html(plot))
70
function Base.show(
71
    io::IO,
72
    ::Union{MIME"application/juno+plotpane",MIME"application/prs.juno.plotpane+html"},
73
    plot::BokehPlot,
74
)
75 20
    return print(io, render_html(plot))
76
end
77
function Base.show(io::IO, ::MIME"juliavscode/html", plot::BokehPlot)
78 20
    return print(io, render_html(plot))
79
end
80 0
function Base.show(io::IO, ::MIME"image/png", plot::BokehPlot)
81 0
    initialize_bokeh_png_deps()
82 0
    image = bokeh.io.export.get_screenshot_as_png(plot)
83 0
    print(io, image._repr_png_())
84 0
    return nothing
85
end
86

87
"""
88
    write(io::IO, plot::BokehPlot)
89
    write(filename::AbstractString, plot::BokehPlot)
90

91
Write the HTML representation of the Bokeh plot to the I/O stream or file.
92
"""
93 20
Base.write(io::IO, plot::BokehPlot) = print(io, render_html(plot))

Read our documentation on viewing source code .

Loading