Skip to content

Commit 7633909

Browse files
authored
Merge pull request #21 from JuliaComputing/jq/tables
Adjust use of Tables interface
2 parents e528dfc + 1852de8 commit 7633909

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/TableView.jl

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,29 @@ 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."))
22+
struct IteratorAndFirst{F, T}
23+
first::F
24+
source::T
25+
len::Int
26+
function IteratorAndFirst(x)
27+
len = Base.haslength(x) ? length(x) : 0
28+
first = iterate(x)
29+
return new{typeof(first), typeof(x)}(first, x, len)
2530
end
31+
end
32+
Base.IteratorSize(::Type{IteratorAndFirst{F, T}}) where {F, T} = Base.IteratorSize(T)
33+
Base.length(x::IteratorAndFirst) = x.len
34+
Base.IteratorEltype(::Type{IteratorAndFirst{F, T}}) where {F, T} = Base.IteratorEltype(T)
35+
Base.eltype(x::IteratorAndFirst) = eltype(x.source)
36+
Base.iterate(x::IteratorAndFirst) = x.first
37+
function Base.iterate(x::IteratorAndFirst, st)
38+
st === nothing && return nothing
39+
return iterate(x.source, st)
40+
end
2641

27-
tablelength = Base.IteratorSize(table) == Base.HasLength() ? length(Tables.rows(table)) : nothing
42+
function showtable(table; dark = false, height = :auto, width = "100%")
43+
rows = Tables.rows(table)
44+
tablelength = Base.IteratorSize(rows) == Base.HasLength() ? length(rows) : nothing
2845

2946
if height === :auto
3047
height = 500
@@ -34,14 +51,21 @@ function showtable(table; dark = false, height = :auto, width = "100%")
3451
end
3552
end
3653

37-
rows = Tables.rows(table)
38-
schema = Tables.schema(table)
54+
schema = Tables.schema(rows)
3955
if schema === nothing
56+
st = iterate(rows)
57+
rows = IteratorAndFirst(st, rows)
58+
names = Symbol[]
4059
types = []
41-
for (i, c) in enumerate(Tables.eachcolumn(first(rows)))
42-
push!(types, typeof(c))
60+
if st !== nothing
61+
row = st[1]
62+
for nm in propertynames(row)
63+
push!(names, nm)
64+
push!(types, typeof(getproperty(row, nm)))
65+
end
66+
else
67+
# no schema and no rows
4368
end
44-
names = collect(propertynames(first(rows)))
4569
else
4670
names = schema.names
4771
types = schema.types

0 commit comments

Comments
 (0)