Skip to content

Commit

Permalink
try Unitful extension
Browse files Browse the repository at this point in the history
  • Loading branch information
joshday committed Sep 20, 2024
1 parent 2157f31 commit 00bbd5d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
14 changes: 11 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"

[compat]
Artifacts = "1.3"
Aqua = "0.8"
Artifacts = "1.3"
Cobweb = "0.6"
Downloads = "1.6"
EasyConfig = "0.1"
JSON3 = "1.14"
StructTypes = "1.10"
julia = "1.7"
Unitful = "1"
julia = "1.9"

[weakdeps]
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
PlotlyLightUnitfulExt = "Unitful"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Aqua"]
test = ["Test", "Unitful", "Aqua"]
17 changes: 10 additions & 7 deletions src/PlotlyLight.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ end
settings::Settings = Settings()

#-----------------------------------------------------------------------------# utils/other
fix_matrix(x::Config) = Config(k => fix_matrix(v) for (k,v) in pairs(x))
fix_matrix(x) = x
fix_matrix(x::AbstractMatrix) = eachrow(x)
# Converting data to JSON is `JSON3.write` with some exceptions
_json(x) = JSON3.write(x; allow_inf=true)
_json(x::AbstractMatrix) = _json(eachrow(x))
_json(x::Config) = JSON3.write(x; allow_inf=true)
_json(x::Union{AbstractDict, NamedTuple}) = _json(Config(k => _json(v) for (k,v) in pairs(x)))


attributes(t::Symbol) = plotly.schema.traces[t].attributes
check_attribute(trace, attr::Symbol) = haskey(attributes(Symbol(trace)), attr) || @warn("`$trace` does not have attribute `$attr`.")
Expand Down Expand Up @@ -78,9 +81,9 @@ Base.getproperty(::typeof(plot), x::Symbol) = (; kw...) -> plot(x; kw...)

#-----------------------------------------------------------------------------# display/show
function html_div(o::Plot; id=randstring(10))
data = JSON3.write(fix_matrix.(o.data); allow_inf=true)
layout = JSON3.write(merge(settings.layout, o.layout); allow_inf=true)
config = JSON3.write(merge(settings.config, o.config); allow_inf=true)
data = _json(o.data)
layout = _json(merge(settings.layout, o.layout))
config = _json(merge(settings.config, o.config))
h.div(class="plotlylight-parent-div",
settings.src,
settings.div(; id, class="plotlylight-plot-div"),
Expand All @@ -106,7 +109,7 @@ end
Base.show(io::IO, ::MIME"text/html", o::Plot) = show(io, MIME"text/html"(), html_iframe(o))
Base.show(io::IO, ::MIME"juliavscode/html", o::Plot) = show(io, MIME"text/html"(), o)

Base.display(::REPL.REPLDisplay, o::Plot) = Cobweb.preview(h.html(h.body(o, style="margin: 0px;")), reuse=settings.reuse_preview)
Base.display(::REPL.REPLDisplay, o::Plot) = Cobweb.preview(html_page(o), reuse=settings.reuse_preview)

mathjax_script = h.script(type="text/javascript", async=true, src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js")

Expand Down
13 changes: 11 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using PlotlyLight
using PlotlyLight: settings
using PlotlyLight: settings, _json
using Cobweb
using Cobweb: h
using JSON3: JSON3
using Test
using Aqua
using Unitful

html(x) = repr("text/html", x)

Expand Down Expand Up @@ -61,7 +62,7 @@ end
@testset "other" begin
@test propertynames(Plot()) isa Vector{Symbol}
@test all(x in propertynames(Plot()) for x in propertynames(plot))
@test PlotlyLight.fix_matrix([1 2; 3 4]) == [[1, 2], [3, 4]]
@test _json([1 2; 3 4]) == _json([[1, 2], [3, 4]])
@test propertynames(JSON3.read(JSON3.write(Plot()))) == [:data, :layout, :config]
end

Expand All @@ -78,6 +79,14 @@ end
end
end

#-----------------------------------------------------------------------------# extensions
@testset "extensions" begin
# Unitful
x = 1u"kg"
@test _json(x) == _json(1)
@test _json([x, x]) == _json([1, 1])
end

#-----------------------------------------------------------------------------# Aqua
Aqua.test_all(PlotlyLight,
deps_compat=(; ignore =[:REPL, :Random], check_extras = (;ignore=[:Test])),
Expand Down

0 comments on commit 00bbd5d

Please sign in to comment.