diff --git a/Project.toml b/Project.toml index db93019..72cc596 100644 --- a/Project.toml +++ b/Project.toml @@ -18,10 +18,26 @@ 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" @@ -29,7 +45,11 @@ 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"] diff --git a/ext/CSVExt.jl b/ext/CSVExt.jl new file mode 100644 index 0000000..fef2330 --- /dev/null +++ b/ext/CSVExt.jl @@ -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 \ No newline at end of file diff --git a/ext/DataFramesExt.jl b/ext/DataFramesExt.jl new file mode 100644 index 0000000..bc3333e --- /dev/null +++ b/ext/DataFramesExt.jl @@ -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 \ No newline at end of file diff --git a/ext/IJuliaExt.jl b/ext/IJuliaExt.jl new file mode 100644 index 0000000..9bf1974 --- /dev/null +++ b/ext/IJuliaExt.jl @@ -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 diff --git a/ext/JSON3Ext.jl b/ext/JSON3Ext.jl new file mode 100644 index 0000000..1b0ed83 --- /dev/null +++ b/ext/JSON3Ext.jl @@ -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 diff --git a/src/PlotlyJS.jl b/src/PlotlyJS.jl index 36a221b..0d3b252 100644 --- a/src/PlotlyJS.jl +++ b/src/PlotlyJS.jl @@ -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 @@ -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