Skip to content

Commit 41c9563

Browse files
committed
Adjust use of Tables interface
1 parent e528dfc commit 41c9563

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/TableView.jl

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,24 @@ end
1919
to_css_size(s::AbstractString) = s
2020
to_css_size(s::Real) = "$(s)px"
2121

22-
function showtable(table; dark = false, height = :auto, width = "100%")
23-
if !Tables.istable(typeof(table))
24-
throw(ArgumentError("Argument is not a table."))
25-
end
22+
struct IteratorAndFirst{F, T}
23+
first::F
24+
source::T
25+
IteratorAndFirst(x) = new(iterate(x), x)
26+
end
27+
Base.IteratorSize(::Type{IteratorAndFirst{F, T}}) where {F, T} = Base.IteratorSize(T)
28+
Base.length(x::IteratorAndFirst) = length(x.source)
29+
Base.IteratorEltype(::Type{IteratorAndFirst{F, T}}) where {F, T} = Base.IteratorEltype(T)
30+
Base.eltype(x::IteratorAndFirst) = eltype(x.source)
31+
Base.iterate(x::IteratorAndFirst) = x.first
32+
function Base.iterate(x::IteratorAndFirst, st)
33+
st === nothing && return nothing
34+
return iterate(x.source)
35+
end
2636

27-
tablelength = Base.IteratorSize(table) == Base.HasLength() ? length(Tables.rows(table)) : nothing
37+
function showtable(table; dark = false, height = :auto, width = "100%")
38+
rows = Table.rows(table)
39+
tablelength = Base.IteratorSize(rows) == Base.HasLength() ? length(rows) : nothing
2840

2941
if height === :auto
3042
height = 500
@@ -34,14 +46,21 @@ function showtable(table; dark = false, height = :auto, width = "100%")
3446
end
3547
end
3648

37-
rows = Tables.rows(table)
38-
schema = Tables.schema(table)
49+
schema = Tables.schema(rows)
3950
if schema === nothing
51+
st = iterate(rows)
52+
rows = IteratorAndFirst(st, rows)
53+
names = Symbol[]
4054
types = []
41-
for (i, c) in enumerate(Tables.eachcolumn(first(rows)))
42-
push!(types, typeof(c))
55+
if st !== nothing
56+
row = st[1]
57+
for nm in propertynames(row)
58+
push!(names, nm)
59+
push!(types, typeof(getproperty(row, nm)))
60+
end
61+
else
62+
# no schema and no rows
4363
end
44-
names = collect(propertynames(first(rows)))
4564
else
4665
names = schema.names
4766
types = schema.types

0 commit comments

Comments
 (0)