Skip to content

Fixup DashBasePlotsExt #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@ version = "0.2.0"

[deps]
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[weakdeps]
PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5"
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
[compat]
JSON3 = "1"
PlotlyBase = "0.8"
PlotlyJS = "0.18"
Plots = "1"
Requires = "1.3"
julia = "1.6"

[extensions]
DashBasePlotlyBaseExt = "PlotlyBase"
DashBasePlotlyJSExt = "PlotlyJS"
DashBasePlotsExt = "Plots"

[compat]
JSON3 = "1"
Plots = "1"
julia = "1.6"
PlotlyBase = "0.8"
PlotlyJS = "0.18"
PackageExtensionCompat = "1"

[extras]
PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5"
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
Expand All @@ -34,3 +29,8 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Plots", "PlotlyBase", "PlotlyJS", "PlotlyKaleido"]

[weakdeps]
PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5"
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
8 changes: 5 additions & 3 deletions ext/DashBasePlotlyBaseExt.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module DashBasePlotlyBaseExt

import DashBase
import PlotlyBase
import PlotlyBase.JSON
using DashBase

isdefined(Base, :get_extension) ? (using PlotlyBase) : (using ..PlotlyBase)

const JSON = PlotlyBase.JSON

function DashBase.to_dash(p::PlotlyBase.Plot)
data = JSON.lower(p)
Expand Down
5 changes: 3 additions & 2 deletions ext/DashBasePlotlyJSExt.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module DashBasePlotlyJSExt

import PlotlyJS
import DashBase
using DashBase

isdefined(Base, :get_extension) ? (using PlotlyJS) : (using ..PlotlyJS)

function DashBase.to_dash(p::PlotlyJS.SyncPlot)
DashBase.to_dash(p.plot)
Expand Down
15 changes: 10 additions & 5 deletions ext/DashBasePlotsExt.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
module DashBasePlotsExt

import Plots
import DashBase
using DashBase

isdefined(Base, :get_extension) ? (using Plots) : (using ..Plots)

function DashBase.to_dash(p::Plots.Plot{Plots.PlotlyBackend})
return if haskey(Base.loaded_modules, Base.PkgId(Base.UUID("a03496cd-edff-5a9b-9e67-9cda94a718b5"), "PlotlyBase")) &&
haskey(Base.loaded_modules, Base.PkgId(Base.UUID("f2990250-8cf9-495f-b13a-cce12b45703c"), "PlotlyKaleido"))
# Note: technically it would be sufficient if PlotlyBase is loaded, but thats how it is currently handled by Plots.jl
Plots.plotlybase_syncplot(p)
sp = Plots.plotlybase_syncplot(p)
DashBase.to_dash(sp)
else
(data = Plots.plotly_series(p), layout = Plots.plotly_layout(p))
Dict(:data => Plots.plotly_series(p), :layout => Plots.plotly_layout(p))
end
end
DashBase.to_dash(p::Plots.Plot{Plots.PlotlyJSBackend}) = Plots.plotlyjs_syncplot(p)

function DashBase.to_dash(p::Plots.Plot{Plots.PlotlyJSBackend})
sp = Plots.plotlyjs_syncplot(p)
return DashBase.to_dash(sp)
end

end
11 changes: 9 additions & 2 deletions src/DashBase.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module DashBase
import JSON3
import PackageExtensionCompat
include("components.jl")
include("registry.jl")
export Component, push_prop!, get_name, get_type, get_namespace,
Expand All @@ -9,8 +8,16 @@ get_dash_dependencies, get_dash_renderer_pkg, get_componens_pkgs,
has_relative_path, has_dev_path, has_external_url, get_type,
get_external_url, get_dev_path, get_relative_path

@static if !isdefined(Base, :get_extension)
using Requires
end

function __init__()
PackageExtensionCompat.@require_extensions
@static if !isdefined(Base, :get_extension)
@require PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5" include("../ext/DashBasePlotlyBaseExt.jl")
@require PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a" include("../ext/DashBasePlotlyJSExt.jl")
@require Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" include("../ext/DashBasePlotsExt.jl")
end
end

end # module
51 changes: 31 additions & 20 deletions test/test_ext.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
using DashBase
using Test
using Plots
plotlyjs()
using DashBase
import PlotlyBase
import PlotlyJS
import Plots

function run_assertions(pl)
obj = @test_nowarn DashBase.to_dash(pl)
@test obj isa Dict{Symbol, Any}
@test obj[:data][1][:y] == [1, 2, 3, 4, 5]
@test haskey(obj, :layout)
@test haskey(obj, :frames)
@test !haskey(obj, :config)
end

@testset "DashBasePlotlyBaseExt" begin
pl = PlotlyBase.Plot(1:5)
run_assertions(pl)
end

@testset "DashBasePlotsJSExt" begin
pl = PlotlyJS.plot(1:5)
run_assertions(pl)
end

@testset "PlotlyJS" begin
pl = @test_nowarn DashBase.to_dash(plot(1:5))
@test pl isa PlotlyJS.SyncPlot
pl = @test_nowarn DashBase.to_dash(pl)
@test haskey(pl, :layout)
@test haskey(pl, :data)
@test haskey(pl, :frames)
@test !haskey(pl, :config)
@testset "DashBasePlotsExt + plotlyjs()" begin
Plots.plotlyjs()
pl = Plots.plot(1:5)
run_assertions(pl)
end

plotly()
@testset "PlotlyBase" begin
pl = @test_nowarn DashBase.to_dash(plot(1:5))
@test pl isa PlotlyBase.Plot
pl = @test_nowarn DashBase.to_dash(pl)
@test haskey(pl, :layout)
@test haskey(pl, :data)
@test haskey(pl, :frames)
@test !haskey(pl, :config)
@testset "DashBasePlotsExt + plotly()" begin
Plots.plotly()
pl = Plots.plot(1:5)
run_assertions(pl)
end