Skip to content

Add conversion of Plots.jl's plotly plots #4

New issue

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

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

Already on GitHub? # to your account

Merged
merged 5 commits into from
Aug 3, 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
20 changes: 20 additions & 0 deletions .github/workflows/jl_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Julia tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
jl_version: ["1.6", "1.8", "1.9"]
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.jl_version }}
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
with:
annotate: true
28 changes: 26 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ version = "0.2.0"

[deps]
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"

[weakdeps]
PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5"
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

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

[compat]
JSON3 = "1"
julia = "1.2"
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"
PlotlyKaleido = "f2990250-8cf9-495f-b13a-cce12b45703c"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Plots", "PlotlyBase", "PlotlyJS", "PlotlyKaleido"]
13 changes: 13 additions & 0 deletions ext/DashBasePlotlyBaseExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module DashBasePlotlyBaseExt

import DashBase
import PlotlyBase
import PlotlyBase.JSON
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would of course break if PlotlyBase stops importing JSON.

I think this here is fine for now, but we should probably make the switch to using JSON3 exclusively inside DashBase.jl and Dash.jl. We should open an issue once this PR is merged.

Looks like PlotlyBase does define JSON3-compatible StructTypes https://github.com/JuliaPlots/PlotlyBase.jl/blob/master/src/json3.jl, but I haven't tested it


function DashBase.to_dash(p::PlotlyBase.Plot)
data = JSON.lower(p)
pop!(data, :config, nothing)
return data
end

end
10 changes: 10 additions & 0 deletions ext/DashBasePlotlyJSExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module DashBasePlotlyJSExt

import PlotlyJS
import DashBase

function DashBase.to_dash(p::PlotlyJS.SyncPlot)
DashBase.to_dash(p.plot)
end

end
18 changes: 18 additions & 0 deletions ext/DashBasePlotsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module DashBasePlotsExt

import Plots
import DashBase


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)
else
(data = Plots.plotly_series(p), layout = Plots.plotly_layout(p))
end
end
DashBase.to_dash(p::Plots.Plot{Plots.PlotlyJSBackend}) = Plots.plotlyjs_syncplot(p)

end
5 changes: 5 additions & 0 deletions src/DashBase.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module DashBase
import JSON3
import PackageExtensionCompat
include("components.jl")
include("registry.jl")
export Component, push_prop!, get_name, get_type, get_namespace,
Expand All @@ -8,4 +9,8 @@ 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

function __init__()
PackageExtensionCompat.@require_extensions
end

end # module
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Test
include("components.jl")
include("registry.jl")
include("test_ext.jl")
include("registry.jl")
25 changes: 25 additions & 0 deletions test/test_ext.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using DashBase
using Test
using Plots
plotlyjs()

@testset "PlotlyJS" begin
pl = @test_nowarn DashBase.to_dash(plot(1:5))
@test pl isa PlotlyJS.SyncPlot
Comment on lines +7 to +8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etpinard do these need to return something readable by JSON3 or is it enough, that they return something that has a to_dash method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Have you tried it inside a Dash.jl callback?

pl = @test_nowarn DashBase.to_dash(pl)
@test haskey(pl, :layout)
@test haskey(pl, :data)
@test haskey(pl, :frames)
@test !haskey(pl, :config)
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)
end