Skip to content
Draft
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CSV = "0.10"
DataFrames = "1"
IJulia = "1"
JSExpr = "0.5, 1"
JSON = "0.20, 0.21"
JSON = "0.20, 0.21, 1"
JSON3 = "1"
PlotlyBase = "0.8.15"
PlotlyKaleido = "2.2.5"
Expand Down
3 changes: 2 additions & 1 deletion src/PlotlyJS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const _mathjax_cdn_path =
struct PlotlyJSDisplay <: AbstractDisplay end

# include the rest of the core parts of the package
include("jsoncompat.jl")
include("display.jl")
include("util.jl")
include("kaleido.jl")
Expand Down Expand Up @@ -87,7 +88,7 @@ function dataset(name::String)::Dict{String,Any}
return Dict(zip(data[1, :], data[2:end, i] for i in 1:size(data, 2)))
elseif endswith(ds_path, "json")
# use json
return JSON.parsefile(ds_path)
return JSON.parsefile(ds_path; dicttype=Dict{String,Any})
end
error("should not ever get here!!! Please file an issue")
end
Expand Down
12 changes: 12 additions & 0 deletions src/jsoncompat.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# JSON.jl compatibility

# JSON.jl < 1 writes non-finite floats (e.g. `NaN` used for gaps in trace data)
# as `null`, whereas JSON.jl v1 throws on them by default. Keep the old behavior.
@static if isdefined(JSON, :JSONStyle) # JSON.jl >= 1
struct PlotlyJSSerialization <: JSON.JSONStyle end
JSON.lower(::PlotlyJSSerialization, x::AbstractFloat) = isfinite(x) ? x : nothing

jsonstring(x) = JSON.json(x; style = PlotlyJSSerialization())
else
jsonstring(x) = JSON.json(x)
end
2 changes: 1 addition & 1 deletion src/kaleido.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function savefig(
_ensure_kaleido_running(; plotlyjs, plotly_version)
P = PlotlyKaleido.P
# convert payload to vector of bytes
bytes = transcode(UInt8, JSON.json(payload))
bytes = transcode(UInt8, jsonstring(payload))
write(P.stdin, bytes)
write(P.stdin, transcode(UInt8, "\n"))
flush(P.stdin)
Expand Down
6 changes: 6 additions & 0 deletions test/kaleido.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ end
end
end
end

@testset "kaleido with non-finite data" begin
# `NaN` is commonly used for gaps in trace data; it must be written as `null`
plt = Plot(scatter(x=1:4, y=[1.0, NaN, Inf, 4.0]))
@test startswith(String(savefig(plt; format="svg")), "<svg")
end
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ if Sys.islinux()
end
include("kaleido.jl")

@testset "SyncPlot with non-finite data" begin
# `NaN` is commonly used for gaps in trace data; it must be written as `null`
@test plot(scatter(x=1:4, y=[1.0, NaN, Inf, 4.0])) isa PlotlyJS.SyncPlot
end

# these are public API
@test isfile(PlotlyJS._js_path)
@test !isempty(PlotlyJS._js_version)
Expand Down