19
19
to_css_size (s:: AbstractString ) = s
20
20
to_css_size (s:: Real ) = " $(s) px"
21
21
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)
25
30
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
26
41
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
28
45
29
46
if height === :auto
30
47
height = 500
@@ -34,14 +51,21 @@ function showtable(table; dark = false, height = :auto, width = "100%")
34
51
end
35
52
end
36
53
37
- rows = Tables. rows (table)
38
- schema = Tables. schema (table)
54
+ schema = Tables. schema (rows)
39
55
if schema === nothing
56
+ st = iterate (rows)
57
+ rows = IteratorAndFirst (st, rows)
58
+ names = Symbol[]
40
59
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
43
68
end
44
- names = collect (propertynames (first (rows)))
45
69
else
46
70
names = schema. names
47
71
types = schema. types
0 commit comments