Skip to content

Commit

Permalink
Add HTML repr for InferenceData (#91)
Browse files Browse the repository at this point in the history
* Catch html character entities

* Use html repr if available and requested

* Test html repr for InferenceData

* Increment version number

* Call PyObject

* Use right variable name

* Drop prefix

* Rewrite tests
  • Loading branch information
sethaxen authored Oct 3, 2020
1 parent 3b1ef94 commit 871903c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ArviZ"
uuid = "131c737c-5715-5e2e-ad31-c244f01c1dc7"
authors = ["Seth Axen <[email protected]>"]
version = "0.4.4"
version = "0.4.5"

[deps]
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
Expand Down
9 changes: 9 additions & 0 deletions src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ function Base.show(io::IO, data::InferenceData)
print(io, out)
return nothing
end
function Base.show(io::IO, ::MIME"text/html", data::InferenceData)
obj = PyObject(data)
(:_repr_html_ in propertynames(obj)) || return show(io, data)
out = obj._repr_html_()
out = replace(out, r"arviz.InferenceData" => "InferenceData")
out = replace(out, r"(<|&lt;)?xarray.Dataset(>|&gt;)?" => "Dataset (xarray.Dataset)")
print(io, out)
return nothing
end

"""
groupnames(data::InferenceData) -> Vector{Symbol}
Expand Down
4 changes: 2 additions & 2 deletions src/dataset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ function Base.show(io::IO, data::Dataset)
return nothing
end
function Base.show(io::IO, ::MIME"text/html", data::Dataset)
obj = data.o
obj = PyObject(data)
(:_repr_html_ in propertynames(obj)) || return show(io, data)
out = obj._repr_html_()
out = replace(out, r"<?xarray.Dataset>?" => "Dataset (xarray.Dataset)")
out = replace(out, r"(<|&lt;)?xarray.Dataset(>|&gt;)?" => "Dataset (xarray.Dataset)")
print(io, out)
return nothing
end
Expand Down
19 changes: 16 additions & 3 deletions test/test_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,22 @@ using MonteCarloMeasurements: Particles
end

@testset "show" begin
@test startswith(sprint(show, data), "InferenceData with groups:")
rest = split(PyObject(data).__str__(), '\n'; limit = 2)[2]
@test split(sprint(show, data), '\n'; limit = 2)[2] == rest
@testset "plain" begin
text = sprint(show, data)
@test startswith(text, "InferenceData with groups:")
rest = split(PyObject(data).__str__(), '\n'; limit = 2)[2]
@test split(text, '\n'; limit = 2)[2] == rest
end

@testset "html" begin
text = repr(MIME("text/html"), data)
@test text isa String
@test !occursin("<xarray.Dataset>", text)
@test !occursin("&lt;xarray.Dataset&gt;", text)
@test !occursin("arviz.InferenceData", text)
@test occursin("Dataset (xarray.Dataset)", text)
@test occursin("InferenceData", text)
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/test_dataset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
@testset "$mimetype" for mimetype in ("plain", "html")
text = repr(MIME("text/$(mimetype)"), dataset)
@test text isa String
@test !occursin("<xarray.Dataset>", text)
@test !occursin("&lt;xarray.Dataset&gt;", text)
@test occursin("Dataset (xarray.Dataset)", text)
end
end
Expand Down

2 comments on commit 871903c

@sethaxen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/22354

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.5 -m "<description of version>" 871903c6d39d8ecb1a68f890d142ad65f4307929
git push origin v0.4.5

Please sign in to comment.