Skip to content

Commit

Permalink
migrate to weakdeps (#463)
Browse files Browse the repository at this point in the history
* migrate to weakdeps

* Update ext/DataFramesExt.jl

Co-authored-by: Thomas Christensen <[email protected]>

* fix loading issues

* add weakdeps to extras also

---------

Co-authored-by: Thomas Christensen <[email protected]>
Co-authored-by: Simon Christ <[email protected]>
  • Loading branch information
3 people authored Jan 19, 2024
1 parent c68b3b9 commit 457befe
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 31 deletions.
22 changes: 21 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,38 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
WebIO = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29"

[weakdeps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"

[extensions]
CSVExt = "CSV"
DataFramesExt = ["DataFrames", "CSV"]
IJuliaExt = "IJulia"
JSON3Ext = "JSON3"

[compat]
Blink = "0.12"
CSV = "0.10"
DataFrames = "1"
IJulia = "1"
JSExpr = "0.5, 1"
JSON = "0.20, 0.21"
JSON3 = "1"
PlotlyBase = "0.8.15"
Reexport = "0.2, 1"
Requires = "1.0"
WebIO = "0.8"
julia = "1.3, 1.4, 1.5, 1.6"

[extras]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["Test", "CSV", "DataFrames", "IJulia", "JSON3"]
14 changes: 14 additions & 0 deletions ext/CSVExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module CSVExt

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

function PlotlyJS.dataset(::Type{CSV.File}, name::String)
ds_path = PlotlyJS.check_dataset_exists(name)
if !endswith(ds_path, "csv")
error("Can only construct CSV.File from a csv data source")
end
CSV.File(ds_path)
end

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

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


PlotlyJS.dataset(::Type{DataFrames.DataFrame}, name::String) = DataFrames.DataFrame(PlotlyJS.dataset(CSV.File, name))

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

using PlotlyJS
isdefined(Base, :get_extension) ? (using IJulia) : (using ..IJulia)
isdefined(Base, :get_extension) ? (using JSON) : (using ..JSON)
isdefined(Base, :get_extension) ? (using PlotlyBase) : (using ..PlotlyBase)


function IJulia.display_dict(p::PlotlyJS.SyncPlot)
Dict(
"application/vnd.plotly.v1+json" => JSON.lower(p),
"text/plain" => sprint(show, "text/plain", p),
"text/html" => let
buf = IOBuffer()
show(buf, MIME("text/html"), p)
String(resize!(buf.data, buf.size))
end
)
end

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

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

JSON3.write(io::IO, p::PlotlyJS.SyncPlot) = JSON3.write(io, p.plot)
JSON3.write(p::PlotlyJS.SyncPlot) = JSON3.write(p.plot)

end
40 changes: 10 additions & 30 deletions src/PlotlyJS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ using JSExpr
using JSExpr: @var, @new
using Blink
using Pkg.Artifacts
using Requires
if !isdefined(Base, :get_extension)
using Requires
end

export plot, dataset, list_datasets, make_subplots, savefig, mgrid

Expand Down Expand Up @@ -131,37 +133,15 @@ function __init__()
insert!(Base.Multimedia.displays, findlast(x -> x isa REPL.REPLDisplay, Base.Multimedia.displays) + 1, PlotlyJSDisplay())
end)

@require JSON2 = "2535ab7d-5cd8-5a07-80ac-9b1792aadce3" JSON2.write(io::IO, p::SyncPlot) = JSON2.write(io, p.plot)
@require JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" begin
JSON3.write(io::IO, p::SyncPlot) = JSON.print(io, p.plot)
JSON3.write(p::SyncPlot) = JSON.json(p.plot)
end

@require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" begin

function IJulia.display_dict(p::SyncPlot)
Dict(
"application/vnd.plotly.v1+json" => JSON.lower(p),
"text/plain" => sprint(show, "text/plain", p),
"text/html" => let
buf = IOBuffer()
show(buf, MIME("text/html"), p)
String(resize!(buf.data, buf.size))
end
)
end
end
@static if !isdefined(Base, :get_extension)
@require JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" include("../ext/JSON3Ext.jl")
@require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" include("../ext/IJuliaExt.jl")

@require CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" begin
function dataset(::Type{CSV.File}, name::String)
ds_path = check_dataset_exists(name)
if !endswith(ds_path, "csv")
error("Can only construct CSV.File from a csv data source")
@require CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" begin
include("../ext/CSVExt.jl")
@require DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
include("../ext/DataFramesExt.jl")
end
CSV.File(ds_path)
end
@require DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
dataset(::Type{DataFrames.DataFrame}, name::String) = DataFrames.DataFrame(dataset(CSV.File, name))
end
end

Expand Down

0 comments on commit 457befe

Please sign in to comment.