Skip to content

Commit 457befe

Browse files
sjkellythchrBeastyBlacksmith
authored
migrate to weakdeps (#463)
* 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]>
1 parent c68b3b9 commit 457befe

File tree

6 files changed

+85
-31
lines changed

6 files changed

+85
-31
lines changed

Project.toml

+21-1
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,38 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1818
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1919
WebIO = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29"
2020

21+
[weakdeps]
22+
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
23+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
24+
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
25+
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
26+
27+
[extensions]
28+
CSVExt = "CSV"
29+
DataFramesExt = ["DataFrames", "CSV"]
30+
IJuliaExt = "IJulia"
31+
JSON3Ext = "JSON3"
32+
2133
[compat]
2234
Blink = "0.12"
35+
CSV = "0.10"
36+
DataFrames = "1"
37+
IJulia = "1"
2338
JSExpr = "0.5, 1"
2439
JSON = "0.20, 0.21"
40+
JSON3 = "1"
2541
PlotlyBase = "0.8.15"
2642
Reexport = "0.2, 1"
2743
Requires = "1.0"
2844
WebIO = "0.8"
2945
julia = "1.3, 1.4, 1.5, 1.6"
3046

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

3454
[targets]
35-
test = ["Test"]
55+
test = ["Test", "CSV", "DataFrames", "IJulia", "JSON3"]

ext/CSVExt.jl

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module CSVExt
2+
3+
using PlotlyJS
4+
isdefined(Base, :get_extension) ? (using CSV) : (using ..CSV)
5+
6+
function PlotlyJS.dataset(::Type{CSV.File}, name::String)
7+
ds_path = PlotlyJS.check_dataset_exists(name)
8+
if !endswith(ds_path, "csv")
9+
error("Can only construct CSV.File from a csv data source")
10+
end
11+
CSV.File(ds_path)
12+
end
13+
14+
end

ext/DataFramesExt.jl

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module DataFramesExt
2+
3+
using PlotlyJS
4+
isdefined(Base, :get_extension) ? (using DataFrames) : (using ..DataFrames)
5+
isdefined(Base, :get_extension) ? (using CSV) : (using ..CSV)
6+
7+
8+
PlotlyJS.dataset(::Type{DataFrames.DataFrame}, name::String) = DataFrames.DataFrame(PlotlyJS.dataset(CSV.File, name))
9+
10+
end

ext/IJuliaExt.jl

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module IJuliaExt
2+
3+
using PlotlyJS
4+
isdefined(Base, :get_extension) ? (using IJulia) : (using ..IJulia)
5+
isdefined(Base, :get_extension) ? (using JSON) : (using ..JSON)
6+
isdefined(Base, :get_extension) ? (using PlotlyBase) : (using ..PlotlyBase)
7+
8+
9+
function IJulia.display_dict(p::PlotlyJS.SyncPlot)
10+
Dict(
11+
"application/vnd.plotly.v1+json" => JSON.lower(p),
12+
"text/plain" => sprint(show, "text/plain", p),
13+
"text/html" => let
14+
buf = IOBuffer()
15+
show(buf, MIME("text/html"), p)
16+
String(resize!(buf.data, buf.size))
17+
end
18+
)
19+
end
20+
21+
end

ext/JSON3Ext.jl

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module JSON3Ext
2+
3+
using PlotlyJS
4+
isdefined(Base, :get_extension) ? (using JSON3) : (using ..JSON3)
5+
6+
JSON3.write(io::IO, p::PlotlyJS.SyncPlot) = JSON3.write(io, p.plot)
7+
JSON3.write(p::PlotlyJS.SyncPlot) = JSON3.write(p.plot)
8+
9+
end

src/PlotlyJS.jl

+10-30
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ using JSExpr
1919
using JSExpr: @var, @new
2020
using Blink
2121
using Pkg.Artifacts
22-
using Requires
22+
if !isdefined(Base, :get_extension)
23+
using Requires
24+
end
2325

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

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

134-
@require JSON2 = "2535ab7d-5cd8-5a07-80ac-9b1792aadce3" JSON2.write(io::IO, p::SyncPlot) = JSON2.write(io, p.plot)
135-
@require JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" begin
136-
JSON3.write(io::IO, p::SyncPlot) = JSON.print(io, p.plot)
137-
JSON3.write(p::SyncPlot) = JSON.json(p.plot)
138-
end
139-
140-
@require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" begin
141-
142-
function IJulia.display_dict(p::SyncPlot)
143-
Dict(
144-
"application/vnd.plotly.v1+json" => JSON.lower(p),
145-
"text/plain" => sprint(show, "text/plain", p),
146-
"text/html" => let
147-
buf = IOBuffer()
148-
show(buf, MIME("text/html"), p)
149-
String(resize!(buf.data, buf.size))
150-
end
151-
)
152-
end
153-
end
136+
@static if !isdefined(Base, :get_extension)
137+
@require JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" include("../ext/JSON3Ext.jl")
138+
@require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" include("../ext/IJuliaExt.jl")
154139

155-
@require CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" begin
156-
function dataset(::Type{CSV.File}, name::String)
157-
ds_path = check_dataset_exists(name)
158-
if !endswith(ds_path, "csv")
159-
error("Can only construct CSV.File from a csv data source")
140+
@require CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" begin
141+
include("../ext/CSVExt.jl")
142+
@require DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
143+
include("../ext/DataFramesExt.jl")
160144
end
161-
CSV.File(ds_path)
162-
end
163-
@require DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
164-
dataset(::Type{DataFrames.DataFrame}, name::String) = DataFrames.DataFrame(dataset(CSV.File, name))
165145
end
166146
end
167147

0 commit comments

Comments
 (0)